r5097: *** empty log message ***
[kmrcl.git] / strings.lisp
index 5343f31cb0f488c3f01af628c57f40b004a42ecb..efce49807b405fad6545fc6e284f8997e4abc5c0 100644 (file)
@@ -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.38 2003/06/12 11:10:38 kevin Exp $
 ;;;;
 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;;
     (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 (simple-string str)
     (declare (fixnum orig-len new-len))
-    (dotimes (i orig-len)
-      (declare (fixnum i))
+          (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))))