Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fnmatch-p #171

Merged
merged 3 commits into from
Jul 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions editorconfig-fnmatch.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@

(require 'cl-lib)

(defvar editorconfig-fnmatch--cache-hash
(make-hash-table :test 'equal)
(defvar editorconfig-fnmatch--cache-hashtable
nil
"Cache of shell pattern and its translation.")
;; Clear cache on file reload
(setq editorconfig-fnmatch--cache-hashtable
(make-hash-table :test 'equal))


(defconst editorconfig-fnmatch--left-brace-regexp
Expand All @@ -82,8 +85,8 @@
num))

;;;###autoload
(defun editorconfig-fnmatch-p (name pattern)
"Test whether NAME match PATTERN.
(defun editorconfig-fnmatch-p (string pattern)
"Test whether STRING match PATTERN.

Matching ignores case if `case-fold-search' is non-nil.

Expand All @@ -97,27 +100,8 @@ be used:
[^name] Matches any single character not in name
{s1,s2,s3} Matches any of the strings given (separated by commas)
{min..max} Matches any number between min and max"
(let* ((translated (editorconfig-fnmatch-translate pattern))
(re (car translated))
(num-groups (nth 1 translated))
(match (string-match re name))
(num-groups-len (length num-groups))
(pattern-matched t))
(when match
(let (num-group matched-num-str matched-num min-num max-num)
(dotimes (index num-groups-len)
(setq num-group (nth index num-groups))
(setq matched-num-str (match-string (1+ index)
name)
min-num (car num-group)
max-num (nth 1 num-group))
(setq matched-num (string-to-number matched-num-str))
(when (or (= (aref matched-num-str 0)
?0)
(< matched-num min-num)
(< max-num matched-num))
(setq pattern-matched nil))))
pattern-matched)))
(string-match (editorconfig-fnmatch-translate pattern)
string))

;;(editorconfig-fnmatch-translate "{a,{-3..3}}.js")
;;(editorconfig-fnmatch-p "1.js" "{a,{-3..3}}.js")
Expand All @@ -127,11 +111,11 @@ be used:

Translation result will be cached, so same translation will not be done twice."
(let ((cached (gethash pattern
editorconfig-fnmatch--cache-hash)))
editorconfig-fnmatch--cache-hashtable)))
(or cached
(puthash pattern
(editorconfig-fnmatch--do-translate pattern)
editorconfig-fnmatch--cache-hash))))
editorconfig-fnmatch--cache-hashtable))))


(defun editorconfig-fnmatch--do-translate (pattern &optional nested)
Expand All @@ -154,7 +138,6 @@ translation is found for PATTERN."
(editorconfig-fnmatch--match-num
editorconfig-fnmatch--right-brace-regexp
pattern)))
(numeric-groups ())

current-char
pos
Expand Down Expand Up @@ -249,16 +232,19 @@ translation is found for PATTERN."
(setq num-range (string-match editorconfig-fnmatch--numeric-range-regexp
pattern-sub))
(if num-range
(setq numeric-groups `(,@numeric-groups ,(mapcar 'string-to-number
(list (match-string 1
pattern-sub)
(match-string 2
pattern-sub))))
result `(,@result "\\([+-]?[0-9]+\\)"))
(let ((number-start (string-to-number (match-string 1
pattern-sub)))
(number-end (string-to-number (match-string 2
pattern-sub))))
(setq result `(,@result ,(concat "\\(?:"
(mapconcat 'number-to-string
(cl-loop for i from number-start to number-end
collect i)
"\\|")
"\\)"))))
(let ((inner (editorconfig-fnmatch--do-translate pattern-sub t)))
(setq result `(,@result ,(format "{%s}"
(car inner)))
numeric-groups `(,@numeric-groups ,@(nth 1 inner)))))
inner)))))
(setq index (1+ pos)))
(if matching-braces
(setq result `(,@result "\\(?:")
Expand Down Expand Up @@ -299,10 +285,7 @@ translation is found for PATTERN."
(setq is-escaped nil))))
(unless nested
(setq result `("^" ,@result "\\'")))
(list (mapconcat 'identity
result
"")
numeric-groups)))
(apply 'concat result)))

(provide 'editorconfig-fnmatch)

Expand Down