1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-21 06:55:39 +00:00

Safer and faster string sorting

* 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.
This commit is contained in:
Mattias Engdegård 2024-05-12 11:22:23 +02:00
parent 86d196c71d
commit 38091e43be

View 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)