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.7 2003/02/07 14:21:55 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 (let ((pos (position match-char procstr)))
96 (subseq procstr 0 pos) subst-str
97 (substitute-string-for-char (subseq procstr (1+ pos)) match-char subst-str))
100 (defun string-substitute (string substring replacement-string)
101 "String substitute by Larry Hunter. Obtained from Google"
102 (let ((substring-length (length substring))
106 (search substring string)
107 (search substring string :start2 last-end)))
109 (concatenate 'string new-string (subseq string last-end)))
113 (subseq string last-end next-start)
115 (setq last-end (+ next-start substring-length)))))
118 (defun string-trim-last-character (s)
119 "Return the string less the last character"
120 (subseq s 0 (1- (length s))))
122 (defun string-hash (str &optional (bitmask 65535))
124 (declare (fixnum hash)
126 (dotimes (i (length str))
128 (setq hash (+ hash (char-code (char str i)))))
129 (logand hash bitmask)))
131 (defun string-not-null? (str)
132 (and str (not (zerop (length str)))))
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)))
139 (defun not-whitespace? (c)
140 (not (whitespace? c)))
142 (defun string-ws? (str)
143 "Return t if string is all whitespace"
145 (null (find-if #'not-whitespace? str))))
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))
153 (declare (fixnum orig-len new-len))
154 (dotimes (i orign-len)
156 (let ((c (schar i str)))
160 (defun escape-xml-string (string)
161 "Escape invalid XML characters"
162 (string-replace-char-string
163 (string-replace-char-string string #\& "&")
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))
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
180 (the fixnum (1- repl-length)))))))))
181 (declare (fixnum old-length repl-length)
182 (simple-string new-string))
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))
193 (setf (schar new-string newpos) (schar string oldpos))
199 (defun make-usb8-array (len)
200 (make-array len :adjustable nil
202 :element-type '(unsigned-byte 8)))
204 (defun usb8-array-to-string (vec)
205 (let* ((len (length vec))
206 (str (make-string len)))
207 (declare (fixnum len)
209 (optimize (speed 3)))
212 (setf (schar str i) (code-char (aref vec i))))
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)))
223 (setf (aref vec i) (char-code (schar str i))))