r4666: *** empty log message ***
[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: strings.lisp,v 1.10 2003/04/28 23:51:59 kevin Exp $
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 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 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
22
23 ;;; Strings
24
25 (defmacro string-append (outputstr &rest args)
26   `(setq ,outputstr (concatenate 'string ,outputstr ,@args)))
27
28 (defun list-to-string (lst)
29   "Converts a list to a string, doesn't include any delimiters between elements"
30   (format nil "~{~A~}" lst))
31
32 (defun count-string-words (str)
33   (declare (simple-string str)
34            (optimize (speed 3) (safety 0)))
35   (let ((n-words 0)
36         (in-word nil))
37     (declare (fixnum n-words))
38     (dotimes (i (length str))
39       (let ((ch (char str i)))
40         (declare (character ch))
41         (if (alphanumericp ch)
42             (unless in-word
43               (incf n-words)
44               (setq in-word t))
45           (setq in-word nil))))
46     n-words))
47
48 #+excl
49 (defun delimited-string-to-list (string &optional (separator #\space))
50   (excl:delimited-string-to-list string separator))
51
52 #-excl
53 (defun delimited-string-to-list (sequence &optional (separator #\space))
54   "Split a string by a delimitor"
55   (loop
56       with start = 0
57       for end = (position separator sequence :start start)
58       collect (subseq sequence start end)
59       until (null end)
60       do
61     (setf start (1+ end))))
62
63 #+excl
64 (defun list-to-delimited-string (list &optional (separator #\space))
65   (excl:list-to-delimited-string list separator))
66
67 #-excl
68 (defun list-to-delimited-string (list &optional (separator #\space))
69   (if (consp list)
70       (let ((fmt (format nil "~~A~~{~A~~A~~}" separator)))
71         (format nil fmt (first list) (rest list)))
72       ""))
73
74 (defun string-invert (str)
75   "Invert case of a string"
76   (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
77            (simple-string str))
78   (let ((up nil) (down nil))
79     (block skip
80       (loop for char of-type character across str do
81             (cond ((upper-case-p char) (if down (return-from skip str) (setf up t)))
82                   ((lower-case-p char) (if up   (return-from skip str) (setf down t)))))
83       (if up (string-downcase str) (string-upcase str)))))
84
85 (defun add-sql-quotes (s)
86   (substitute-string-for-char s #\' "''"))
87
88 (defun escape-backslashes (s)
89   (substitute-string-for-char s #\\ "\\\\"))
90
91 (defun substitute-string-for-char (procstr match-char subst-str) 
92   "Substitutes a string for a single matching character of a string"
93   (replace-chars-strings procstr (list (cons match-char subst-str))))
94
95 (defun string-substitute (string substring replacement-string)
96   "String substitute by Larry Hunter. Obtained from Google"
97   (let ((substring-length (length substring))
98         (last-end 0)
99         (new-string ""))
100     (do ((next-start
101           (search substring string)
102           (search substring string :start2 last-end)))
103         ((null next-start)
104          (concatenate 'string new-string (subseq string last-end)))
105       (setq new-string
106         (concatenate 'string
107           new-string
108           (subseq string last-end next-start)
109           replacement-string))
110       (setq last-end (+ next-start substring-length)))))
111
112 (defun string-trim-last-character (s)
113   "Return the string less the last character"
114   (aif (plusp (length s))
115        (subseq s 0 (1- it))
116        s))
117
118 (defun string-hash (str &optional (bitmask 65535))
119   (let ((hash 0))
120     (declare (fixnum hash)
121              (simple-string str))
122     (dotimes (i (length str))
123       (declare (fixnum i))
124       (setq hash (+ hash (char-code (char str i)))))
125     (logand hash bitmask)))
126
127 (defun string-not-null? (str)
128   (and str (not (zerop (length str)))))
129   
130 (defun whitespace? (c) 
131   (declare (character c))
132   (declare (optimize (speed 3) (safety 0)))
133   (or (char= c #\Space) (char= c #\Tab) (char= c #\Return) (char= c #\Linefeed)))
134
135 (defun not-whitespace? (c)
136   (not (whitespace? c)))
137
138 (defun string-ws? (str)
139   "Return t if string is all whitespace"
140   (when (stringp str)
141     (null (find-if #'not-whitespace? str))))
142
143 (defun replaced-string-length (str repl-alist)
144   (declare (string str))
145   (let* ((orig-len (length str))
146          (new-len orig-len))
147     (declare (fixnum orig-len new-len))
148     (dotimes (i orig-len)
149       (declare (fixnum i))
150       (let* ((c (char str i))
151              (match (assoc c repl-alist :test #'char=)))
152         (declare (character c))
153         (when match
154           (incf new-len (1- (length (cdr match)))))))
155     new-len))
156
157 (defun string-replace-chars-strings (str repl-alist)
158   "Replace all instances of a chars with a string. repl-alist is an assoc
159 list of characters and replacement strings."
160   (declare (simple-string str))
161   (do* ((orig-len (length str))
162         (new-string (make-string (replaced-string-length str repl-alist)))
163         (spos 0 (1+ spos))
164         (dpos 0))
165       ((>= spos orig-len)
166        new-string)
167     (declare (fixnum spos dpos) (simple-string new-string))
168     (let* ((c (char str spos))
169            (match (assoc c repl-alist :test #'char=)))
170       (declare (character c))
171       (if match
172           (let* ((subst (cdr match))
173                  (len (length subst)))
174             (declare (fixnum len))
175             (dotimes (j len)
176               (declare (fixnum j))
177               (setf (char new-string dpos) (char subst j))
178               (incf dpos)))
179         (progn
180           (setf (char new-string dpos) c)
181           (incf dpos))))))
182
183 (defun escape-xml-string (string)
184   "Escape invalid XML characters"
185   (string-replace-chars-strings 
186    string '((#\& . "&amp;") (#\> . "&gt;") (#\< . "&lt;"))))
187
188 (defun string-replace-char-string (string repl-char repl-str)
189   "Replace all occurances of repl-char with repl-str"
190  (declare (simple-string string))
191   (let ((count (count repl-char string)))
192     (declare (fixnum count))
193     (if (zerop count)
194         string
195         (locally (declare (optimize (speed 3) (safety 0)))
196           (let* ((old-length (length string))
197                  (repl-length (length repl-str))
198                  (new-string (make-string (the fixnum
199                                             (+ old-length
200                                                (the fixnum
201                                                  (* count
202                                                     (the fixnum (1- repl-length)))))))))
203             (declare (fixnum old-length repl-length)
204                      (simple-string new-string))
205             (let ((newpos 0))
206               (declare (fixnum newpos))
207               (dotimes (oldpos (length string))
208                 (declare (fixnum oldpos))
209                 (if (char= repl-char (schar string oldpos))
210                     (dotimes (repl-pos repl-length)
211                       (declare (fixnum repl-pos))
212                       (setf (schar new-string newpos) (schar repl-str repl-pos))
213                       (incf newpos))
214                     (progn
215                       (setf (schar new-string newpos) (schar string oldpos))
216                       (incf newpos)))))
217             new-string)))))
218
219     
220
221 (defun make-usb8-array (len)
222   (make-array len :adjustable nil
223               :fill-pointer nil
224               :element-type '(unsigned-byte 8)))
225
226 (defun usb8-array-to-string (vec)
227   (let* ((len (length vec))
228          (str (make-string len)))
229     (declare (fixnum len)
230              (simple-string str)
231              (optimize (speed 3)))
232     (dotimes (i len)
233       (declare (fixnum i))
234       (setf (schar str i) (code-char (aref vec i))))
235     str))
236
237 (defun string-to-usb8-array (str)
238   (let* ((len (length str))
239          (vec (make-usb8-array len)))
240     (declare (fixnum len)
241              (type (array fixnum (*)) vec)
242              (optimize (speed 3)))
243     (dotimes (i len)
244       (declare (fixnum i))
245       (setf (aref vec i) (char-code (schar str i))))
246     vec))
247