Function: STARTS-WITH

Documentation

Test whether the first elements of SEQUENCE are the same (as per TEST) as the elements of PREFIX. If RETURN-SUFFIX is T the functions returns, as a second value, a displaced array pointing to the sequence after PREFIX.

Source

(defun starts-with (sequence prefix &key (test #'eql) (return-suffix nil))
  "Test whether the first elements of SEQUENCE are the same (as
  per TEST) as the elements of PREFIX.

If RETURN-SUFFIX is T the functions returns, as a second value, a
displaced array pointing to the sequence after PREFIX."
  (let ((length1 (length sequence))
        (length2 (length prefix)))
    (when (< length1 length2)
      (return-from starts-with (values nil nil)))
    (dotimes (index length2)
      (when (not (funcall test (elt sequence index) (elt prefix index)))
        (return-from starts-with (values nil nil))))
    ;; if we get here then we match
    (values t
            (if return-suffix
                (make-array (- (length sequence) (length prefix))
                            :element-type (array-element-type sequence)
                            :displaced-to sequence
                            :displaced-index-offset (length prefix)
                            :adjustable nil)
                nil))))
Source Context