1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: strings.lisp
6 ;;;; Purpose: Strings utility functions for KMRCL package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: strings.lisp,v 1.10 2003/04/28 23:51:59 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
21 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
25 (defmacro string-append (outputstr &rest args)
26 `(setq ,outputstr (concatenate 'string ,outputstr ,@args)))
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))
32 (defun count-string-words (str)
33 (declare (simple-string str)
34 (optimize (speed 3) (safety 0)))
37 (declare (fixnum n-words))
38 (dotimes (i (length str))
39 (let ((ch (char str i)))
40 (declare (character ch))
41 (if (alphanumericp ch)
49 (defun delimited-string-to-list (string &optional (separator #\space))
50 (excl:delimited-string-to-list string separator))
53 (defun delimited-string-to-list (sequence &optional (separator #\space))
54 "Split a string by a delimitor"
57 for end = (position separator sequence :start start)
58 collect (subseq sequence start end)
61 (setf start (1+ end))))
64 (defun list-to-delimited-string (list &optional (separator #\space))
65 (excl:list-to-delimited-string list separator))
68 (defun list-to-delimited-string (list &optional (separator #\space))
70 (let ((fmt (format nil "~~A~~{~A~~A~~}" separator)))
71 (format nil fmt (first list) (rest list)))
74 (defun string-invert (str)
75 "Invert case of a string"
76 (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
78 (let ((up nil) (down nil))
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)))))
85 (defun add-sql-quotes (s)
86 (substitute-string-for-char s #\' "''"))
88 (defun escape-backslashes (s)
89 (substitute-string-for-char s #\\ "\\\\"))
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))))
95 (defun string-substitute (string substring replacement-string)
96 "String substitute by Larry Hunter. Obtained from Google"
97 (let ((substring-length (length substring))
101 (search substring string)
102 (search substring string :start2 last-end)))
104 (concatenate 'string new-string (subseq string last-end)))
108 (subseq string last-end next-start)
110 (setq last-end (+ next-start substring-length)))))
112 (defun string-trim-last-character (s)
113 "Return the string less the last character"
114 (aif (plusp (length s))
118 (defun string-hash (str &optional (bitmask 65535))
120 (declare (fixnum hash)
122 (dotimes (i (length str))
124 (setq hash (+ hash (char-code (char str i)))))
125 (logand hash bitmask)))
127 (defun string-not-null? (str)
128 (and str (not (zerop (length str)))))
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)))
135 (defun not-whitespace? (c)
136 (not (whitespace? c)))
138 (defun string-ws? (str)
139 "Return t if string is all whitespace"
141 (null (find-if #'not-whitespace? str))))
143 (defun replaced-string-length (str repl-alist)
144 (declare (string str))
145 (let* ((orig-len (length str))
147 (declare (fixnum orig-len new-len))
148 (dotimes (i orig-len)
150 (let* ((c (char str i))
151 (match (assoc c repl-alist :test #'char=)))
152 (declare (character c))
154 (incf new-len (1- (length (cdr match)))))))
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)))
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))
172 (let* ((subst (cdr match))
173 (len (length subst)))
174 (declare (fixnum len))
177 (setf (char new-string dpos) (char subst j))
180 (setf (char new-string dpos) c)
183 (defun escape-xml-string (string)
184 "Escape invalid XML characters"
185 (string-replace-chars-strings
186 string '((#\& . "&") (#\> . ">") (#\< . "<"))))
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))
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
202 (the fixnum (1- repl-length)))))))))
203 (declare (fixnum old-length repl-length)
204 (simple-string new-string))
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))
215 (setf (schar new-string newpos) (schar string oldpos))
221 (defun make-usb8-array (len)
222 (make-array len :adjustable nil
224 :element-type '(unsigned-byte 8)))
226 (defun usb8-array-to-string (vec)
227 (let* ((len (length vec))
228 (str (make-string len)))
229 (declare (fixnum len)
231 (optimize (speed 3)))
234 (setf (schar str i) (code-char (aref vec i))))
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)))
245 (setf (aref vec i) (char-code (schar str i))))