X-Git-Url: http://git.kpe.io/?p=kmrcl.git;a=blobdiff_plain;f=strings.lisp;h=9dbe1babdccd7950172033dc0d5c6d9614ff4921;hp=4ef18f0134e586755ab1cb9177e82ff53ca11b10;hb=e55fe0dc654bb7cd01e978a7080b6087788f173f;hpb=8513c6f273613b0ceb2b48c07da6a0b1803c32a5 diff --git a/strings.lisp b/strings.lisp index 4ef18f0..9dbe1ba 100644 --- a/strings.lisp +++ b/strings.lisp @@ -413,9 +413,9 @@ for characters in a string" (declare (type (integer 0 15) n)) (schar +hex-chars+ n)) -(defconstant +char-code-lower-a+ (char-code #\a)) -(defconstant +char-code-upper-a+ (char-code #\A)) -(defconstant +char-code-0+ (char-code #\0)) +(defconst +char-code-lower-a+ (char-code #\a)) +(defconst +char-code-upper-a+ (char-code #\A)) +(defconst +char-code-0+ (char-code #\0)) (declaim (type fixnum +char-code-0+ +char-code-upper-a+ +char-code-0)) @@ -604,7 +604,12 @@ for characters in a string" (defun split-alphanumeric-string (string) "Separates a string at any non-alphanumeric chararacter" - (flet ((is-sep (char) (non-alphanumericp char))) + (declare (simple-string string) + (optimize (speed 3) (safety 0))) + (flet ((is-sep (char) + (declare (character char)) + (and (non-alphanumericp char) + (not (char= #\_ char))))) (let ((tokens nil)) (do* ((token-start (position-if-not #'is-sep string) @@ -619,6 +624,30 @@ for characters in a string" (push (subseq string token-start token-end) tokens))))) +(defun trim-non-alphanumeric (word) + "Strip non-alphanumeric characters from beginning and end of a word." + (declare (simple-string word) + (optimize (speed 3) (safety 0) (space 0))) + (let* ((start 0) + (len (length word)) + (end len)) + (declare (fixnum start end len)) + (do ((done nil)) + ((or done (= start end))) + (if (alphanumericp (schar word start)) + (setq done t) + (incf start))) + (when (> end start) + (do ((done nil)) + ((or done (= start end))) + (if (alphanumericp (schar word (1- end))) + (setq done t) + (decf end)))) + (if (or (plusp start) (/= len end)) + (subseq word start end) + word))) + + (defun collapse-whitespace (s) "Convert multiple whitespace characters to a single space character."