Improve error signalling for seq-subseq.
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.
This commit is contained in:
parent
0aec2aaccd
commit
feadec307d
3 changed files with 15 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue