X-Git-Url: http://git.kpe.io/?p=kmrcl.git;a=blobdiff_plain;f=strings.lisp;h=bf10d058b85d29ba1da086f9cbc00195ce58540b;hp=5343f31cb0f488c3f01af628c57f40b004a42ecb;hb=6729a4cf6ca2361b36a8ada705e7325b62c2fe54;hpb=a287b1a6782d53c22fc1807e44aae62fb7094bc5 diff --git a/strings.lisp b/strings.lisp index 5343f31..bf10d05 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.41 2003/06/15 07:48:30 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)) @@ -316,6 +318,26 @@ Leading zeros are present." (unless (char= (schar str (+ i pos)) (schar substr i)) (return nil))))) +(defun string-delimited-string-to-list (str substr) + "splits a string delimited by substr into a list of strings" + #+ignore + (declare (simple-string str substr) + (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))) + (do* ((substr-len (length substr)) + (strlen (length str)) + (output '()) + (pos 0) + (end (fast-string-search substr str substr-len pos strlen) + (fast-string-search substr str substr-len pos strlen))) + ((null end) + (when (< pos strlen) + (push (subseq str pos) output)) + (nreverse output)) + (declare (fixnum strlen substr-len pos) + (type (or fixnum null) end)) + (push (subseq str pos end) output) + (setq pos (+ end substr-len)))) + (defun string-to-list-skip-delimiter (str &optional (delim #\space)) "Return a list of strings, delimited by spaces, skipping spaces." (declare (simple-string str) @@ -336,3 +358,66 @@ 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)))) + +(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)))) + + +;;; URL Encoding + +(defun non-alphanumericp (ch) + (not (alphanumericp ch))) + +(defvar +hex-chars+ "0123456789ABCDEF") +(declaim (type (simple-array character 16) +hex-chars+)) + +(defun hexchar (n) + (declare (type (integer 0 15) n)) + (aref +hex-chars+ n)) + +(defun escape-uri-field (query) + "Escape non-alphanumeric characters for URI fields" + (declare (simple-string query) + (optimize (speed 3) (safety 0) (space 0))) + (do* ((count (count-string-char-if #'non-alphanumericp query)) + (len (length query)) + (new-len (+ len (* 2 count))) + (str (make-string new-len)) + (spos 0 (1+ spos)) + (dpos 0 (1+ dpos))) + ((= spos len) str) + (declare (fixnum count len new-len spos dpos) + (simple-string str)) + (let ((ch (schar query spos))) + (if (non-alphanumericp ch) + (let ((c (char-code ch))) + (setf (schar str dpos) #\%) + (incf dpos) + (setf (schar str dpos) (hexchar (logand (ash c -4) 15))) + (incf dpos) + (setf (schar str dpos) (hexchar (logand c 15)))) + (setf (schar str dpos) ch))))) +