X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=strings.lisp;h=6c3eb448881c0d2c235cd5df3b0adffaeef33780;hb=7e52d3cf184126838285c8d49a2ac6bf9accfa5a;hp=a070731a2afff44463a1def6f1bae6619e2c4fc2;hpb=5dbd1fda3cf8f68c070cf3036dc6b1b536bc9f5a;p=kmrcl.git diff --git a/strings.lisp b/strings.lisp index a070731..6c3eb44 100644 --- a/strings.lisp +++ b/strings.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; -;;;; $Id: strings.lisp,v 1.36 2003/06/07 05:45:14 kevin Exp $ +;;;; $Id: strings.lisp,v 1.39 2003/06/12 17:58:45 kevin Exp $ ;;;; ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -168,23 +168,25 @@ (null (find-if #'not-whitespace? str)))) (defun replaced-string-length (str repl-alist) - (declare (string str)) - (let* ((orig-len (length str)) - (new-len orig-len)) - (declare (fixnum orig-len new-len)) - (dotimes (i orig-len) - (declare (fixnum i)) + (declare (simple-string str) + (fixnum orig-len new-len) + (optimize (speed 3) (safety 0) (space 0))) + (do* ((i 0 (1+ i)) + (orig-len (length str)) + (new-len orig-len)) + ((= i orig-len) new-len) + (declare (fixnum i orig-len new-len)) (let* ((c (char str i)) (match (assoc c repl-alist :test #'char=))) (declare (character c)) (when match - (incf new-len (1- (length (cdr match))))))) - new-len)) + (incf new-len (1- (length (cdr match)))))))) (defun substitute-chars-strings (str repl-alist) "Replace all instances of a chars with a string. repl-alist is an assoc list of characters and replacement strings." - (declare (simple-string str)) + (declare (simple-string str) + (optimize (speed 3) (safety 0) (space 0))) (do* ((orig-len (length str)) (new-string (make-string (replaced-string-length str repl-alist))) (spos 0 (1+ spos)) @@ -198,7 +200,8 @@ list of characters and replacement strings." (if match (let* ((subst (cdr match)) (len (length subst))) - (declare (fixnum len)) + (declare (fixnum len) + (simple-string subst)) (dotimes (j len) (declare (fixnum j)) (setf (char new-string dpos) (char subst j)) @@ -212,31 +215,31 @@ list of characters and replacement strings." (substitute-chars-strings string '((#\& . "&") (#\< . "<")))) (defun make-usb8-array (len) - (make-array len :adjustable nil - :fill-pointer nil - :element-type '(unsigned-byte 8))) + (make-array len :element-type '(unsigned-byte 8))) (defun usb8-array-to-string (vec) + (declare (type (simple-array (unsigned-byte 8) (*)) vec)) (let* ((len (length vec)) (str (make-string len))) (declare (fixnum len) (simple-string str) (optimize (speed 3))) - (dotimes (i len) + (do ((i 0 (1+ i))) + ((= i len) str) (declare (fixnum i)) - (setf (schar str i) (code-char (aref vec i)))) - str)) + (setf (schar str i) (code-char (aref vec i)))))) (defun string-to-usb8-array (str) + (declare (simple-string str)) (let* ((len (length str)) (vec (make-usb8-array len))) (declare (fixnum len) - (type (array fixnum (*)) vec) + (type (simple-array (unsigned-byte 8) (*)) vec) (optimize (speed 3))) - (dotimes (i len) + (do ((i 0 (1+ i))) + ((= i len) vec) (declare (fixnum i)) - (setf (aref vec i) (char-code (schar str i)))) - vec)) + (setf (aref vec i) (char-code (schar str i)))))) (defun concat-separated-strings (separator &rest lists) (format nil (concatenate 'string "~{~A~^" (string separator) "~}") @@ -331,3 +334,20 @@ Leading zeros are present." (nreverse results)) (declare (fixnum i j end)) (push (subseq str i j) results))) + +(defun string-starts-with (start str) + (and (>= (length str) (length start)) + (string-equal start str :end2 (length start)))) + +(defun count-string-char (s c) + "Return a count of the number of times a character appears in a string" + (declare (simple-string s) + (character c) + (optimize (speed 3) (safety 0))) + (do ((len (length s)) + (i 0 (1+ i)) + (count 0)) + ((= i len) count) + (declare (fixnum i len count)) + (when (char= (schar s i) c) + (incf count))))