X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=strings.lisp;h=08aec10ea8e92262ebcb92097ab5effab402f63c;hb=547cd5d282aa4531b5d05eddb981df90a03bf59b;hp=6c3eb448881c0d2c235cd5df3b0adffaeef33780;hpb=7e52d3cf184126838285c8d49a2ac6bf9accfa5a;p=kmrcl.git diff --git a/strings.lisp b/strings.lisp index 6c3eb44..08aec10 100644 --- a/strings.lisp +++ b/strings.lisp @@ -7,7 +7,7 @@ ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; -;;;; $Id: strings.lisp,v 1.39 2003/06/12 17:58:45 kevin Exp $ +;;;; $Id: strings.lisp,v 1.42 2003/06/15 13:49:42 kevin Exp $ ;;;; ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; @@ -318,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) @@ -351,3 +371,53 @@ Leading zeros are present." (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-string +hex-chars+)) + +(defun hexchar (n) + (declare (type (integer 0 15) n)) + (schar +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))))) +