diff --git a/editorconfig-fnmatch.el b/editorconfig-fnmatch.el index 1d3bb125..69a0c766 100644 --- a/editorconfig-fnmatch.el +++ b/editorconfig-fnmatch.el @@ -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 @@ -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. @@ -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") @@ -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) @@ -154,7 +138,6 @@ translation is found for PATTERN." (editorconfig-fnmatch--match-num editorconfig-fnmatch--right-brace-regexp pattern))) - (numeric-groups ()) current-char pos @@ -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 "\\(?:") @@ -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)