+ (declare (optimize (speed 3) (safety 1)
+ (debug 1) (space 2))
+ (fixnum a))
+ (string<= (coerce "e99mo7yAJ6oU4" 'base-string)
+ (coerce "aaABAAbaa" 'base-string)
+ :start1 a))
+ 9)
+ 9))
+
+;; String trimming.
+
+(flet ((make-test (string left right both)
+ (macrolet ((check (fun wanted)
+ `(let ((result (,fun " " string)))
+ (assert (equal result ,wanted))
+ (when (equal string ,wanted)
+ ;; Check that the original string is
+ ;; returned when no changes are needed. Not
+ ;; required by the spec, but a desireable
+ ;; feature for performance.
+ (assert (eql result string))))))
+ ;; Check the functional implementations
+ (locally
+ (declare (notinline string-left-trim string-right-trim
+ string-trim))
+ (check string-left-trim left)
+ (check string-right-trim right)
+ (check string-trim both))
+ ;; Check the transforms
+ (locally
+ (declare (type simple-string string))
+ (check string-left-trim left)
+ (check string-right-trim right)
+ (check string-trim both)))))
+ (make-test "x " "x " "x" "x")
+ (make-test " x" "x" " x" "x")
+ (make-test " x " "x " " x" "x")
+ (make-test " x x " "x x " " x x" "x x"))
+
+
+;;; Trimming should respect fill-pointers
+(let* ((s (make-array 9 :initial-contents "abcdabadd" :element-type
+ 'character :fill-pointer 7))
+ (s2 (string-left-trim "ab" s))
+ (s3 (string-right-trim "ab" s)))
+ (assert (equal "abcdaba" s))
+ (assert (equal "cdaba" s2))
+ (assert (equal "abcd" s3)))
+
+;;; Trimming should replace displacement offsets
+(let* ((etype 'base-char)
+ (s0
+ (make-array '(6) :initial-contents "abcaeb" :element-type etype))
+ (s
+ (make-array '(3) :element-type etype :displaced-to s0 :displaced-index-offset 1)))
+ (assert (equal "bc" (string-right-trim "ab" s)))
+ (assert (equal "bca" s))
+ (assert (equal "abcaeb" s0)))
+
+;;; Trimming non-simple-strings when there is nothing to do
+(let ((a (make-array 10 :element-type 'character :initial-contents "abcde00000" :fill-pointer 5)))
+ (assert (equal "abcde" (string-right-trim "Z" a))))
+
+;;; Trimming non-strings when there is nothing to do.
+(string-right-trim " " #\a)
+