From 5ad8f3e5709a3823371ea6aa039b5e7e50feca1f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sun, 17 Jul 2022 19:05:03 +0200 Subject: [PATCH] Use `take` where clearly safe to do so (bug#56521) * lisp/emacs-lisp/seq.el (seq-take): * lisp/auth-source.el (auth-source-secrets-search) (auth-source-plstore-search): * lisp/gnus/message.el (message-insert-formatted-citation-line): * lisp/net/dbus.el (dbus-unregister-object): * lisp/replace.el (occur-context-lines): * test/src/print-tests.el (print-circular): Replace hand-written loop or `butlast` call with `take` for clarity, performance and validation. We have the equivalence (take N LIST) = (butlast LIST (- (length LIST) N)). --- lisp/auth-source.el | 4 ++-- lisp/emacs-lisp/seq.el | 12 +++++++----- lisp/gnus/message.el | 3 +-- lisp/net/dbus.el | 4 +--- lisp/replace.el | 5 ++--- test/src/print-tests.el | 2 +- 6 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lisp/auth-source.el b/lisp/auth-source.el index 12da2c3d73d..a802ef856dc 100644 --- a/lisp/auth-source.el +++ b/lisp/auth-source.el @@ -1622,7 +1622,7 @@ authentication tokens: (not (string-match label item))) collect item))) ;; TODO: respect max in `secrets-search-items', not after the fact - (items (butlast items (- (length items) max))) + (items (take max items)) ;; convert the item name to a full plist (items (mapcar (lambda (item) (append @@ -2080,7 +2080,7 @@ entries for git.gnus.org: search-keys))) (items (plstore-find store search-spec)) (item-names (mapcar #'car items)) - (items (butlast items (- (length items) max))) + (items (take max items)) ;; convert the item to a full plist (items (mapcar (lambda (item) (let* ((plist (copy-tree (cdr item))) diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 36c17f4cd5e..0d9483aecb6 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -587,11 +587,13 @@ Signal an error if SEQUENCE is empty." (cl-defmethod seq-take ((list list) n) "Optimized implementation of `seq-take' for lists." - (let ((result '())) - (while (and list (> n 0)) - (setq n (1- n)) - (push (pop list) result)) - (nreverse result))) + (if (eval-when-compile (fboundp 'take)) + (take n list) + (let ((result '())) + (while (and list (> n 0)) + (setq n (1- n)) + (push (pop list) result)) + (nreverse result)))) (cl-defmethod seq-drop-while (pred (list list)) "Optimized implementation of `seq-drop-while' for lists." diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el index 7c2b24c6eee..00a27fb5f51 100644 --- a/lisp/gnus/message.el +++ b/lisp/gnus/message.el @@ -4180,8 +4180,7 @@ See `message-citation-line-format'." (setq fname (car names) lname (string-join (cdr names) " "))) ((> count 3) - (setq fname (string-join (butlast names (- count 2)) - " ") + (setq fname (string-join (take 2 names) " ") lname (string-join (nthcdr 2 names) " ")))) (when (string-match "\\(.*\\),\\'" fname) (let ((newlname (match-string 1 fname))) diff --git a/lisp/net/dbus.el b/lisp/net/dbus.el index d4d4ed54e90..6c978c5a5fe 100644 --- a/lisp/net/dbus.el +++ b/lisp/net/dbus.el @@ -941,9 +941,7 @@ association to the service from D-Bus." ;; Loop over the registered functions. (dolist (elt entry) - (when (equal - value - (butlast (cdr elt) (- (length (cdr elt)) (length value)))) + (when (equal value (take (length value) (cdr elt))) (setq ret t) ;; Compute new hash value. If it is empty, remove it from the ;; hash table. diff --git a/lisp/replace.el b/lisp/replace.el index 54ee64f64a5..f8cc784f7c6 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -2437,9 +2437,8 @@ See also `multi-occur'." (if (>= (+ prev-line (length prev-after-lines)) (- curr-line (length before-lines))) (setq prev-after-lines - (butlast prev-after-lines - (- (length prev-after-lines) - (- curr-line prev-line (length before-lines) 1)))) + (take (- curr-line prev-line (length before-lines) 1) + prev-after-lines)) ;; Separate non-overlapping context lines with a dashed line. (setq separator "-------\n"))) diff --git a/test/src/print-tests.el b/test/src/print-tests.el index 6ff7e997837..f818b4d4715 100644 --- a/test/src/print-tests.el +++ b/test/src/print-tests.el @@ -514,7 +514,7 @@ otherwise, use a different charset." (should (< lead (length numbers))) (should (<= lead loopback-index)) (should (< loopback-index (length numbers))) - (let ((lead-part (butlast numbers (- (length numbers) lead))) + (let ((lead-part (take lead numbers)) (loop-part (nthcdr lead numbers))) ;; The lead part must match exactly. (should (equal lead-part (number-sequence 1 lead))) -- 2.30.2