X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=strings.lisp;h=6c3eb448881c0d2c235cd5df3b0adffaeef33780;hb=7e52d3cf184126838285c8d49a2ac6bf9accfa5a;hp=5343f31cb0f488c3f01af628c57f40b004a42ecb;hpb=a287b1a6782d53c22fc1807e44aae62fb7094bc5;p=kmrcl.git diff --git a/strings.lisp b/strings.lisp index 5343f31..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.37 2003/06/12 02:38:39 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 (simple-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)) @@ -336,3 +338,16 @@ Leading zeros are present." (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))))