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.14 2003/04/29 00:49:09 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)
48 (defun delimited-string-to-list (string &optional (separator #\space))
49 (excl:delimited-string-to-list string separator))
52 (defun delimited-string-to-list (sequence &optional (separator #\space))
53 "Split a string by a delimitor"
56 for end = (position separator sequence :start start)
57 collect (subseq sequence start end)
60 (setf start (1+ end))))
63 (defun list-to-delimited-string (list &optional (separator #\space))
64 (excl:list-to-delimited-string list separator))
67 (defun list-to-delimited-string (list &optional (separator #\space))
69 (let ((fmt (format nil "~~A~~{~A~~A~~}" separator)))
70 (format nil fmt (first list) (rest list)))
73 (defun string-invert (str)
74 "Invert case of a string"
75 (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
77 (let ((up nil) (down nil))
79 (loop for char of-type character across str do
80 (cond ((upper-case-p char) (if down (return-from skip str) (setf up t)))
81 ((lower-case-p char) (if up (return-from skip str) (setf down t)))))
82 (if up (string-downcase str) (string-upcase str)))))
84 (defun add-sql-quotes (s)
85 (substitute-string-for-char s #\' "''"))
87 (defun escape-backslashes (s)
88 (substitute-string-for-char s #\\ "\\\\"))
90 (defun substitute-string-for-char (procstr match-char subst-str)
91 "Substitutes a string for a single matching character of a string"
92 (substitute-chars-strings procstr (list (cons match-char subst-str))))
94 (defun string-substitute (string substring replacement-string)
95 "String substitute by Larry Hunter. Obtained from Google"
96 (let ((substring-length (length substring))
100 (search substring string)
101 (search substring string :start2 last-end)))
103 (concatenate 'string new-string (subseq string last-end)))
107 (subseq string last-end next-start)
109 (setq last-end (+ next-start substring-length)))))
111 (defun string-trim-last-character (s)
112 "Return the string less the last character"
113 (let ((len (length s)))
115 (subseq s 0 (1- len))
118 (defun nstring-trim-last-character (s)
119 "Return the string less the last character"
120 (let ((len (length s)))
122 (nsubseq s 0 (1- len))
125 (defun string-hash (str &optional (bitmask 65535))
127 (declare (fixnum hash)
129 (dotimes (i (length str))
131 (setq hash (+ hash (char-code (char str i)))))
132 (logand hash bitmask)))
134 (defun string-not-null? (str)
135 (and str (not (zerop (length str)))))
137 (defun whitespace? (c)
138 (declare (character c))
139 (locally (declare (optimize (speed 3) (safety 0)))
140 (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
141 (char= c #\Linefeed))))
143 (defun not-whitespace? (c)
144 (not (whitespace? c)))
146 (defun string-ws? (str)
147 "Return t if string is all whitespace"
149 (null (find-if #'not-whitespace? str))))
151 (defun replaced-string-length (str repl-alist)
152 (declare (string str))
153 (let* ((orig-len (length str))
155 (declare (fixnum orig-len new-len))
156 (dotimes (i orig-len)
158 (let* ((c (char str i))
159 (match (assoc c repl-alist :test #'char=)))
160 (declare (character c))
162 (incf new-len (1- (length (cdr match)))))))
165 (defun substitute-chars-strings (str repl-alist)
166 "Replace all instances of a chars with a string. repl-alist is an assoc
167 list of characters and replacement strings."
168 (declare (simple-string str))
169 (do* ((orig-len (length str))
170 (new-string (make-string (replaced-string-length str repl-alist)))
175 (declare (fixnum spos dpos) (simple-string new-string))
176 (let* ((c (char str spos))
177 (match (assoc c repl-alist :test #'char=)))
178 (declare (character c))
180 (let* ((subst (cdr match))
181 (len (length subst)))
182 (declare (fixnum len))
185 (setf (char new-string dpos) (char subst j))
188 (setf (char new-string dpos) c)
191 (defun escape-xml-string (string)
192 "Escape invalid XML characters"
193 (substitute-chars-strings
194 string '((#\& . "&") (#\> . ">") (#\< . "<"))))
197 (defun make-usb8-array (len)
198 (make-array len :adjustable nil
200 :element-type '(unsigned-byte 8)))
202 (defun usb8-array-to-string (vec)
203 (let* ((len (length vec))
204 (str (make-string len)))
205 (declare (fixnum len)
207 (optimize (speed 3)))
210 (setf (schar str i) (code-char (aref vec i))))
213 (defun string-to-usb8-array (str)
214 (let* ((len (length str))
215 (vec (make-usb8-array len)))
216 (declare (fixnum len)
217 (type (array fixnum (*)) vec)
218 (optimize (speed 3)))
221 (setf (aref vec i) (char-code (schar str i))))