branch: scratch/editorconfig
commit abd2e42c90806d245b30d001ce3ff311e7b529e2
Author: Stefan Monnier <monn...@iro.umontreal.ca>
Commit: Stefan Monnier <monn...@iro.umontreal.ca>

    (editorconfig-fnmatch--do-translate): Reduce redundancy
    
    * editorconfig-fnmatch.el (editorconfig-fnmatch--do-translate):
    Consolidate the `push`s outside of `cl-case`.  Use `regexp-opt`.
---
 editorconfig-fnmatch.el | 236 ++++++++++++++++++++++--------------------------
 1 file changed, 110 insertions(+), 126 deletions(-)

diff --git a/editorconfig-fnmatch.el b/editorconfig-fnmatch.el
index 374c814394..a3edfc3c5d 100644
--- a/editorconfig-fnmatch.el
+++ b/editorconfig-fnmatch.el
@@ -160,132 +160,116 @@ translation is found for PATTERN."
         (setq current-char (aref pattern index)
               index (1+ index))
 
-        (cl-case current-char
-          (?*
-           (setq pos index)
-           (if (and (< pos length)
-                    (= (aref pattern pos) ?*))
-               (push ".*" result)
-             (push "[^/]*" result)))
-
-          (??
-           (push "[^/]" result))
-
-          (?\[
-           (if in-brackets
-               (push "\\[" result)
-             (if (= (aref pattern index) ?/)
-                 ;; Slash after an half-open bracket
-                 (progn
-                   (push "\\[/" result)
-                   (setq index (+ index 1)))
-               (setq pos index
-                     has-slash nil)
-               (while (and (< pos length)
-                           (not (= (aref pattern pos) ?\]))
-                           (not has-slash))
-                 (if (and (= (aref pattern pos) ?/)
-                          (not (= (aref pattern (- pos 1)) ?\\)))
-                     (setq has-slash t)
-                   (setq pos (1+ pos))))
-               (if has-slash
-                   (progn
-                     (push (concat "\\["
-                                   (substring pattern
-                                              index
-                                              (1+ pos))
-                                   "\\]")
-                           result)
-                     (setq index (+ pos 2)))
-                 (if (and (< index length)
-                          (memq (aref pattern index)
-                                '(?! ?^)))
-                     (progn
-                       (setq index (1+ index))
-                       (push "[^" result))
-                   (push "[" result))
-                 (setq in-brackets t)))))
-
-          (?-
-           (if in-brackets
-               (push "-" result)
-             (push "\\-" result)))
-
-          (?\]
-           (push "]" result)
-           (setq in-brackets nil))
-
-          (?{
-           (setq pos index
-                 has-comma nil)
-           (while (and (or (and (< pos length)
-                                (not (= (aref pattern pos) ?})))
-                           is-escaped)
-                       (not has-comma))
-             (if (and (eq (aref pattern pos) ?,)
-                      (not is-escaped))
-                 (setq has-comma t)
-               (setq is-escaped (and (eq (aref pattern pos)
-                                         ?\\)
-                                     (not is-escaped))
-                     pos (1+ pos))))
-           (if (and (not has-comma)
-                    (< pos length))
-               (let ((pattern-sub (substring pattern index pos)))
-                 (setq num-range (string-match 
editorconfig-fnmatch--numeric-range-regexp
-                                               pattern-sub))
-                 (if num-range
-                     (let ((number-start (string-to-number (match-string 1
-                                                                         
pattern-sub)))
-                           (number-end (string-to-number (match-string 2
-                                                                       
pattern-sub))))
-                       (push (concat "\\(?:"
-                                     (mapconcat #'number-to-string
-                                                (cl-loop for i from 
number-start to number-end
-                                                         collect i)
-                                                "\\|")
-                                     "\\)")
-                             result))
-                   (let ((inner (editorconfig-fnmatch--do-translate 
pattern-sub t)))
-                     (push (format "{%s}" inner) result)))
-                 (setq index (1+ pos)))
-             (if matching-braces
-                 (progn
-                   (push "\\(?:" result)
-                   (setq brace-level (1+ brace-level)))
-               (push "{" result))))
-
-          (?,
-           (if (and (> brace-level 0)
-                    (not is-escaped))
-               (push "\\|" result)
-             (push "\\," result)))
-
-          (?}
-           (if (and (> brace-level 0)
-                    (not is-escaped))
-               (progn
-                 (push "\\)" result)
-                 (setq brace-level (- brace-level 1)))
-             (push "}" result)))
-
-          (?/
-           (if (and (<= (+ index 3) (length pattern))
-                    (string= (substring pattern index (+ index 3)) "**/"))
-               (progn
-                 (push "\\(?:/\\|/.*/\\)" result)
-                 (setq index (+ index 3)))
-             (push "/" result)))
-
-          (t
-           (unless (= current-char ?\\)
-             (push (regexp-quote (char-to-string current-char)) result))))
-
-        (if (= current-char ?\\)
-            (progn (when is-escaped
-                     (push "\\\\" result))
-                   (setq is-escaped (not is-escaped)))
-          (setq is-escaped nil))))
+        (push
+         (cl-case current-char
+           (?*
+            (setq pos index)
+            (if (and (< pos length)
+                     (= (aref pattern pos) ?*))
+                ".*"
+              "[^/]*"))
+
+           (?? "[^/]")
+
+           (?\[
+            (if in-brackets
+                "\\["
+              (if (= (aref pattern index) ?/)
+                  ;; Slash after an half-open bracket
+                  (progn
+                    (setq index (+ index 1))
+                    "\\[/")
+                (setq pos index
+                      has-slash nil)
+                (while (and (< pos length)
+                            (not (= (aref pattern pos) ?\]))
+                            (not has-slash))
+                  (if (and (= (aref pattern pos) ?/)
+                           (not (= (aref pattern (- pos 1)) ?\\)))
+                      (setq has-slash t)
+                    (setq pos (1+ pos))))
+                (if has-slash
+                    (let ((content (substring pattern index (1+ pos))))
+                      (setq index (+ pos 2))
+                      (concat "\\[" content "\\]"))
+                  (setq in-brackets t)
+                  (if (and (< index length)
+                           (memq (aref pattern index)
+                                 '(?! ?^)))
+                      (progn
+                        (setq index (1+ index))
+                        "[^")
+                    "[")))))
+
+           (?- (if in-brackets "-" "\\-"))
+
+           (?\] (setq in-brackets nil) "]")
+
+           (?\{
+            (setq pos index
+                  has-comma nil)
+            (while (and (or (and (< pos length)
+                                 (not (= (aref pattern pos) ?})))
+                            is-escaped)
+                        (not has-comma))
+              (if (and (eq (aref pattern pos) ?,)
+                       (not is-escaped))
+                  (setq has-comma t)
+                (setq is-escaped (and (eq (aref pattern pos)
+                                          ?\\)
+                                      (not is-escaped))
+                      pos (1+ pos))))
+            (if (and (not has-comma)
+                     (< pos length))
+                (let ((pattern-sub (substring pattern index pos)))
+                  (setq num-range (string-match
+                                   editorconfig-fnmatch--numeric-range-regexp
+                                   pattern-sub))
+                  (setq index (1+ pos))
+                  (if num-range
+                      (let ((number-start (string-to-number
+                                           (match-string 1 pattern-sub)))
+                            (number-end (string-to-number
+                                         (match-string 2 pattern-sub))))
+                        (regexp-opt
+                         (mapcar #'number-to-string
+                                 (cl-loop for i from number-start to number-end
+                                          collect i))))
+                    (let ((inner (editorconfig-fnmatch--do-translate
+                                  pattern-sub t)))
+                      (format "{%s}" inner))))
+              (if matching-braces
+                  (progn
+                    (setq brace-level (1+ brace-level))
+                    "\\(?:")
+                "{")))
+
+           (?,
+            (if (and (> brace-level 0)
+                     (not is-escaped))
+                "\\|" "\\,"))
+
+           (?\}
+            (if (and (> brace-level 0)
+                     (not is-escaped))
+                (progn
+                  (setq brace-level (- brace-level 1))
+                  "\\)")
+              "}"))
+
+           (?/
+            (if (and (<= (+ index 3) (length pattern))
+                     (string= (substring pattern index (+ index 3)) "**/"))
+                (progn
+                  (setq index (+ index 3))
+                  "\\(?:/\\|/.*/\\)")
+              "/"))
+
+           (?\\ (when is-escaped "\\\\"))
+           (t (regexp-quote (char-to-string current-char))))
+         result)
+
+        (setq is-escaped (and (= current-char ?\\) (not is-escaped)))))
     (unless nested
       (setq result `("\\'" ,@result "\\`")))
     (apply #'concat (reverse result))))

Reply via email to