]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve error signalling for seq-subseq.
authorPhillip Lord <phillip.lord@newcastle.ac.uk>
Fri, 7 Aug 2015 21:12:59 +0000 (22:12 +0100)
committerPhillip Lord <phillip.lord@newcastle.ac.uk>
Fri, 7 Aug 2015 21:12:59 +0000 (22:12 +0100)
The existing behaviour for seq-subseq is to error when indexes are too
large, but to silently ignore numbers which are too negative for lists.
String and vector handling errors in both cases. This has been
regularlised.

Error signalling behaviour has been explicitly added to the docstring of
seq-subseq, and also to cl-subseq which largely defers to
seq-subseq (and is therefore also impacted by this change).

Tests have been added for these exceptional cases, as well as one non
exceptional base case.

lisp/emacs-lisp/cl-extra.el
lisp/emacs-lisp/seq.el
test/automated/seq-tests.el

index 101864d3721b60c724045c106d1f56620c8e3b86..9742014db0cd40d47f2d1112f5a93d7d9893c43e 100644 (file)
@@ -518,7 +518,9 @@ This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
 (defun cl-subseq (seq start &optional end)
   "Return the subsequence of SEQ from START to END.
 If END is omitted, it defaults to the length of the sequence.
-If START or END is negative, it counts from the end."
+If START or END is negative, it counts from the end.
+Signal an error if START or END are outside of the sequence (i.e
+too large if positive or too small if negative)"
   (declare (gv-setter
             (lambda (new)
               (macroexp-let2 nil new new
index 9eed36eb68c8dfdad320002a44cc39c4107a7677..038b20e3b5e4371ccd9cc8cd4ae067251d99d74b 100644 (file)
@@ -221,12 +221,17 @@ TESTFN is used to compare elements, or `equal' if TESTFN is nil."
 (defun seq-subseq (seq start &optional end)
   "Return the subsequence of SEQ from START to END.
 If END is omitted, it defaults to the length of the sequence.
-If START or END is negative, it counts from the end."
+If START or END is negative, it counts from the end.
+
+Signal an error if START or END are outside of the sequence (i.e
+too large if positive or too small if negative)"
   (cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
         ((listp seq)
          (let (len (errtext (format "Bad bounding indices: %s, %s" start end)))
            (and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
            (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq))))))
+           (unless (>= start 0)
+             (error "%s" errtext))
            (when (> start 0)
              (setq seq (nthcdr (1- start) seq))
              (or seq (error "%s" errtext))
index 3643ce53cb0ca39e2e933045160ae4f47e421629..74c0700759ecbc462ca55461002718d35d521c98 100644 (file)
@@ -187,7 +187,12 @@ Evaluate BODY for each created sequence.
   (should-not   (seq-subseq '(1 2 3) 3))
   (should       (seq-subseq '(1 2 3) -3))
   (should-error (seq-subseq '(1 2 3) 1 4))
-  (should       (seq-subseq '(1 2 3) 1 3)))
+  (should       (seq-subseq '(1 2 3) 1 3))
+  (should-error (seq-subseq '() -1))
+  (should-error (seq-subseq [] -1))
+  (should-error (seq-subseq "" -1))
+  (should-not (seq-subseq '() 0))
+  (should-error(seq-subseq '() 0 -1)))
 
 (ert-deftest test-seq-concatenate ()
   (with-test-sequences (seq '(2 4 6))