]> code.delx.au - gnu-emacs/commitdiff
* lisp/character-fold.el: Code simplifications
authorArtur Malabarba <bruce.connor.am@gmail.com>
Sat, 28 Nov 2015 10:32:46 +0000 (10:32 +0000)
committerArtur Malabarba <bruce.connor.am@gmail.com>
Sat, 28 Nov 2015 15:06:37 +0000 (15:06 +0000)
(character-fold-table): Reduce the scope of a variable.
(character-fold-to-regexp): Change logic to work directly on the
input string.  It's a little easier to understand, probably
faster, and sets us up for implementing multi-char matches.

* test/automated/character-fold-tests.el
(character-fold--test-fold-to-regexp): New test.

lisp/character-fold.el
test/automated/character-fold-tests.el

index 4b526c2027716323dedc30465322617d3ec23e44..749d1135ce56317ac683352c707559fea0f13bf6 100644 (file)
 \f
 (defconst character-fold-table
   (eval-when-compile
-    (let* ((equiv (make-char-table 'character-fold-table))
-           (table (unicode-property-table-internal 'decomposition))
-           (func (char-table-extra-slot table 1)))
+    (let ((equiv (make-char-table 'character-fold-table))
+          (table (unicode-property-table-internal 'decomposition)))
       ;; Ensure the table is populated.
-      (map-char-table
-       (lambda (char v) (when (consp char) (funcall func (car char) v table)))
-       table)
+      (let ((func (char-table-extra-slot table 1)))
+        (map-char-table (lambda (char v)
+                          (when (consp char)
+                            (funcall func (car char) v table)))
+                        table))
 
       ;; Compile a list of all complex characters that each simple
       ;; character should match.
 Any character in STRING that has an entry in
 `character-fold-table' is replaced with that entry (which is a
 regexp) and other characters are `regexp-quote'd."
-  (let* ((spaces 0)
-         (chars (mapcar #'identity string))
-         (out chars))
+  (let ((spaces 0)
+        (i 0)
+        (end (length string))
+        (out nil))
     ;; When the user types a space, we want to match the table entry
     ;; for ?\s, which is generally a regexp like "[ ...]".  However,
     ;; the `search-spaces-regexp' variable doesn't "see" spaces inside
@@ -137,24 +139,19 @@ regexp) and other characters are `regexp-quote'd."
     ;; search engine acts on a bunch of spaces, not on individual
     ;; spaces, so if the string contains sequential spaces like "  ", we
     ;; need to keep them grouped together like this: "\\(  \\|[ ...][ ...]\\)".
-    (while chars
-      (let ((c (car chars)))
-        (setcar chars
-                (cond
-                 ((eq c ?\s)
-                  (setq spaces (1+ spaces))
-                  nil)
-                 ((> spaces 0)
-                  (prog1 (concat (character-fold--make-space-string spaces)
-                                 (or (aref character-fold-table c)
-                                     (regexp-quote (string c))))
-                    (setq spaces 0)))
-                 (t (or (aref character-fold-table c)
-                        (regexp-quote (string c))))))
-        (setq chars (cdr chars))))
-    (concat (apply #'concat out)
-            (when (> spaces 0)
-              (character-fold--make-space-string spaces)))))
+    (while (< i end)
+      (pcase (aref string i)
+        (`?\s (setq spaces (1+ spaces)))
+        (c (when (> spaces 0)
+             (push (character-fold--make-space-string spaces) out)
+             (setq spaces 0))
+           (push (or (aref character-fold-table c)
+                     (regexp-quote (string c)))
+                 out)))
+      (setq i (1+ i)))
+    (when (> spaces 0)
+      (push (character-fold--make-space-string spaces) out))
+    (apply #'concat (nreverse out))))
 
 \f
 ;;; Commands provided for completeness.
index 2b1a15c9e764d4e1a34f95c0bbf238c64dffd981..40f0aecf4494058e210828105023a1b062179538 100644 (file)
        (concat w1 "\s\n\s\t\f\t\n\r\t" w2)
        (concat w1 (make-string 90 ?\s) w2)))))
 
+(ert-deftest character-fold--test-fold-to-regexp ()
+  (let ((character-fold-table (make-char-table 'character-fold-table)))
+    (aset character-fold-table ?a "abc")
+    (aset character-fold-table ?1 "123")
+    (aset character-fold-table ?\s "-!-")
+    (should (equal (character-fold-to-regexp "a1a1")
+                   "abc123abc123"))
+    (should (equal (character-fold-to-regexp "a1  a 1")
+                   "abc123\\(?:  \\|-!--!-\\)abc\\(?: \\|-!-\\)123"))))
+
 (provide 'character-fold-tests)
 ;;; character-fold-tests.el ends here