r4982: Auto commit for Debian build
[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.32 2003/05/16 12:51:11 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
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 ;; From Larry Hunter with modifications
48 (defun position-char (char string start max)
49   (declare (optimize (speed 3) (safety 0) (space 0))
50            (fixnum start max) (simple-string string))
51   (do* ((i start (1+ i)))
52        ((= i max) nil)
53     (declare (fixnum i))
54     (when (char= char (schar string i)) (return i))))
55
56 (defun delimited-string-to-list (string &optional (separator #\space) 
57                                                   skip-terminal)
58   "split a string with delimiter"
59   (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
60            (type string string)
61            (type character separator))
62   (do* ((len (length string))
63         (output '())
64         (pos 0)
65         (end (position-char separator string pos len)
66              (position-char separator string pos len)))
67        ((null end)
68         (if (< pos len)
69             (push (subseq string pos) output)
70             (when (or (not skip-terminal) (zerop len))
71               (push "" output)))
72         (nreverse output))
73     (declare (type fixnum pos len)
74              (type (or null fixnum) end))
75     (push (subseq string pos end) output)
76     (setq pos (1+ end))))
77
78
79 (defun list-to-delimited-string (list &optional (separator #\space))
80   (format nil (format nil "~~{~~A~~^~A~~}" separator) list))
81
82 (defun string-invert (str)
83   "Invert case of a string"
84   (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
85            (simple-string str))
86   (let ((up nil) (down nil))
87     (block skip
88       (loop for char of-type character across str do
89             (cond ((upper-case-p char)
90                    (if down (return-from skip str) (setf up t)))
91                   ((lower-case-p char)
92                    (if up   (return-from skip str) (setf down t)))))
93       (if up (string-downcase str) (string-upcase str)))))
94
95 (defun add-sql-quotes (s)
96   (substitute-string-for-char s #\' "''"))
97
98 (defun escape-backslashes (s)
99   (substitute-string-for-char s #\\ "\\\\"))
100
101 (defun substitute-string-for-char (procstr match-char subst-str) 
102   "Substitutes a string for a single matching character of a string"
103   (substitute-chars-strings procstr (list (cons match-char subst-str))))
104
105 (defun string-substitute (string substring replacement-string)
106   "String substitute by Larry Hunter. Obtained from Google"
107   (let ((substring-length (length substring))
108         (last-end 0)
109         (new-string ""))
110     (do ((next-start
111           (search substring string)
112           (search substring string :start2 last-end)))
113         ((null next-start)
114          (concatenate 'string new-string (subseq string last-end)))
115       (setq new-string
116         (concatenate 'string
117           new-string
118           (subseq string last-end next-start)
119           replacement-string))
120       (setq last-end (+ next-start substring-length)))))
121
122 (defun string-trim-last-character (s)
123   "Return the string less the last character"
124   (let ((len (length s)))
125     (if (plusp len)
126         (subseq s 0 (1- len))
127         s)))
128
129 (defun nstring-trim-last-character (s)
130   "Return the string less the last character"
131   (let ((len (length s)))
132     (if (plusp len)
133         (nsubseq s 0 (1- len))
134         s)))
135
136 (defun string-hash (str &optional (bitmask 65535))
137   (let ((hash 0))
138     (declare (fixnum hash)
139              (simple-string str))
140     (dotimes (i (length str))
141       (declare (fixnum i))
142       (setq hash (+ hash (char-code (char str i)))))
143     (logand hash bitmask)))
144
145 (defun string-not-null? (str)
146   (and str (not (zerop (length str)))))
147   
148 (defun whitespace? (c) 
149   (declare (character c))
150   (locally (declare (optimize (speed 3) (safety 0)))
151     (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
152         (char= c #\Linefeed))))
153
154 (defun not-whitespace? (c)
155   (not (whitespace? c)))
156
157 (defun string-ws? (str)
158   "Return t if string is all whitespace"
159   (when (stringp str)
160     (null (find-if #'not-whitespace? str))))
161
162 (defun replaced-string-length (str repl-alist)
163   (declare (string str))
164   (let* ((orig-len (length str))
165          (new-len orig-len))
166     (declare (fixnum orig-len new-len))
167     (dotimes (i orig-len)
168       (declare (fixnum i))
169       (let* ((c (char str i))
170              (match (assoc c repl-alist :test #'char=)))
171         (declare (character c))
172         (when match
173           (incf new-len (1- (length (cdr match)))))))
174     new-len))
175
176 (defun substitute-chars-strings (str repl-alist)
177   "Replace all instances of a chars with a string. repl-alist is an assoc
178 list of characters and replacement strings."
179   (declare (simple-string str))
180   (do* ((orig-len (length str))
181         (new-string (make-string (replaced-string-length str repl-alist)))
182         (spos 0 (1+ spos))
183         (dpos 0))
184       ((>= spos orig-len)
185        new-string)
186     (declare (fixnum spos dpos) (simple-string new-string))
187     (let* ((c (char str spos))
188            (match (assoc c repl-alist :test #'char=)))
189       (declare (character c))
190       (if match
191           (let* ((subst (cdr match))
192                  (len (length subst)))
193             (declare (fixnum len))
194             (dotimes (j len)
195               (declare (fixnum j))
196               (setf (char new-string dpos) (char subst j))
197               (incf dpos)))
198         (progn
199           (setf (char new-string dpos) c)
200           (incf dpos))))))
201
202 (defun escape-xml-string (string)
203   "Escape invalid XML characters"
204   (substitute-chars-strings 
205    string '((#\& . "&amp;") (#\> . "&gt;") (#\< . "&lt;") (#\" . "&quot;"))))
206
207
208 (defun make-usb8-array (len)
209   (make-array len :adjustable nil
210               :fill-pointer nil
211               :element-type '(unsigned-byte 8)))
212
213 (defun usb8-array-to-string (vec)
214   (let* ((len (length vec))
215          (str (make-string len)))
216     (declare (fixnum len)
217              (simple-string str)
218              (optimize (speed 3)))
219     (dotimes (i len)
220       (declare (fixnum i))
221       (setf (schar str i) (code-char (aref vec i))))
222     str))
223
224 (defun string-to-usb8-array (str)
225   (let* ((len (length str))
226          (vec (make-usb8-array len)))
227     (declare (fixnum len)
228              (type (array fixnum (*)) vec)
229              (optimize (speed 3)))
230     (dotimes (i len)
231       (declare (fixnum i))
232       (setf (aref vec i) (char-code (schar str i))))
233     vec))
234
235 (defun concat-separated-strings (separator &rest lists)
236   (format nil (format nil "~~{~~A~~^~A~~}" separator) (append-sublists lists)))
237
238 (defun only-null-list-elements-p (lst)
239   (or (null lst) (every #'null lst)))
240
241 (defun print-separated-strings (strm separator &rest lists)
242   (declare (optimize (speed 3) (safety 0) (space 0) (debug 0)
243                      (compilation-speed 0)))
244   (do* ((rest-lists lists (cdr rest-lists))
245         (list (car rest-lists) (car rest-lists))
246         (last-list (only-null-list-elements-p (cdr rest-lists))
247                    (only-null-list-elements-p (cdr rest-lists))))
248        ((null rest-lists) strm)
249     (do* ((lst list (cdr lst))
250           (elem (car lst) (car lst))
251           (last-elem (null (cdr lst)) (null (cdr lst))))
252          ((null lst))
253       (write-string elem strm)
254       (unless (and last-elem last-list)
255         (write-string separator strm)))))
256
257 (defun prefixed-fixnum-string (num pchar len)
258   "Outputs a string of LEN digit with an optional initial character PCHAR.
259 Leading zeros are present."
260   (declare (optimize (speed 3) (safety 0) (space 0))
261            (type fixnum num len))
262   (when pchar
263     (incf len))
264   (do* ((zero-code (char-code #\0))
265         (result (make-string len :initial-element #\0))
266         (minus? (minusp num))
267         (val (if minus? (- 0 num) num) (floor (/ val 10)))
268         (pos (1- len) (1- pos))
269         (mod (mod val 10) (mod val 10)))
270       ((or (zerop val) (minusp pos))
271        (when pchar
272          (setf (schar result 0) pchar))
273        (when minus? (setf (schar result (if pchar 1 0)) #\-))
274        result)
275     (declare (fixnum val mod zero-code pos) (simple-string result))
276     (setf (schar result pos) (code-char (+ zero-code mod)))))
277
278 (defun integer-string (num len)
279   "Outputs a string of LEN digit with an optional initial character PCHAR.
280 Leading zeros are present."
281   (declare (optimize (speed 3) (safety 0) (space 0))
282            (type fixnum len) (type integer num))
283   (do* ((zero-code (char-code #\0))
284         (result (make-string len :initial-element #\0))
285         (minus? (minusp num))
286         (val (if minus? (- 0 num) num) (floor (/ val 10)))
287         (pos (1- len) (1- pos))
288         (mod (mod val 10) (mod val 10)))
289       ((or (zerop val) (minusp pos))
290        (when minus? (setf (schar result (if pchar 1 0)) #\-))
291        result)
292     (declare (fixnum mod zero-code pos) (simple-string result) (integer val))
293     (setf (schar result pos) (code-char (+ zero-code mod)))))