Automated commit for debian release 1.99-1
[kmrcl.git] / strings.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          strings.lisp
6 ;;;; Purpose:       Strings utility functions for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002-2006 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19
20 (in-package #:kmrcl)
21
22 ;;; Strings
23
24 (defmacro string-append (outputstr &rest args)
25   `(setq ,outputstr (concatenate 'string ,outputstr ,@args)))
26
27 (defun list-to-string (lst)
28   "Converts a list to a string, doesn't include any delimiters between elements"
29   (format nil "~{~A~}" lst))
30
31 (defun count-string-words (str)
32   (declare (simple-string str)
33            (optimize (speed 3) (safety 0) (space 0)))
34   (let ((n-words 0)
35         (in-word nil))
36     (declare (fixnum n-words))
37     (do* ((len (length str))
38           (i 0 (1+ i)))
39         ((= i len) n-words)
40       (declare (fixnum i))
41       (if (alphanumericp (schar str i))
42           (unless in-word
43             (incf n-words)
44             (setq in-word t))
45         (setq in-word nil)))))
46
47 (defun position-char (char string start max)
48   (declare (optimize (speed 3) (safety 0) (space 0))
49            (fixnum start max) (simple-string string))
50   (do* ((i start (1+ i)))
51        ((= i max) nil)
52     (declare (fixnum i))
53     (when (char= char (schar string i)) (return i))))
54
55 (defun position-not-char (char string start max)
56   (declare (optimize (speed 3) (safety 0) (space 0))
57            (fixnum start max) (simple-string string))
58   (do* ((i start (1+ i)))
59        ((= i max) nil)
60     (declare (fixnum i))
61     (when (char/= char (schar string i)) (return i))))
62
63 (defun delimited-string-to-list (string &optional (separator #\space)
64                                                   skip-terminal)
65   "split a string with delimiter"
66   (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
67            (type string string)
68            (type character separator))
69   (do* ((len (length string))
70         (output '())
71         (pos 0)
72         (end (position-char separator string pos len)
73              (position-char separator string pos len)))
74        ((null end)
75         (if (< pos len)
76             (push (subseq string pos) output)
77             (when (or (not skip-terminal) (zerop len))
78               (push "" output)))
79         (nreverse output))
80     (declare (type fixnum pos len)
81              (type (or null fixnum) end))
82     (push (subseq string pos end) output)
83     (setq pos (1+ end))))
84
85
86 (defun list-to-delimited-string (list &optional (separator " "))
87   (format nil (concatenate 'string "~{~A~^" (string separator) "~}") list))
88
89 (defun string-invert (str)
90   "Invert case of a string"
91   (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
92            (simple-string str))
93   (let ((up nil) (down nil))
94     (block skip
95       (loop for char of-type character across str do
96             (cond ((upper-case-p char)
97                    (if down (return-from skip str) (setf up t)))
98                   ((lower-case-p char)
99                    (if up   (return-from skip str) (setf down t)))))
100       (if up (string-downcase str) (string-upcase str)))))
101
102 (defun add-sql-quotes (s)
103   (substitute-string-for-char s #\' "''"))
104
105 (defun escape-backslashes (s)
106   (substitute-string-for-char s #\\ "\\\\"))
107
108 (defun substitute-string-for-char (procstr match-char subst-str)
109   "Substitutes a string for a single matching character of a string"
110   (substitute-chars-strings procstr (list (cons match-char subst-str))))
111
112 (defun string-substitute (string substring replacement-string)
113   "String substitute by Larry Hunter. Obtained from Google"
114   (let ((substring-length (length substring))
115         (last-end 0)
116         (new-string ""))
117     (do ((next-start
118           (search substring string)
119           (search substring string :start2 last-end)))
120         ((null next-start)
121          (concatenate 'string new-string (subseq string last-end)))
122       (setq new-string
123         (concatenate 'string
124           new-string
125           (subseq string last-end next-start)
126           replacement-string))
127       (setq last-end (+ next-start substring-length)))))
128
129 (defun string-trim-last-character (s)
130   "Return the string less the last character"
131   (let ((len (length s)))
132     (if (plusp len)
133         (subseq s 0 (1- len))
134         s)))
135
136 (defun nstring-trim-last-character (s)
137   "Return the string less the last character"
138   (let ((len (length s)))
139     (if (plusp len)
140         (nsubseq s 0 (1- len))
141         s)))
142
143 (defun string-hash (str &optional (bitmask 65535))
144   (let ((hash 0))
145     (declare (fixnum hash)
146              (simple-string str))
147     (dotimes (i (length str))
148       (declare (fixnum i))
149       (setq hash (+ hash (char-code (char str i)))))
150     (logand hash bitmask)))
151
152 (defun is-string-empty (str)
153   (zerop (length str)))
154
155 (defvar *whitespace-chars* '(#\space #\tab #\return #\linefeed
156                              #+allegro #\%space
157                              #+lispworks #\No-Break-Space))
158
159 (defun is-char-whitespace (c)
160   (declare (character c) (optimize (speed 3) (safety 0)))
161   (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
162       (char= c #\Linefeed)
163       #+allegro (char= c #\%space)
164       #+lispworks (char= c #\No-Break-Space)))
165
166 (defun is-string-whitespace (str)
167   "Return t if string is all whitespace"
168   (every #'is-char-whitespace str))
169
170 (defun string-right-trim-whitespace (str)
171   (string-right-trim *whitespace-chars* str))
172
173 (defun string-left-trim-whitespace (str)
174   (string-left-trim *whitespace-chars* str))
175
176 (defun string-trim-whitespace (str)
177   (string-trim *whitespace-chars* str))
178
179 (defun replaced-string-length (str repl-alist)
180   (declare (simple-string str)
181            (optimize (speed 3) (safety 0) (space 0)))
182     (do* ((i 0 (1+ i))
183           (orig-len (length str))
184           (new-len orig-len))
185          ((= i orig-len) new-len)
186       (declare (fixnum i orig-len new-len))
187       (let* ((c (char str i))
188              (match (assoc c repl-alist :test #'char=)))
189         (declare (character c))
190         (when match
191           (incf new-len (1- (length
192                              (the simple-string (cdr match)))))))))
193
194 (defun substitute-chars-strings (str repl-alist)
195   "Replace all instances of a chars with a string. repl-alist is an assoc
196 list of characters and replacement strings."
197   (declare (simple-string str)
198            (optimize (speed 3) (safety 0) (space 0)))
199   (do* ((orig-len (length str))
200         (new-string (make-string (replaced-string-length str repl-alist)))
201         (spos 0 (1+ spos))
202         (dpos 0))
203       ((>= spos orig-len)
204        new-string)
205     (declare (fixnum spos dpos) (simple-string new-string))
206     (let* ((c (char str spos))
207            (match (assoc c repl-alist :test #'char=)))
208       (declare (character c))
209       (if match
210           (let* ((subst (cdr match))
211                  (len (length subst)))
212             (declare (fixnum len)
213                      (simple-string subst))
214             (dotimes (j len)
215               (declare (fixnum j))
216               (setf (char new-string dpos) (char subst j))
217               (incf dpos)))
218         (progn
219           (setf (char new-string dpos) c)
220           (incf dpos))))))
221
222 (defun escape-xml-string (string)
223   "Escape invalid XML characters"
224   (substitute-chars-strings string '((#\& . "&amp;") (#\< . "&lt;"))))
225
226 (defun make-usb8-array (len)
227   (make-array len :element-type '(unsigned-byte 8)))
228
229 (defun usb8-array-to-string (vec &key (start 0) end)
230   (declare (type (simple-array (unsigned-byte 8) (*)) vec)
231            (fixnum start))
232   (unless end
233     (setq end (length vec)))
234   (let* ((len (- end start))
235          (str (make-string len)))
236     (declare (fixnum len)
237              (simple-string str)
238              (optimize (speed 3) (safety 0)))
239     (do ((i 0 (1+ i)))
240         ((= i len) str)
241       (declare (fixnum i))
242       (setf (schar str i) (code-char (aref vec (the fixnum (+ i start))))))))
243
244 (defun string-to-usb8-array (str)
245   (declare (simple-string str))
246   (let* ((len (length str))
247          (vec (make-usb8-array len)))
248     (declare (fixnum len)
249              (type (simple-array (unsigned-byte 8) (*)) vec)
250              (optimize (speed 3)))
251     (do ((i 0 (1+ i)))
252         ((= i len) vec)
253       (declare (fixnum i))
254       (setf (aref vec i) (char-code (schar str i))))))
255
256 (defun concat-separated-strings (separator &rest lists)
257   (format nil (concatenate 'string "~{~A~^" (string separator) "~}")
258           (append-sublists lists)))
259
260 (defun only-null-list-elements-p (lst)
261   (or (null lst) (every #'null lst)))
262
263 (defun print-separated-strings (strm separator &rest lists)
264   (declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
265                      (compilation-speed 0)))
266   (do* ((rest-lists lists (cdr rest-lists))
267         (list (car rest-lists) (car rest-lists))
268         (last-list (only-null-list-elements-p (cdr rest-lists))
269                    (only-null-list-elements-p (cdr rest-lists))))
270        ((null rest-lists) strm)
271     (do* ((lst list (cdr lst))
272           (elem (car lst) (car lst))
273           (last-elem (null (cdr lst)) (null (cdr lst))))
274          ((null lst))
275       (write-string elem strm)
276       (unless (and last-elem last-list)
277         (write-string separator strm)))))
278
279 (eval-when (:compile-toplevel :load-toplevel :execute)
280   (defmacro def-prefixed-number-string (fn-name type &optional doc)
281     `(defun ,fn-name (num pchar len)
282        ,@(when (stringp doc) (list doc))
283        (declare (optimize (speed 3) (safety 0) (space 0))
284                 (fixnum len)
285                 (,type num))
286        (when pchar
287          (incf len))
288        (do* ((zero-code (char-code #\0))
289            (result (make-string len :initial-element #\0))
290            (minus? (minusp num))
291            (val (if minus? (- num) num)
292                 (nth-value 0 (floor val 10)))
293            (pos (1- len) (1- pos))
294            (mod (mod val 10) (mod val 10)))
295          ((or (zerop val) (minusp pos))
296           (when pchar
297             (setf (schar result 0) pchar))
298           (when minus? (setf (schar result (if pchar 1 0)) #\-))
299           result)
300        (declare (,type val)
301                 (fixnum mod zero-code pos)
302                 (boolean minus?)
303                 (simple-string result))
304        (setf (schar result pos) (code-char (the fixnum (+ zero-code mod))))))))
305
306 (def-prefixed-number-string prefixed-fixnum-string fixnum
307  "Outputs a string of LEN digit with an optional initial character PCHAR.
308 Leading zeros are present. LEN must be a fixnum.")
309
310 (def-prefixed-number-string prefixed-integer-string integer
311  "Outputs a string of LEN digit with an optional initial character PCHAR.
312 Leading zeros are present. LEN must be an integer.")
313
314 (defun integer-string (num len)
315   "Outputs a string of LEN digit with an optional initial character PCHAR.
316 Leading zeros are present."
317   (declare (optimize (speed 3) (safety 0) (space 0))
318            (type fixnum len)
319            (type integer num))
320   (do* ((zero-code (char-code #\0))
321         (result (make-string len :initial-element #\0))
322         (minus? (minusp num))
323         (val (if minus? (- 0 num) num)
324              (nth-value 0 (floor val 10)))
325         (pos (1- len) (1- pos))
326         (mod (mod val 10) (mod val 10)))
327       ((or (zerop val) (minusp pos))
328        (when minus? (setf (schar result 0) #\-))
329        result)
330     (declare (fixnum mod zero-code pos) (simple-string result) (integer val))
331     (setf (schar result pos) (code-char (+ zero-code mod)))))
332
333 (defun fast-string-search (substr str substr-length startpos endpos)
334   "Optimized search for a substring in a simple-string"
335   (declare (simple-string substr str)
336            (fixnum substr-length startpos endpos)
337            (optimize (speed 3) (space 0) (safety 0)))
338   (do* ((pos startpos (1+ pos))
339         (lastpos (- endpos substr-length)))
340        ((> pos lastpos) nil)
341     (declare (fixnum pos lastpos))
342     (do ((i 0 (1+ i)))
343         ((= i substr-length)
344          (return-from fast-string-search pos))
345       (declare (fixnum i))
346       (unless (char= (schar str (+ i pos)) (schar substr i))
347         (return nil)))))
348
349 (defun string-delimited-string-to-list (str substr)
350   "splits a string delimited by substr into a list of strings"
351   (declare (simple-string str substr)
352            (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0)
353                      (debug 0)))
354   (do* ((substr-len (length substr))
355         (strlen (length str))
356         (output '())
357         (pos 0)
358         (end (fast-string-search substr str substr-len pos strlen)
359              (fast-string-search substr str substr-len pos strlen)))
360        ((null end)
361         (when (< pos strlen)
362           (push (subseq str pos) output))
363         (nreverse output))
364     (declare (fixnum strlen substr-len pos)
365              (type (or fixnum null) end))
366     (push (subseq str pos end) output)
367     (setq pos (+ end substr-len))))
368
369 (defun string-to-list-skip-delimiter (str &optional (delim #\space))
370   "Return a list of strings, delimited by spaces, skipping spaces."
371   (declare (simple-string str)
372            (optimize (speed 0) (space 0) (safety 0)))
373   (do* ((results '())
374         (end (length str))
375         (i (position-not-char delim str 0 end)
376            (position-not-char delim str j end))
377         (j (when i (position-char delim str i end))
378            (when i (position-char delim str i end))))
379        ((or (null i) (null j))
380         (when (and i (< i end))
381           (push (subseq str i end) results))
382         (nreverse results))
383     (declare (fixnum end)
384              (type (or fixnum null) i j))
385     (push (subseq str i j) results)))
386
387 (defun string-starts-with (start str)
388   (and (>= (length str) (length start))
389        (string-equal start str :end2 (length start))))
390
391 (defun count-string-char (s c)
392   "Return a count of the number of times a character appears in a string"
393   (declare (simple-string s)
394            (character c)
395            (optimize (speed 3) (safety 0)))
396   (do ((len (length s))
397        (i 0 (1+ i))
398        (count 0))
399       ((= i len) count)
400     (declare (fixnum i len count))
401     (when (char= (schar s i) c)
402       (incf count))))
403
404 (defun count-string-char-if (pred s)
405   "Return a count of the number of times a predicate is true
406 for characters in a string"
407   (declare (simple-string s)
408            (type (or function symbol) pred)
409            (optimize (speed 3) (safety 0) (space 0)))
410   (do ((len (length s))
411        (i 0 (1+ i))
412        (count 0))
413       ((= i len) count)
414     (declare (fixnum i len count))
415     (when (funcall pred (schar s i))
416       (incf count))))
417
418
419 ;;; URL Encoding
420
421 (defun non-alphanumericp (ch)
422   (not (alphanumericp ch)))
423
424 (defvar +hex-chars+ "0123456789ABCDEF")
425 (declaim (type simple-string +hex-chars+))
426
427 (defun hexchar (n)
428   (declare (type (integer 0 15) n))
429   (schar +hex-chars+ n))
430
431 (defconstant* +char-code-lower-a+ (char-code #\a))
432 (defconstant* +char-code-upper-a+ (char-code #\A))
433 (defconstant* +char-code-0+ (char-code #\0))
434 (declaim (type fixnum +char-code-0+ +char-code-upper-a+
435                +char-code-0))
436
437 (defun charhex (ch)
438   "convert hex character to decimal"
439   (let ((code (char-code (char-upcase ch))))
440     (declare (fixnum ch))
441     (if (>= code +char-code-upper-a+)
442         (+ 10 (- code +char-code-upper-a+))
443         (- code +char-code-0+))))
444
445 (defun binary-sequence-to-hex-string (seq)
446   (let ((list (etypecase seq
447                 (list seq)
448                 (sequence (map 'list #'identity seq)))))
449     (string-downcase (format nil "~{~2,'0X~}" list))))
450
451 (defun encode-uri-string (query)
452   "Escape non-alphanumeric characters for URI fields"
453   (declare (simple-string query)
454            (optimize (speed 3) (safety 0) (space 0)))
455   (do* ((count (count-string-char-if #'non-alphanumericp query))
456         (len (length query))
457         (new-len (+ len (* 2 count)))
458         (str (make-string new-len))
459         (spos 0 (1+ spos))
460         (dpos 0 (1+ dpos)))
461       ((= spos len) str)
462     (declare (fixnum count len new-len spos dpos)
463              (simple-string str))
464     (let ((ch (schar query spos)))
465       (if (non-alphanumericp ch)
466           (let ((c (char-code ch)))
467             (setf (schar str dpos) #\%)
468             (incf dpos)
469             (setf (schar str dpos) (hexchar (logand (ash c -4) 15)))
470             (incf dpos)
471             (setf (schar str dpos) (hexchar (logand c 15))))
472         (setf (schar str dpos) ch)))))
473
474 (defun decode-uri-string (query)
475   "Unescape non-alphanumeric characters for URI fields"
476   (declare (simple-string query)
477            (optimize (speed 3) (safety 0) (space 0)))
478   (do* ((count (count-string-char query #\%))
479         (len (length query))
480         (new-len (- len (* 2 count)))
481         (str (make-string new-len))
482         (spos 0 (1+ spos))
483         (dpos 0 (1+ dpos)))
484       ((= spos len) str)
485     (declare (fixnum count len new-len spos dpos)
486              (simple-string str))
487     (let ((ch (schar query spos)))
488       (if (char= #\% ch)
489           (let ((c1 (charhex (schar query (1+ spos))))
490                 (c2 (charhex (schar query (+ spos 2)))))
491             (declare (fixnum c1 c2))
492             (setf (schar str dpos)
493                   (code-char (logior c2 (ash c1 4))))
494             (incf spos 2))
495         (setf (schar str dpos) ch)))))
496
497
498 (defun uri-query-to-alist (query)
499   "Converts non-decoded URI query to an alist of settings"
500   (mapcar (lambda (set)
501             (let ((lst (kmrcl:delimited-string-to-list set #\=)))
502               (cons (first lst) (second lst))))
503           (kmrcl:delimited-string-to-list
504            (kmrcl:decode-uri-string query) #\&)))
505
506 (eval-when (:compile-toplevel :load-toplevel :execute)
507   (defvar +unambiguous-charset+
508     "abcdefghjkmnpqrstuvwxyz123456789ABCDEFGHJKLMNPQSRTUVWXYZ")
509   (defconstant* +unambiguous-length+ (length +unambiguous-charset+)))
510
511 (defun random-char (&optional (set :lower-alpha))
512   (ecase set
513     (:lower-alpha
514      (code-char (+ +char-code-lower-a+ (random 26))))
515     (:lower-alphanumeric
516      (let ((n (random 36)))
517        (if (>= n 26)
518            (code-char (+ +char-code-0+ (- n 26)))
519          (code-char (+ +char-code-lower-a+ n)))))
520     (:upper-alpha
521      (code-char (+ +char-code-upper-a+ (random 26))))
522     (:unambiguous
523      (schar +unambiguous-charset+ (random +unambiguous-length+)))
524     (:upper-lower-alpha
525      (let ((n (random 52)))
526        (if (>= n 26)
527            (code-char (+ +char-code-upper-a+ (- n 26)))
528          (code-char (+ +char-code-lower-a+ n)))))))
529
530
531 (defun random-string (&key (length 10) (set :lower-alpha))
532   "Returns a random lower-case string."
533   (declare (optimize (speed 3)))
534   (let ((s (make-string length)))
535     (declare (simple-string s))
536     (dotimes (i length s)
537       (setf (schar s i) (random-char set)))))
538
539
540 (defun first-char (s)
541   (declare (simple-string s))
542   (when (and (stringp s) (plusp (length s)))
543     (schar s 0)))
544
545 (defun last-char (s)
546   (declare (simple-string s))
547   (when (stringp s)
548     (let ((len (length s)))
549       (when (plusp len))
550       (schar s (1- len)))))
551
552 (defun ensure-string (v)
553   (typecase v
554     (string v)
555     (character (string v))
556     (symbol (symbol-name v))
557     (otherwise (write-to-string v))))
558
559 (defun string-right-trim-one-char (char str)
560   (declare (simple-string str))
561   (let* ((len (length str))
562          (last (1- len)))
563     (declare (fixnum len last))
564     (if (char= char (schar str last))
565         (subseq str 0 last)
566       str)))
567
568
569 (defun remove-char-string (char str)
570   (declare (character char)
571            (string str))
572   (do* ((len (length str))
573         (out (make-string len))
574         (pos 0 (1+ pos))
575         (opos 0))
576        ((= pos len) (subseq out 0 opos))
577     (declare (fixnum pos opos len)
578              (simple-string out))
579     (let ((c (char str pos)))
580       (declare (character c))
581       (when (char/= c char)
582         (setf (schar out opos) c)
583         (incf opos)))))
584
585
586 (defun string-strip-ending (str endings)
587   (if (stringp endings)
588       (setq endings (list endings)))
589   (let ((len (length str)))
590     (dolist (ending endings str)
591       (when (and (>= len (length ending))
592                  (string-equal ending
593                                (subseq str (- len
594                                               (length ending)))))
595         (return-from string-strip-ending
596           (subseq str 0 (- len (length ending))))))))
597
598
599 (defun string-maybe-shorten (str maxlen)
600   (string-elide str maxlen :end))
601
602 (defun string-elide (str maxlen position)
603   (declare (fixnum maxlen))
604   (let ((len (length str)))
605     (declare (fixnum len))
606     (cond
607      ((<= len maxlen)
608       str)
609      ((<= maxlen 3)
610       "...")
611      ((eq position :middle)
612       (multiple-value-bind (mid remain) (truncate maxlen 2)
613         (let ((end1 (- mid 1))
614               (start2 (- len (- mid 2) remain)))
615           (concatenate 'string (subseq str 0 end1) "..." (subseq str start2)))))
616      ((or (eq position :end) t)
617       (concatenate 'string (subseq str 0 (- maxlen 3)) "...")))))
618
619 (defun shrink-vector (str size)
620   #+allegro
621   (excl::.primcall 'sys::shrink-svector str size)
622   #+cmu
623   (lisp::shrink-vector str size)
624   #+lispworks
625   (system::shrink-vector$vector str size)
626   #+sbcl
627   (sb-kernel:shrink-vector str size)
628   #+scl
629   (common-lisp::shrink-vector str size)
630   #-(or allegro cmu lispworks sbcl scl)
631   (setq str (subseq str 0 size))
632   str)
633
634 (defun lex-string (string &key (whitespace '(#\space #\newline)))
635   "Separates a string at whitespace and returns a list of strings"
636   (flet ((is-sep (char) (member char whitespace :test #'char=)))
637     (let ((tokens nil))
638       (do* ((token-start
639              (position-if-not #'is-sep string)
640              (when token-end
641                (position-if-not #'is-sep string :start (1+ token-end))))
642             (token-end
643              (when token-start
644                (position-if #'is-sep string :start token-start))
645              (when token-start
646                (position-if #'is-sep string :start token-start))))
647            ((null token-start) (nreverse tokens))
648         (push (subseq string token-start token-end) tokens)))))
649
650 (defun split-alphanumeric-string (string)
651   "Separates a string at any non-alphanumeric chararacter"
652   (declare (simple-string string)
653            (optimize (speed 3) (safety 0)))
654   (flet ((is-sep (char)
655            (declare (character char))
656            (and (non-alphanumericp char)
657                 (not (char= #\_ char)))))
658     (let ((tokens nil))
659       (do* ((token-start
660              (position-if-not #'is-sep string)
661              (when token-end
662                (position-if-not #'is-sep string :start (1+ token-end))))
663             (token-end
664              (when token-start
665                (position-if #'is-sep string :start token-start))
666              (when token-start
667                (position-if #'is-sep string :start token-start))))
668            ((null token-start) (nreverse tokens))
669         (push (subseq string token-start token-end) tokens)))))
670
671
672 (defun trim-non-alphanumeric (word)
673   "Strip non-alphanumeric characters from beginning and end of a word."
674   (declare (simple-string word)
675            (optimize (speed 3) (safety 0) (space 0)))
676   (let* ((start 0)
677          (len (length word))
678          (end len))
679     (declare (fixnum start end len))
680     (do ((done nil))
681         ((or done (= start end)))
682       (if (alphanumericp (schar word start))
683           (setq done t)
684         (incf start)))
685     (when (> end start)
686       (do ((done nil))
687           ((or done (= start end)))
688         (if (alphanumericp (schar word (1- end)))
689             (setq done t)
690           (decf end))))
691     (if (or (plusp start) (/= len end))
692         (subseq word start end)
693       word)))
694
695
696 (defun collapse-whitespace (s)
697   "Convert multiple whitespace characters to a single space character."
698   (declare (simple-string s)
699            (optimize (speed 3) (safety 0)))
700   (with-output-to-string (stream)
701     (do ((pos 0 (1+ pos))
702          (in-white nil)
703          (len (length s)))
704         ((= pos len))
705       (declare (fixnum pos len))
706       (let ((c (schar s pos)))
707         (declare (character c))
708         (cond
709          ((kl:is-char-whitespace c)
710           (unless in-white
711             (write-char #\space stream))
712           (setq in-white t))
713          (t
714           (setq in-white nil)
715           (write-char c stream)))))))
716
717 (defun string->list (string)
718   (let ((eof (list nil)))
719     (with-input-from-string (stream string)
720       (do ((x (read stream nil eof) (read stream nil eof))
721            (l nil (cons x l)))
722           ((eq x eof) (nreverse l))))))