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.26 2003/05/11 21:51:44 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 ;;;; *************************************************************************
24 (defmacro string-append (outputstr &rest args)
25 `(setq ,outputstr (concatenate 'string ,outputstr ,@args)))
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))
31 (defun count-string-words (str)
32 (declare (simple-string str)
33 (optimize (speed 3) (safety 0)))
36 (declare (fixnum n-words))
37 (dotimes (i (length str))
38 (let ((ch (char str i)))
39 (declare (character ch))
40 (if (alphanumericp ch)
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)))
54 (when (char= char (schar string i)) (return i))))
56 (defun delimited-string-to-list (string &optional (separator #\space)
58 "split a string with delimiter"
59 (declare (optimize (speed 3) (safety 0) (space 0) (compilation-speed 0))
61 (type character separator))
62 (do* ((len (length string))
65 (end (position-char separator string pos len)
66 (position-char separator string pos len)))
69 (push (subseq string pos) output)
70 (when (or (not skip-terminal) (zerop len))
73 (declare (type fixnum pos len)
74 (type (or null fixnum) end))
75 (push (subseq string pos end) output)
79 (defun list-to-delimited-string (list &optional (separator #\space))
80 (format nil (format nil "~~{~~A~~^~A~~}" separator) list))
82 (defun string-invert (str)
83 "Invert case of a string"
84 (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
86 (let ((up nil) (down nil))
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)))
92 (if up (return-from skip str) (setf down t)))))
93 (if up (string-downcase str) (string-upcase str)))))
95 (defun add-sql-quotes (s)
96 (substitute-string-for-char s #\' "''"))
98 (defun escape-backslashes (s)
99 (substitute-string-for-char s #\\ "\\\\"))
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))))
105 (defun string-substitute (string substring replacement-string)
106 "String substitute by Larry Hunter. Obtained from Google"
107 (let ((substring-length (length substring))
111 (search substring string)
112 (search substring string :start2 last-end)))
114 (concatenate 'string new-string (subseq string last-end)))
118 (subseq string last-end next-start)
120 (setq last-end (+ next-start substring-length)))))
122 (defun string-trim-last-character (s)
123 "Return the string less the last character"
124 (let ((len (length s)))
126 (subseq s 0 (1- len))
129 (defun nstring-trim-last-character (s)
130 "Return the string less the last character"
131 (let ((len (length s)))
133 (nsubseq s 0 (1- len))
136 (defun string-hash (str &optional (bitmask 65535))
138 (declare (fixnum hash)
140 (dotimes (i (length str))
142 (setq hash (+ hash (char-code (char str i)))))
143 (logand hash bitmask)))
145 (defun string-not-null? (str)
146 (and str (not (zerop (length str)))))
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))))
154 (defun not-whitespace? (c)
155 (not (whitespace? c)))
157 (defun string-ws? (str)
158 "Return t if string is all whitespace"
160 (null (find-if #'not-whitespace? str))))
162 (defun replaced-string-length (str repl-alist)
163 (declare (string str))
164 (let* ((orig-len (length str))
166 (declare (fixnum orig-len new-len))
167 (dotimes (i orig-len)
169 (let* ((c (char str i))
170 (match (assoc c repl-alist :test #'char=)))
171 (declare (character c))
173 (incf new-len (1- (length (cdr match)))))))
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)))
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))
191 (let* ((subst (cdr match))
192 (len (length subst)))
193 (declare (fixnum len))
196 (setf (char new-string dpos) (char subst j))
199 (setf (char new-string dpos) c)
202 (defun escape-xml-string (string)
203 "Escape invalid XML characters"
204 (substitute-chars-strings
205 string '((#\& . "&") (#\> . ">") (#\< . "<") (#\" . """))))
208 (defun make-usb8-array (len)
209 (make-array len :adjustable nil
211 :element-type '(unsigned-byte 8)))
213 (defun usb8-array-to-string (vec)
214 (let* ((len (length vec))
215 (str (make-string len)))
216 (declare (fixnum len)
218 (optimize (speed 3)))
221 (setf (schar str i) (code-char (aref vec i))))
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)))
232 (setf (aref vec i) (char-code (schar str i))))
235 (defun concat-separated-strings (separator &rest lists)
236 (format nil (format nil "~~{~~A~~^~A~~}" separator) (append-sublists lists)))
238 (defun only-null-list-elements-p (lst)
239 (or (null lst) (every #'null lst)))
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))))
253 (write-string elem strm)
254 (unless (and last-elem last-list)
255 (write-string separator strm)))))