From 3cb9d624f65d603d49bc48bb78af24145615a63a Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Tue, 10 Jun 2008 12:07:52 +0100 Subject: [PATCH] str: Handy functions for testing prefixes/suffixes. --- str.lisp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/str.lisp b/str.lisp index 94a58a4..5cf05aa 100644 --- 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) @@ -134,4 +135,30 @@ (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 -------------------------------------------------- -- 2.11.0