chiark / gitweb /
mdw-base.lisp: Make section heading for `with-places' more useful.
[lisp] / str.lisp
index 94a58a45dcebb948817c1577da6b336841bdae88..a943bae632af21b9047ac99f3cbd8d7598052b22 100644 (file)
--- a/str.lisp
+++ b/str.lisp
@@ -25,7 +25,8 @@
 
 (defpackage #:mdw.str
   (:use #:common-lisp #:mdw.base)
-  (:export #:join-strings #:str-next-word #:str-split-words))
+  (:export #:join-strings #:str-next-word #:str-split-words
+          #:str-beginsp #:str-endsp))
 (in-package #:mdw.str)
 
 (defun join-strings (del strs)
@@ -36,7 +37,7 @@ (defun join-strings (del strs)
   (with-output-to-string (s)
     (when strs
       (loop
-        (princ (stringify (pop strs)) s)
+       (princ (stringify (pop strs)) s)
        (unless strs
          (return))
        (princ del s)))))
@@ -134,4 +135,30 @@ (defun str-split-words (string &key quotedp start end max)
         (incf n)))
     (nreverse l)))
 
+(declaim (inline str-beginsp))
+(defun str-beginsp (string prefix &key (start1 0) end1 (start2 0) end2)
+  "Returns true if STRING (or the appropriate substring of it) begins with
+   PREFIX."
+  (setf-default end1 (length string)
+               end2 (length prefix))
+  (let ((strlen (- end1 start1))
+       (prelen (- end2 start2)))
+    (and (>= strlen prelen)
+        (string= string prefix
+                 :start1 start1 :end1 (+ start1 prelen)
+                 :start2 start2 :end2 end2))))
+
+(declaim (inline str-endsp))
+(defun str-endsp (string suffix &key (start1 0) end1 (start2 0) end2)
+  "Returns true if STRING (or the appropriate substring of it) ends with
+   SUFFIX."
+  (setf-default end1 (length string)
+               end2 (length suffix))
+  (let ((strlen (- end1 start1))
+       (suflen (- end2 start2)))
+    (and (>= strlen suflen)
+        (string= string suffix
+                 :start1 (- end1 suflen) :end1 end1
+                 :start2 start2 :end2 end2))))
+
 ;;;----- That's all, folks --------------------------------------------------