r7819: more getopt improvements, tests
[kmrcl.git] / strings.lisp
index 03d9b66a82dca873377e855c24ce2c6af7fcfcae..1832ff2a5dfa58f94564ee87f7b59587527c7b6e 100644 (file)
 (defun is-string-empty (str)
   (zerop (length str)))
 
+(defvar *whitespace-chars* '(#\space #\tab #\return #\linefeed))
+
 (defun is-char-whitespace (c) 
   (declare (character c) (optimize (speed 3) (safety 0)))
   (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
   "Return t if string is all whitespace"
   (every #'is-char-whitespace str))
 
+(defun string-right-trim-whitespace (str)
+  (string-right-trim *whitespace-chars* str))
+
+(defun string-left-trim-whitespace (str)
+  (string-left-trim *whitespace-chars* str))
+
+(defun string-trim-whitespace (str)
+  (string-trim *whitespace-chars* str))
+
 (defun replaced-string-length (str repl-alist)
   (declare (simple-string str)
           (optimize (speed 3) (safety 0) (space 0)))
@@ -591,3 +602,20 @@ 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)))
+       (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)))))