X-Git-Url: http://git.kpe.io/?p=kmrcl.git;a=blobdiff_plain;f=strings.lisp;h=bf10d058b85d29ba1da086f9cbc00195ce58540b;hp=f71c137c3cf8e2ce0a3780bca14ed9c3af693555;hb=6729a4cf6ca2361b36a8ada705e7325b62c2fe54;hpb=19187833b40be4cf8bfa5b159da42bae5d365888 diff --git a/strings.lisp b/strings.lisp index f71c137..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.40 2003/06/14 23:24:31 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 ;;;; @@ -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) @@ -364,3 +384,40 @@ for characters in a string" (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))))) +