r8580: add tests/improvements to ensure-keyword-*
[kmrcl.git] / strings.lisp
index 1832ff2a5dfa58f94564ee87f7b59587527c7b6e..8eb08f74e9faa7ac1b243ee78988db6b2a239444 100644 (file)
 (defun is-string-empty (str)
   (zerop (length str)))
 
-(defvar *whitespace-chars* '(#\space #\tab #\return #\linefeed))
+(defvar *whitespace-chars* '(#\space #\tab #\return #\linefeed
+                            #+allegro #\%space
+                            #+lispworks #\No-Break-Space))
 
 (defun is-char-whitespace (c) 
   (declare (character c) (optimize (speed 3) (safety 0)))
   (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
-      (char= c #\Linefeed)))
+      (char= c #\Linefeed)
+      #+allegro (char= c #\%space)
+      #+lispworks (char= c #\No-Break-Space)))
 
 (defun is-string-whitespace (str)
   "Return t if string is all whitespace"
@@ -423,7 +427,7 @@ for characters in a string"
        (+ 10 (- code +char-code-upper-a+))
        (- code +char-code-0+))))
 
-(defun uriencode-string (query)
+(defun encode-uri-string (query)
   "Escape non-alphanumeric characters for URI fields"
   (declare (simple-string query)
           (optimize (speed 3) (safety 0) (space 0)))
@@ -446,7 +450,7 @@ for characters in a string"
            (setf (schar str dpos) (hexchar (logand c 15))))
        (setf (schar str dpos) ch)))))
 
-(defun uridecode-string (query)
+(defun decode-uri-string (query)
   "Unescape non-alphanumeric characters for URI fields"
   (declare (simple-string query)
           (optimize (speed 3) (safety 0) (space 0)))
@@ -602,20 +606,31 @@ for characters in a string"
         (push (subseq string token-start token-end) tokens)))))
 
 
-(defun match-unique-abbreviation (abbr strings)
-  "Returns position of ABBR in STRINGS. ABBR may be a unique abbreviation.
-Returns NIL if no match found."
-  (let ((len (length abbr))
-       (matches nil))
-    (dotimes (i (length strings))
-      (let* ((s (nth i strings))
-            (l (length s)))
+
+(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
-         ((= len l)
-          (when (string= abbr s)
-            (push (cons s i) matches)))
-         ((< len l)
-          (when (string= abbr (subseq s 0 len))
-            (push (cons s i) matches))))))
-    (when (= 1 (length matches))
-      (cdr (first matches)))))
+        ((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)))))))
+
+(defun string->list (string)
+  (let ((eof (list nil)))
+    (with-input-from-string (stream string)
+      (do ((x (read stream nil eof) (read stream nil eof))
+           (l nil (cons x l)))
+          ((eq x eof) (nreverse l))))))