diff options
| author | Artur Malabarba | 2015-11-28 10:32:46 +0000 |
|---|---|---|
| committer | Artur Malabarba | 2015-11-28 15:06:37 +0000 |
| commit | 35c7dc06ed1bd27fc69b2a79e88a8cfb2a6b3dae (patch) | |
| tree | 8bc77df75389f52131279071d450eac8c8b7721b | |
| parent | ea087151a901da36dd9ddc0843583906afafc7c5 (diff) | |
| download | emacs-35c7dc06ed1bd27fc69b2a79e88a8cfb2a6b3dae.tar.gz emacs-35c7dc06ed1bd27fc69b2a79e88a8cfb2a6b3dae.zip | |
* lisp/character-fold.el: Code simplifications
(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.
| -rw-r--r-- | lisp/character-fold.el | 51 | ||||
| -rw-r--r-- | test/automated/character-fold-tests.el | 10 |
2 files changed, 34 insertions, 27 deletions
diff --git a/lisp/character-fold.el b/lisp/character-fold.el index 4b526c20277..749d1135ce5 100644 --- a/lisp/character-fold.el +++ b/lisp/character-fold.el | |||
| @@ -25,13 +25,14 @@ | |||
| 25 | 25 | ||
| 26 | (defconst character-fold-table | 26 | (defconst character-fold-table |
| 27 | (eval-when-compile | 27 | (eval-when-compile |
| 28 | (let* ((equiv (make-char-table 'character-fold-table)) | 28 | (let ((equiv (make-char-table 'character-fold-table)) |
| 29 | (table (unicode-property-table-internal 'decomposition)) | 29 | (table (unicode-property-table-internal 'decomposition))) |
| 30 | (func (char-table-extra-slot table 1))) | ||
| 31 | ;; Ensure the table is populated. | 30 | ;; Ensure the table is populated. |
| 32 | (map-char-table | 31 | (let ((func (char-table-extra-slot table 1))) |
| 33 | (lambda (char v) (when (consp char) (funcall func (car char) v table))) | 32 | (map-char-table (lambda (char v) |
| 34 | table) | 33 | (when (consp char) |
| 34 | (funcall func (car char) v table))) | ||
| 35 | table)) | ||
| 35 | 36 | ||
| 36 | ;; Compile a list of all complex characters that each simple | 37 | ;; Compile a list of all complex characters that each simple |
| 37 | ;; character should match. | 38 | ;; character should match. |
| @@ -126,9 +127,10 @@ | |||
| 126 | Any character in STRING that has an entry in | 127 | Any character in STRING that has an entry in |
| 127 | `character-fold-table' is replaced with that entry (which is a | 128 | `character-fold-table' is replaced with that entry (which is a |
| 128 | regexp) and other characters are `regexp-quote'd." | 129 | regexp) and other characters are `regexp-quote'd." |
| 129 | (let* ((spaces 0) | 130 | (let ((spaces 0) |
| 130 | (chars (mapcar #'identity string)) | 131 | (i 0) |
| 131 | (out chars)) | 132 | (end (length string)) |
| 133 | (out nil)) | ||
| 132 | ;; When the user types a space, we want to match the table entry | 134 | ;; When the user types a space, we want to match the table entry |
| 133 | ;; for ?\s, which is generally a regexp like "[ ...]". However, | 135 | ;; for ?\s, which is generally a regexp like "[ ...]". However, |
| 134 | ;; the `search-spaces-regexp' variable doesn't "see" spaces inside | 136 | ;; the `search-spaces-regexp' variable doesn't "see" spaces inside |
| @@ -137,24 +139,19 @@ regexp) and other characters are `regexp-quote'd." | |||
| 137 | ;; search engine acts on a bunch of spaces, not on individual | 139 | ;; search engine acts on a bunch of spaces, not on individual |
| 138 | ;; spaces, so if the string contains sequential spaces like " ", we | 140 | ;; spaces, so if the string contains sequential spaces like " ", we |
| 139 | ;; need to keep them grouped together like this: "\\( \\|[ ...][ ...]\\)". | 141 | ;; need to keep them grouped together like this: "\\( \\|[ ...][ ...]\\)". |
| 140 | (while chars | 142 | (while (< i end) |
| 141 | (let ((c (car chars))) | 143 | (pcase (aref string i) |
| 142 | (setcar chars | 144 | (`?\s (setq spaces (1+ spaces))) |
| 143 | (cond | 145 | (c (when (> spaces 0) |
| 144 | ((eq c ?\s) | 146 | (push (character-fold--make-space-string spaces) out) |
| 145 | (setq spaces (1+ spaces)) | 147 | (setq spaces 0)) |
| 146 | nil) | 148 | (push (or (aref character-fold-table c) |
| 147 | ((> spaces 0) | 149 | (regexp-quote (string c))) |
| 148 | (prog1 (concat (character-fold--make-space-string spaces) | 150 | out))) |
| 149 | (or (aref character-fold-table c) | 151 | (setq i (1+ i))) |
| 150 | (regexp-quote (string c)))) | 152 | (when (> spaces 0) |
| 151 | (setq spaces 0))) | 153 | (push (character-fold--make-space-string spaces) out)) |
| 152 | (t (or (aref character-fold-table c) | 154 | (apply #'concat (nreverse out)))) |
| 153 | (regexp-quote (string c)))))) | ||
| 154 | (setq chars (cdr chars)))) | ||
| 155 | (concat (apply #'concat out) | ||
| 156 | (when (> spaces 0) | ||
| 157 | (character-fold--make-space-string spaces))))) | ||
| 158 | 155 | ||
| 159 | 156 | ||
| 160 | ;;; Commands provided for completeness. | 157 | ;;; Commands provided for completeness. |
diff --git a/test/automated/character-fold-tests.el b/test/automated/character-fold-tests.el index 2b1a15c9e76..40f0aecf449 100644 --- a/test/automated/character-fold-tests.el +++ b/test/automated/character-fold-tests.el | |||
| @@ -54,5 +54,15 @@ | |||
| 54 | (concat w1 "\s\n\s\t\f\t\n\r\t" w2) | 54 | (concat w1 "\s\n\s\t\f\t\n\r\t" w2) |
| 55 | (concat w1 (make-string 90 ?\s) w2))))) | 55 | (concat w1 (make-string 90 ?\s) w2))))) |
| 56 | 56 | ||
| 57 | (ert-deftest character-fold--test-fold-to-regexp () | ||
| 58 | (let ((character-fold-table (make-char-table 'character-fold-table))) | ||
| 59 | (aset character-fold-table ?a "abc") | ||
| 60 | (aset character-fold-table ?1 "123") | ||
| 61 | (aset character-fold-table ?\s "-!-") | ||
| 62 | (should (equal (character-fold-to-regexp "a1a1") | ||
| 63 | "abc123abc123")) | ||
| 64 | (should (equal (character-fold-to-regexp "a1 a 1") | ||
| 65 | "abc123\\(?: \\|-!--!-\\)abc\\(?: \\|-!-\\)123")))) | ||
| 66 | |||
| 57 | (provide 'character-fold-tests) | 67 | (provide 'character-fold-tests) |
| 58 | ;;; character-fold-tests.el ends here | 68 | ;;; character-fold-tests.el ends here |