X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=strings.lisp;h=a97c37ba551f5d2e436da7954d69a138e248232d;hb=035b66e6fe51559e2db70691ddcae4ab641a4873;hp=87d325425c8e7902b9ea06542952e31836aa408e;hpb=b63abea0b53984f96a17b89011e9ca7e6b98602e;p=kmrcl.git diff --git a/strings.lisp b/strings.lisp index 87d3254..a97c37b 100644 --- a/strings.lisp +++ b/strings.lisp @@ -602,3 +602,24 @@ for characters in a string" (push (subseq string token-start token-end) tokens))))) + +(defun collapse-whitespace (s) + "Convert multiple whitespace characters to a single space character." + (declare (simple-string s) + (optimize (speed 3) (safety 0))) + (with-output-to-string (stream) + (do ((pos 0 (1+ pos)) + (in-white nil) + (len (length s))) + ((= pos len)) + (declare (fixnum pos len)) + (let ((c (schar s pos))) + (declare (character c)) + (cond + ((kl:is-char-whitespace c) + (unless in-white + (write-char #\space stream)) + (setq in-white t)) + (t + (setq in-white nil) + (write-char c stream)))))))