From 45e04484357737ef225a4cff8ee6a8de68cb36c6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sun, 12 May 2024 11:22:23 +0200 Subject: [PATCH] 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. (cherry picked from commit 38091e43bee991920db81263fe56da444a123fae) --- lisp/emacs-lisp/cl-seq.el | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lisp/emacs-lisp/cl-seq.el b/lisp/emacs-lisp/cl-seq.el index 42f54603899..60d72d8657d 100644 --- a/lisp/emacs-lisp/cl-seq.el +++ b/lisp/emacs-lisp/cl-seq.el @@ -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) -- 2.39.5