Skip to content
Snippets Groups Projects
Commit ea73fa22 authored by ram's avatar ram
Browse files

Changed ELT and %SETELT to check that the index is less than the LENGTH,

which might differ from the array dimension when the array is non-simple.
parent 786d0f31
No related branches found
No related tags found
No related merge requests found
...@@ -75,30 +75,39 @@ ...@@ -75,30 +75,39 @@
(defun elt (sequence index) (defun elt (sequence index)
"Returns the element of SEQUENCE specified by INDEX." "Returns the element of SEQUENCE specified by INDEX."
(if (listp sequence) (etypecase sequence
(if (< index 0) (list
(error "~S: index too small." index) (if (< index 0)
(do ((count index (1- count))) (error "~S: index too small." index)
((= count 0) (car sequence)) (do ((count index (1- count)))
(declare (fixnum count)) ((= count 0) (car sequence))
(if (atom sequence) (declare (fixnum count))
(error "~S: index too large." index) (if (atom sequence)
(setq sequence (cdr sequence))))) (error "~S: index too large." index)
(aref sequence index))) (setq sequence (cdr sequence))))))
(vector
(when (>= index (length sequence))
(error "~S: index too large." index))
(aref sequence index))))
(defun %setelt (sequence index newval) (defun %setelt (sequence index newval)
"Store NEWVAL as the component of SEQUENCE specified by INDEX." "Store NEWVAL as the component of SEQUENCE specified by INDEX."
(if (listp sequence) (etypecase sequence
(if (< index 0) (list
(error "~S: index too small." index) (if (< index 0)
(do ((count index (1- count)) (error "~S: index too small." index)
(seq sequence)) (do ((count index (1- count))
((= count 0) (rplaca seq newval) sequence) (seq sequence))
(declare (fixnum count)) ((= count 0) (rplaca seq newval) sequence)
(if (atom (cdr seq)) (declare (fixnum count))
(error "~S: index too large." index) (if (atom (cdr seq))
(setq seq (cdr seq))))) (error "~S: index too large." index)
(setf (aref sequence index) newval))) (setq seq (cdr seq))))))
(vector
(when (>= index (length sequence))
(error "~S: index too large." index))
(setf (aref sequence index) newval))))
(defun length (sequence) (defun length (sequence)
"Returns an integer that is the length of SEQUENCE." "Returns an integer that is the length of SEQUENCE."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment