r8183: docbook functions
[kmrcl.git] / strings.lisp
index fd2f37ca071e8dc6a7f8f7b1a849cbf849d19738..a97c37ba551f5d2e436da7954d69a138e248232d 100644 (file)
@@ -603,3 +603,23 @@ for characters in a string"
 
 
 
+(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)))))))