]> git.eshelyaron.com Git - emacs.git/commitdiff
Safer and faster string sorting
authorMattias EngdegÄrd <mattiase@acm.org>
Sun, 12 May 2024 09:22:23 +0000 (11:22 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 12 May 2024 15:49:42 +0000 (17:49 +0200)
* lisp/emacs-lisp/cl-seq.el (cl-sort):
Don't use string mutation when sorting the characters in a string.
This avoids O(n^2) run time and makes it future-safe.

(cherry picked from commit 38091e43bee991920db81263fe56da444a123fae)

lisp/emacs-lisp/cl-seq.el

index 42f54603899c57c4556fc629257e10280e90e563..60d72d8657dd8d749dce732e92f27357f6cae424 100644 (file)
@@ -668,7 +668,10 @@ This is a destructive function; it reuses the storage of SEQ if possible.
 \nKeywords supported:  :key
 \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
   (if (nlistp cl-seq)
-      (cl-replace cl-seq (apply 'cl-sort (append cl-seq nil) cl-pred cl-keys))
+      (if (stringp cl-seq)
+          (concat (apply #'cl-sort (vconcat cl-seq) cl-pred cl-keys))
+        (cl-replace cl-seq
+                    (apply #'cl-sort (append cl-seq nil) cl-pred cl-keys)))
     (cl--parsing-keywords (:key) ()
       (if (memq cl-key '(nil identity))
          (sort cl-seq cl-pred)