Using an adjustable displaced array as a cursor on another array.

Scanning a string can be done using input functions withwith-input-from-string:

(with-input-from-string (stream "Hello World!  How do you do? ")
  (let ((c (make-string 4)))
    (loop
      :for pos = (read-sequence c stream)
      :while (= pos (length c))
      :do (print c)
      :finally (terpri))))

"Hell" 
"o Wo" 
"rld!" 
"  Ho" 
"w do" 
" you" 
" do?" 
nil

This can also be done using a displaced adjustable array, without copying the data that is read:

(defun make-vector-cursor (vector &key (size 0) (offset 0))
  (make-array size
              :element-type (array-element-type vector)
              :adjustable t
              :displaced-to vector
              :displaced-index-offset offset))

(defun advance-cursor (cursor &key (size (length cursor)))
  (multiple-value-bind (vector offset) (array-displacement cursor)
    (adjust-array cursor size
                  :element-type (array-element-type vector)
                  :displaced-to vector
                  :displaced-index-offset (+ offset (length cursor)))))
(let ((c (make-vector-cursor "Hello World!  How do you do? " :size 4)))
  (loop
    :do (print c)
    :while (ignore-errors (advance-cursor c))
    :finally (terpri)))

"Hell" 
"o Wo" 
"rld!" 
"  Ho" 
"w do" 
" you" 
" do?" 
NIL
(let ((c (make-vector-cursor "Hello World!  How do you do? " :size 1)))
  (loop
    :for size :from 1
    :do (print c)
    :while (ignore-errors (advance-cursor c :size size))
    :finally (terpri)))

"H" 
"e" 
"ll" 
"o W" 
"orld" 
"!  Ho" 
"w do y" 
"ou do? " 
nil

This post courtesy of Pascal J. Bourguignon