diff options
| -rw-r--r-- | lisp/emacs-lisp/cl-extra.el | 4 | ||||
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 7 | ||||
| -rw-r--r-- | test/automated/seq-tests.el | 7 |
3 files changed, 15 insertions, 3 deletions
diff --git a/lisp/emacs-lisp/cl-extra.el b/lisp/emacs-lisp/cl-extra.el index 101864d3721..9742014db0c 100644 --- a/lisp/emacs-lisp/cl-extra.el +++ b/lisp/emacs-lisp/cl-extra.el | |||
| @@ -518,7 +518,9 @@ This sets the values of: `cl-most-positive-float', `cl-most-negative-float', | |||
| 518 | (defun cl-subseq (seq start &optional end) | 518 | (defun cl-subseq (seq start &optional end) |
| 519 | "Return the subsequence of SEQ from START to END. | 519 | "Return the subsequence of SEQ from START to END. |
| 520 | If END is omitted, it defaults to the length of the sequence. | 520 | If END is omitted, it defaults to the length of the sequence. |
| 521 | If START or END is negative, it counts from the end." | 521 | If START or END is negative, it counts from the end. |
| 522 | Signal an error if START or END are outside of the sequence (i.e | ||
| 523 | too large if positive or too small if negative)" | ||
| 522 | (declare (gv-setter | 524 | (declare (gv-setter |
| 523 | (lambda (new) | 525 | (lambda (new) |
| 524 | (macroexp-let2 nil new new | 526 | (macroexp-let2 nil new new |
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 9eed36eb68c..038b20e3b5e 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el | |||
| @@ -221,12 +221,17 @@ TESTFN is used to compare elements, or `equal' if TESTFN is nil." | |||
| 221 | (defun seq-subseq (seq start &optional end) | 221 | (defun seq-subseq (seq start &optional end) |
| 222 | "Return the subsequence of SEQ from START to END. | 222 | "Return the subsequence of SEQ from START to END. |
| 223 | If END is omitted, it defaults to the length of the sequence. | 223 | If END is omitted, it defaults to the length of the sequence. |
| 224 | If START or END is negative, it counts from the end." | 224 | If START or END is negative, it counts from the end. |
| 225 | |||
| 226 | Signal an error if START or END are outside of the sequence (i.e | ||
| 227 | too large if positive or too small if negative)" | ||
| 225 | (cond ((or (stringp seq) (vectorp seq)) (substring seq start end)) | 228 | (cond ((or (stringp seq) (vectorp seq)) (substring seq start end)) |
| 226 | ((listp seq) | 229 | ((listp seq) |
| 227 | (let (len (errtext (format "Bad bounding indices: %s, %s" start end))) | 230 | (let (len (errtext (format "Bad bounding indices: %s, %s" start end))) |
| 228 | (and end (< end 0) (setq end (+ end (setq len (seq-length seq))))) | 231 | (and end (< end 0) (setq end (+ end (setq len (seq-length seq))))) |
| 229 | (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq)))))) | 232 | (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq)))))) |
| 233 | (unless (>= start 0) | ||
| 234 | (error "%s" errtext)) | ||
| 230 | (when (> start 0) | 235 | (when (> start 0) |
| 231 | (setq seq (nthcdr (1- start) seq)) | 236 | (setq seq (nthcdr (1- start) seq)) |
| 232 | (or seq (error "%s" errtext)) | 237 | (or seq (error "%s" errtext)) |
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index 3643ce53cb0..74c0700759e 100644 --- a/test/automated/seq-tests.el +++ b/test/automated/seq-tests.el | |||
| @@ -187,7 +187,12 @@ Evaluate BODY for each created sequence. | |||
| 187 | (should-not (seq-subseq '(1 2 3) 3)) | 187 | (should-not (seq-subseq '(1 2 3) 3)) |
| 188 | (should (seq-subseq '(1 2 3) -3)) | 188 | (should (seq-subseq '(1 2 3) -3)) |
| 189 | (should-error (seq-subseq '(1 2 3) 1 4)) | 189 | (should-error (seq-subseq '(1 2 3) 1 4)) |
| 190 | (should (seq-subseq '(1 2 3) 1 3))) | 190 | (should (seq-subseq '(1 2 3) 1 3)) |
| 191 | (should-error (seq-subseq '() -1)) | ||
| 192 | (should-error (seq-subseq [] -1)) | ||
| 193 | (should-error (seq-subseq "" -1)) | ||
| 194 | (should-not (seq-subseq '() 0)) | ||
| 195 | (should-error(seq-subseq '() 0 -1))) | ||
| 191 | 196 | ||
| 192 | (ert-deftest test-seq-concatenate () | 197 | (ert-deftest test-seq-concatenate () |
| 193 | (with-test-sequences (seq '(2 4 6)) | 198 | (with-test-sequences (seq '(2 4 6)) |