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