r4669: 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.12 2003/04/29 00:43:56 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
22 ;;; Strings
23
24 (defmacro string-append (outputstr &rest args)
25   `(setq ,outputstr (concatenate 'string ,outputstr ,@args)))
26
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))
30
31 (defun count-string-words (str)
32   (declare (simple-string str)
33            (optimize (speed 3) (safety 0)))
34   (let ((n-words 0)
35         (in-word nil))
36     (declare (fixnum n-words))
37     (dotimes (i (length str))
38       (let ((ch (char str i)))
39         (declare (character ch))
40         (if (alphanumericp ch)
41             (unless in-word
42               (incf n-words)
43               (setq in-word t))
44           (setq in-word nil))))
45     n-words))
46
47 #+excl
48 (defun delimited-string-to-list (string &optional (separator #\space))
49   (excl:delimited-string-to-list string separator))
50
51 #-excl
52 (defun delimited-string-to-list (sequence &optional (separator #\space))
53   "Split a string by a delimitor"
54   (loop
55       with start = 0
56       for end = (position separator sequence :start start)
57       collect (subseq sequence start end)
58       until (null end)
59       do
60     (setf start (1+ end))))
61
62 #+excl
63 (defun list-to-delimited-string (list &optional (separator #\space))
64   (excl:list-to-delimited-string list separator))
65
66 #-excl
67 (defun list-to-delimited-string (list &optional (separator #\space))
68   (if (consp list)
69       (let ((fmt (format nil "~~A~~{~A~~A~~}" separator)))
70         (format nil fmt (first list) (rest list)))
71       ""))
72
73 (defun string-invert (str)
74   "Invert case of a string"
75   (declare (optimize (speed 3) (compilation-speed 0) (debug 0) (safety 0))
76            (simple-string str))
77   (let ((up nil) (down nil))
78     (block skip
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)))))
83
84 (defun add-sql-quotes (s)
85   (substitute-string-for-char s #\' "''"))
86
87 (defun escape-backslashes (s)
88   (substitute-string-for-char s #\\ "\\\\"))
89
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))))
93
94 (defun string-substitute (string substring replacement-string)
95   "String substitute by Larry Hunter. Obtained from Google"
96   (let ((substring-length (length substring))
97         (last-end 0)
98         (new-string ""))
99     (do ((next-start
100           (search substring string)
101           (search substring string :start2 last-end)))
102         ((null next-start)
103          (concatenate 'string new-string (subseq string last-end)))
104       (setq new-string
105         (concatenate 'string
106           new-string
107           (subseq string last-end next-start)
108           replacement-string))
109       (setq last-end (+ next-start substring-length)))))
110
111 (defun string-trim-last-character (s)
112   "Return the string less the last character"
113   (let ((len (length s)))
114     (if (plusp len)
115         (subseq s 0 (1- len))
116         s))
117
118 (defun string-hash (str &optional (bitmask 65535))
119   (let ((hash 0))
120     (declare (fixnum hash)
121              (simple-string str))
122     (dotimes (i (length str))
123       (declare (fixnum i))
124       (setq hash (+ hash (char-code (char str i)))))
125     (logand hash bitmask)))
126
127 (defun string-not-null? (str)
128   (and str (not (zerop (length str)))))
129   
130 (defun whitespace? (c) 
131   (declare (character c))
132   (locally (declare (optimize (speed 3) (safety 0)))
133     (or (char= c #\Space) (char= c #\Tab) (char= c #\Return)
134         (char= c #\Linefeed))))
135
136 (defun not-whitespace? (c)
137   (not (whitespace? c)))
138
139 (defun string-ws? (str)
140   "Return t if string is all whitespace"
141   (when (stringp str)
142     (null (find-if #'not-whitespace? str))))
143
144 (defun replaced-string-length (str repl-alist)
145   (declare (string str))
146   (let* ((orig-len (length str))
147          (new-len orig-len))
148     (declare (fixnum orig-len new-len))
149     (dotimes (i orig-len)
150       (declare (fixnum i))
151       (let* ((c (char str i))
152              (match (assoc c repl-alist :test #'char=)))
153         (declare (character c))
154         (when match
155           (incf new-len (1- (length (cdr match)))))))
156     new-len))
157
158 (defun substitute-chars-strings (str repl-alist)
159   "Replace all instances of a chars with a string. repl-alist is an assoc
160 list of characters and replacement strings."
161   (declare (simple-string str))
162   (do* ((orig-len (length str))
163         (new-string (make-string (replaced-string-length str repl-alist)))
164         (spos 0 (1+ spos))
165         (dpos 0))
166       ((>= spos orig-len)
167        new-string)
168     (declare (fixnum spos dpos) (simple-string new-string))
169     (let* ((c (char str spos))
170            (match (assoc c repl-alist :test #'char=)))
171       (declare (character c))
172       (if match
173           (let* ((subst (cdr match))
174                  (len (length subst)))
175             (declare (fixnum len))
176             (dotimes (j len)
177               (declare (fixnum j))
178               (setf (char new-string dpos) (char subst j))
179               (incf dpos)))
180         (progn
181           (setf (char new-string dpos) c)
182           (incf dpos))))))
183
184 (defun escape-xml-string (string)
185   "Escape invalid XML characters"
186   (substitute-chars-strings 
187    string '((#\& . "&amp;") (#\> . "&gt;") (#\< . "&lt;"))))
188
189
190 (defun make-usb8-array (len)
191   (make-array len :adjustable nil
192               :fill-pointer nil
193               :element-type '(unsigned-byte 8)))
194
195 (defun usb8-array-to-string (vec)
196   (let* ((len (length vec))
197          (str (make-string len)))
198     (declare (fixnum len)
199              (simple-string str)
200              (optimize (speed 3)))
201     (dotimes (i len)
202       (declare (fixnum i))
203       (setf (schar str i) (code-char (aref vec i))))
204     str))
205
206 (defun string-to-usb8-array (str)
207   (let* ((len (length str))
208          (vec (make-usb8-array len)))
209     (declare (fixnum len)
210              (type (array fixnum (*)) vec)
211              (optimize (speed 3)))
212     (dotimes (i len)
213       (declare (fixnum i))
214       (setf (aref vec i) (char-code (schar str i))))
215     vec))
216