;;;; 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.40 2003/06/14 23:24:31 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 (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))
(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))
(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) "~}")
(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))))
+
+(defun count-string-char-if (pred s)
+ "Return a count of the number of times a predicate is true
+for characters in a string"
+ (declare (simple-string s)
+ (optimize (speed 3) (safety 0) (space 0)))
+ (do ((len (length s))
+ (i 0 (1+ i))
+ (count 0))
+ ((= i len) count)
+ (declare (fixnum i len count))
+ (when (funcall pred (schar s i))
+ (incf count))))