r3982: 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.7 2003/02/07 14:21:55 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   (let ((pos (position match-char procstr)))
94     (if pos
95         (concatenate 'string
96           (subseq procstr 0 pos) subst-str
97           (substitute-string-for-char (subseq procstr (1+ pos)) match-char subst-str))
98       procstr)))
99
100 (defun string-substitute (string substring replacement-string)
101   "String substitute by Larry Hunter. Obtained from Google"
102   (let ((substring-length (length substring))
103         (last-end 0)
104         (new-string ""))
105     (do ((next-start
106           (search substring string)
107           (search substring string :start2 last-end)))
108         ((null next-start)
109          (concatenate 'string new-string (subseq string last-end)))
110       (setq new-string
111         (concatenate 'string
112           new-string
113           (subseq string last-end next-start)
114           replacement-string))
115       (setq last-end (+ next-start substring-length)))))
116
117
118 (defun string-trim-last-character (s)
119 "Return the string less the last character"
120   (subseq s 0 (1- (length s))))
121
122 (defun string-hash (str &optional (bitmask 65535))
123   (let ((hash 0))
124     (declare (fixnum hash)
125              (simple-string str))
126     (dotimes (i (length str))
127       (declare (fixnum i))
128       (setq hash (+ hash (char-code (char str i)))))
129     (logand hash bitmask)))
130
131 (defun string-not-null? (str)
132   (and str (not (zerop (length str)))))
133   
134 (defun whitespace? (c) 
135   (declare (character c))
136   (declare (optimize (speed 3) (safety 0)))
137   (or (char= c #\Space) (char= c #\Tab) (char= c #\Return) (char= c #\Linefeed)))
138
139 (defun not-whitespace? (c)
140   (not (whitespace? c)))
141
142 (defun string-ws? (str)
143   "Return t if string is all whitespace"
144   (when (stringp str)
145     (null (find-if #'not-whitespace? str))))
146
147 #+ignore
148 (defun string-replace-chars-strings (str repl-alist)
149   "Replace all instances of a chars with a string. repl-alist is an assoc
150 list of characters and replacement strings."
151   (let* ((orig-len (length str))
152          (new-len orign-len))
153     (declare (fixnum orig-len new-len))
154     (dotimes (i orign-len)
155       (declare (fixnum i))
156       (let ((c (schar i str)))
157         )))
158   str)
159
160 (defun escape-xml-string (string)
161   "Escape invalid XML characters"
162   (string-replace-char-string
163    (string-replace-char-string string #\& "&")
164    #\< "&lt;"))
165
166 (defun string-replace-char-string (string repl-char repl-str)
167   "Replace all occurances of repl-char with repl-str"
168  (declare (simple-string string))
169   (let ((count (count repl-char string)))
170     (declare (fixnum count))
171     (if (zerop count)
172         string
173         (locally (declare (optimize (speed 3) (safety 0)))
174           (let* ((old-length (length string))
175                  (repl-length (length repl-str))
176                  (new-string (make-string (the fixnum
177                                             (+ old-length
178                                                (the fixnum
179                                                  (* count
180                                                     (the fixnum (1- repl-length)))))))))
181             (declare (fixnum old-length repl-length)
182                      (simple-string new-string))
183             (let ((newpos 0))
184               (declare (fixnum newpos))
185               (dotimes (oldpos (length string))
186                 (declare (fixnum oldpos))
187                 (if (char= repl-char (schar string oldpos))
188                     (dotimes (repl-pos repl-length)
189                       (declare (fixnum repl-pos))
190                       (setf (schar new-string newpos) (schar repl-str repl-pos))
191                       (incf newpos))
192                     (progn
193                       (setf (schar new-string newpos) (schar string oldpos))
194                       (incf newpos)))))
195             new-string)))))
196
197     
198
199 (defun make-usb8-array (len)
200   (make-array len :adjustable nil
201               :fill-pointer nil
202               :element-type '(unsigned-byte 8)))
203
204 (defun usb8-array-to-string (vec)
205   (let* ((len (length vec))
206          (str (make-string len)))
207     (declare (fixnum len)
208              (simple-string str)
209              (optimize (speed 3)))
210     (dotimes (i len)
211       (declare (fixnum i))
212       (setf (schar str i) (code-char (aref vec i))))
213     str))
214
215 (defun string-to-usb8-array (str)
216   (let* ((len (length str))
217          (vec (make-usb8-array len)))
218     (declare (fixnum len)
219              (type (array fixnum (*)) vec)
220              (optimize (speed 3)))
221     (dotimes (i len)
222       (declare (fixnum i))
223       (setf (aref vec i) (char-code (schar str i))))
224     vec))
225