/[cmucl]/src/code/string.lisp
ViewVC logotype

Contents of /src/code/string.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Sun Jun 17 19:12:34 2001 UTC (12 years, 10 months ago) by pw
Branch: MAIN
CVS Tags: release-18e-base, LINKAGE_TABLE, PRE_LINKAGE_TABLE, release-18e-pre2, cold-pcl-base, UNICODE-BASE, release-18e, release-18e-pre1
Branch point for: UNICODE-BRANCH, release-18e-branch, cold-pcl
Changes since 1.10: +7 -6 lines
From eric Marsden:

Fix some error types to be ANSI compliant.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.3 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 pw 1.11 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/string.lisp,v 1.11 2001/06/17 19:12:34 pw Exp $")
9 ram 1.3 ;;;
10 ram 1.1 ;;; **********************************************************************
11     ;;;
12 pw 1.11 ;;; Functions to implement strings for CMU Common Lisp
13 ram 1.1 ;;; Written by David Dill
14 ram 1.4 ;;; Rewritten by Skef Wholey, Bill Chiles and Rob MacLachlan.
15 ram 1.1 ;;;
16     ;;; ****************************************************************
17     ;;;
18 ram 1.4 (in-package "LISP")
19 ram 1.1 (export '(char schar string
20     string= string-equal string< string> string<= string>= string/=
21     string-lessp string-greaterp string-not-lessp string-not-greaterp
22     string-not-equal
23     make-string
24     string-trim string-left-trim string-right-trim
25     string-upcase
26     string-downcase string-capitalize nstring-upcase nstring-downcase
27     nstring-capitalize))
28    
29    
30     (defun string (X)
31     "Coerces X into a string. If X is a string, X is returned. If X is a
32     symbol, X's pname is returned. If X is a character then a one element
33     string containing that character is returned. If X cannot be coerced
34     into a string, an error occurs."
35     (cond ((stringp x) x)
36     ((symbolp x) (symbol-name x))
37     ((characterp x)
38     (let ((res (make-string 1)))
39     (setf (schar res 0) x) res))
40     (t
41 pw 1.11 (error 'simple-type-error
42     :datum x
43     :expected-type '(or string symbol character)
44     :format-control "~S cannot be coerced to a string."
45     :format-arguments (list x)))))
46 ram 1.1
47     ;;; With-One-String is used to set up some string hacking things. The keywords
48     ;;; are parsed, and the string is hacked into a simple-string.
49    
50     (eval-when (compile)
51    
52     (defmacro with-one-string (string start end cum-offset &rest forms)
53 ram 1.4 `(let ((,string (if (stringp ,string) ,string (string ,string))))
54 pw 1.10 ;; Optimizer may prove STRING is one.
55     (declare (optimize (ext:inhibit-warnings 3)))
56 ram 1.4 (with-array-data ((,string ,string :offset-var ,cum-offset)
57     (,start ,start)
58     (,end (or ,end (length (the vector ,string)))))
59     ,@forms)))
60 ram 1.1
61     )
62    
63     ;;; With-String is like With-One-String, but doesn't parse keywords.
64    
65     (eval-when (compile)
66    
67     (defmacro with-string (string &rest forms)
68 ram 1.4 `(let ((,string (if (stringp ,string) ,string (string ,string))))
69     (with-array-data ((,string ,string)
70     (start)
71     (end (length (the vector ,string))))
72     ,@forms)))
73 ram 1.1
74     )
75    
76     ;;; With-Two-Strings is used to set up string comparison operations. The
77     ;;; keywords are parsed, and the strings are hacked into simple-strings.
78    
79     (eval-when (compile)
80    
81     (defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
82     start2 end2 &rest forms)
83 ram 1.4 `(let ((,string1 (if (stringp ,string1) ,string1 (string ,string1)))
84     (,string2 (if (stringp ,string2) ,string2 (string ,string2))))
85     (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
86     (,start1 ,start1)
87     (,end1 (or ,end1 (length (the vector ,string1)))))
88     (with-array-data ((,string2 ,string2)
89     (,start2 ,start2)
90     (,end2 (or ,end2 (length (the vector ,string2)))))
91     ,@forms))))
92 ram 1.1
93     )
94 wlott 1.2
95 ram 1.1
96     (defun char (string index)
97     "Given a string and a non-negative integer index less than the length of
98     the string, returns the character object representing the character at
99     that position in the string."
100 ram 1.4 (declare (optimize (safety 1)))
101 ram 1.1 (char string index))
102    
103     (defun %charset (string index new-el)
104 ram 1.4 (declare (optimize (safety 1)))
105 ram 1.1 (setf (char string index) new-el))
106    
107     (defun schar (string index)
108     "SCHAR returns the character object at an indexed position in a string
109     just as CHAR does, except the string must be a simple-string."
110 ram 1.4 (declare (optimize (safety 1)))
111 ram 1.1 (schar string index))
112    
113     (defun %scharset (string index new-el)
114 ram 1.4 (declare (optimize (safety 1)))
115 ram 1.1 (setf (schar string index) new-el))
116    
117     (defun string=* (string1 string2 start1 end1 start2 end2)
118 ram 1.4 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
119     (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
120 ram 1.1
121    
122     (defun string/=* (string1 string2 start1 end1 start2 end2)
123 ram 1.4 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
124     (let ((comparison (%sp-string-compare string1 start1 end1
125     string2 start2 end2)))
126     (if comparison (- (the fixnum comparison) offset1)))))
127 ram 1.1
128     (eval-when (compile eval)
129    
130     ;;; Lessp is true if the desired expansion is for string<* or string<=*.
131     ;;; Equalp is true if the desired expansion is for string<=* or string>=*.
132     (defmacro string<>=*-body (lessp equalp)
133     (let ((offset1 (gensym)))
134 ram 1.4 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
135     (let ((index (%sp-string-compare string1 start1 end1
136     string2 start2 end2)))
137     (if index
138 ram 1.8 (cond ((= (the fixnum index) (the fixnum end1))
139     ,(if lessp
140     `(- (the fixnum index) ,offset1)
141     `nil))
142     ((= (+ (the fixnum index) (- start2 start1))
143     (the fixnum end2))
144     ,(if lessp
145     `nil
146     `(- (the fixnum index) ,offset1)))
147 ram 1.4 ((,(if lessp 'char< 'char>)
148     (schar string1 index)
149     (schar string2 (+ (the fixnum index) (- start2 start1))))
150     (- (the fixnum index) ,offset1))
151     (t nil))
152     ,(if equalp `(- (the fixnum end1) ,offset1) 'nil))))))
153 ram 1.1 ) ; eval-when
154    
155     (defun string<* (string1 string2 start1 end1 start2 end2)
156     (declare (fixnum start1 start2))
157     (string<>=*-body t nil))
158    
159     (defun string>* (string1 string2 start1 end1 start2 end2)
160     (declare (fixnum start1 start2))
161     (string<>=*-body nil nil))
162    
163     (defun string<=* (string1 string2 start1 end1 start2 end2)
164     (declare (fixnum start1 start2))
165     (string<>=*-body t t))
166    
167     (defun string>=* (string1 string2 start1 end1 start2 end2)
168     (declare (fixnum start1 start2))
169     (string<>=*-body nil t))
170    
171    
172    
173     (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
174     "Given two strings, if the first string is lexicographically less than
175     the second string, returns the longest common prefix (using char=)
176     of the two strings. Otherwise, returns ()."
177     (string<* string1 string2 start1 end1 start2 end2))
178    
179     (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
180     "Given two strings, if the first string is lexicographically greater than
181     the second string, returns the longest common prefix (using char=)
182     of the two strings. Otherwise, returns ()."
183     (string>* string1 string2 start1 end1 start2 end2))
184    
185    
186     (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
187     "Given two strings, if the first string is lexicographically less than
188     or equal to the second string, returns the longest common prefix
189     (using char=) of the two strings. Otherwise, returns ()."
190     (string<=* string1 string2 start1 end1 start2 end2))
191    
192     (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
193     "Given two strings, if the first string is lexicographically greater
194     than or equal to the second string, returns the longest common prefix
195     (using char=) of the two strings. Otherwise, returns ()."
196     (string>=* string1 string2 start1 end1 start2 end2))
197    
198     (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
199     "Given two strings (string1 and string2), and optional integers start1,
200     start2, end1 and end2, compares characters in string1 to characters in
201     string2 (using char=)."
202     (string=* string1 string2 start1 end1 start2 end2))
203    
204     (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
205     "Given two strings, if the first string is not lexicographically equal
206     to the second string, returns the longest common prefix (using char=)
207     of the two strings. Otherwise, returns ()."
208     (string/=* string1 string2 start1 end1 start2 end2))
209    
210    
211     (eval-when (compile eval)
212    
213     ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
214     ;;; STRING-EQUAL and STRING-NOT-EQUAL.
215     (defmacro string-not-equal-loop (end end-value
216     &optional (abort-value nil abortp))
217     (declare (fixnum end))
218     (let ((end-test (if (= end 1)
219     `(= index1 (the fixnum end1))
220     `(= index2 (the fixnum end2)))))
221     `(do ((index1 start1 (1+ index1))
222     (index2 start2 (1+ index2)))
223     (,(if abortp
224     end-test
225     `(or ,end-test
226     (not (char-equal (schar string1 index1)
227     (schar string2 index2)))))
228     ,end-value)
229     (declare (fixnum index1 index2))
230     ,@(if abortp
231     `((if (not (char-equal (schar string1 index1)
232     (schar string2 index2)))
233     (return ,abort-value)))))))
234    
235     ) ; eval-when
236    
237     (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
238     "Given two strings (string1 and string2), and optional integers start1,
239     start2, end1 and end2, compares characters in string1 to characters in
240     string2 (using char-equal)."
241     (declare (fixnum start1 start2))
242 ram 1.4 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
243     (let ((slen1 (- (the fixnum end1) start1))
244     (slen2 (- (the fixnum end2) start2)))
245     (declare (fixnum slen1 slen2))
246     (if (or (minusp slen1) (minusp slen2))
247     ;;prevent endless looping later.
248     (error "Improper bounds for string comparison."))
249     (if (= slen1 slen2)
250     ;;return () immediately if lengths aren't equal.
251     (string-not-equal-loop 1 t nil)))))
252 ram 1.1
253     (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
254     "Given two strings, if the first string is not lexicographically equal
255     to the second string, returns the longest common prefix (using char-equal)
256     of the two strings. Otherwise, returns ()."
257 ram 1.4 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
258     (let ((slen1 (- end1 start1))
259     (slen2 (- end2 start2)))
260     (declare (fixnum slen1 slen2))
261     (if (or (minusp slen1) (minusp slen2))
262     ;;prevent endless looping later.
263     (error "Improper bounds for string comparison."))
264     (cond ((or (minusp slen1) (or (minusp slen2)))
265     (error "Improper substring for comparison."))
266     ((= slen1 slen2)
267     (string-not-equal-loop 1 nil (- index1 offset1)))
268     ((< slen1 slen2)
269     (string-not-equal-loop 1 (- index1 offset1)))
270     (t
271     (string-not-equal-loop 2 (- index1 offset1)))))))
272 ram 1.1
273    
274    
275     (eval-when (compile eval)
276    
277     ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
278     ;;; and string2 and a test on the current characters from string1 and string2
279     ;;; for the following macro.
280     (defun string-less-greater-equal-tests (lessp equalp)
281     (if lessp
282     (if equalp
283     ;; STRING-NOT-GREATERP
284     (values '<= `(not (char-greaterp char1 char2)))
285     ;; STRING-LESSP
286     (values '< `(char-lessp char1 char2)))
287     (if equalp
288     ;; STRING-NOT-LESSP
289     (values '>= `(not (char-lessp char1 char2)))
290     ;; STRING-GREATERP
291     (values '> `(char-greaterp char1 char2)))))
292    
293     (defmacro string-less-greater-equal (lessp equalp)
294     (multiple-value-bind (length-test character-test)
295     (string-less-greater-equal-tests lessp equalp)
296 ram 1.4 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
297     (let ((slen1 (- (the fixnum end1) start1))
298     (slen2 (- (the fixnum end2) start2)))
299     (declare (fixnum slen1 slen2))
300     (if (or (minusp slen1) (minusp slen2))
301     ;;prevent endless looping later.
302     (error "Improper bounds for string comparison."))
303     (do ((index1 start1 (1+ index1))
304     (index2 start2 (1+ index2))
305     (char1)
306     (char2))
307     ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
308     (if (,length-test slen1 slen2) (- index1 offset1)))
309     (declare (fixnum index1 index2))
310     (setq char1 (schar string1 index1))
311     (setq char2 (schar string2 index2))
312     (if (not (char-equal char1 char2))
313     (if ,character-test
314     (return (- index1 offset1))
315     (return ()))))))))
316 ram 1.1
317     ) ; eval-when
318    
319     (defun string-lessp* (string1 string2 start1 end1 start2 end2)
320     (declare (fixnum start1 start2))
321     (string-less-greater-equal t nil))
322    
323     (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
324     (declare (fixnum start1 start2))
325     (string-less-greater-equal nil nil))
326    
327     (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
328     (declare (fixnum start1 start2))
329     (string-less-greater-equal nil t))
330    
331     (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
332     (declare (fixnum start1 start2))
333     (string-less-greater-equal t t))
334    
335     (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
336     "Given two strings, if the first string is lexicographically less than
337     the second string, returns the longest common prefix (using char-equal)
338     of the two strings. Otherwise, returns ()."
339     (string-lessp* string1 string2 start1 end1 start2 end2))
340    
341     (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
342     "Given two strings, if the first string is lexicographically greater than
343     the second string, returns the longest common prefix (using char-equal)
344     of the two strings. Otherwise, returns ()."
345     (string-greaterp* string1 string2 start1 end1 start2 end2))
346    
347     (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
348     "Given two strings, if the first string is lexicographically greater
349     than or equal to the second string, returns the longest common prefix
350     (using char-equal) of the two strings. Otherwise, returns ()."
351     (string-not-lessp* string1 string2 start1 end1 start2 end2))
352    
353     (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
354     end2)
355     "Given two strings, if the first string is lexicographically less than
356     or equal to the second string, returns the longest common prefix
357     (using char-equal) of the two strings. Otherwise, returns ()."
358     (string-not-greaterp* string1 string2 start1 end1 start2 end2))
359    
360    
361 dtc 1.9 (defun make-string (count &key element-type ((:initial-element fill-char)))
362 ram 1.1 "Given a character count and an optional fill character, makes and returns
363     a new string Count long filled with the fill character."
364 dtc 1.9 (declare (fixnum count)
365     (ignore element-type))
366 ram 1.1 (if fill-char
367     (do ((i 0 (1+ i))
368     (string (make-string count)))
369     ((= i count) string)
370     (declare (fixnum i))
371     (setf (schar string i) fill-char))
372     (make-string count)))
373    
374     (defun string-upcase (string &key (start 0) end)
375     "Given a string, returns a new string that is a copy of it with
376     all lower case alphabetic characters converted to uppercase."
377     (declare (fixnum start))
378 ram 1.5 (let* ((string (if (stringp string) string (string string)))
379     (slen (length string)))
380 ram 1.4 (declare (fixnum slen))
381 ram 1.1 (with-one-string string start end offset
382     (let ((offset-slen (+ slen offset))
383     (newstring (make-string slen)))
384     (declare (fixnum offset-slen))
385     (do ((index offset (1+ index))
386     (new-index 0 (1+ new-index)))
387     ((= index start))
388     (declare (fixnum index new-index))
389     (setf (schar newstring new-index) (schar string index)))
390     (do ((index start (1+ index))
391     (new-index (- start offset) (1+ new-index)))
392     ((= index (the fixnum end)))
393     (declare (fixnum index new-index))
394     (setf (schar newstring new-index)
395     (char-upcase (schar string index))))
396     (do ((index end (1+ index))
397     (new-index (- (the fixnum end) offset) (1+ new-index)))
398     ((= index offset-slen))
399     (declare (fixnum index new-index))
400     (setf (schar newstring new-index) (schar string index)))
401     newstring))))
402    
403     (defun string-downcase (string &key (start 0) end)
404     "Given a string, returns a new string that is a copy of it with
405     all upper case alphabetic characters converted to lowercase."
406     (declare (fixnum start))
407 ram 1.5 (let* ((string (if (stringp string) string (string string)))
408     (slen (length string)))
409 ram 1.4 (declare (fixnum slen))
410 ram 1.1 (with-one-string string start end offset
411     (let ((offset-slen (+ slen offset))
412     (newstring (make-string slen)))
413     (declare (fixnum offset-slen))
414     (do ((index offset (1+ index))
415     (new-index 0 (1+ new-index)))
416     ((= index start))
417     (declare (fixnum index new-index))
418     (setf (schar newstring new-index) (schar string index)))
419     (do ((index start (1+ index))
420     (new-index (- start offset) (1+ new-index)))
421     ((= index (the fixnum end)))
422     (declare (fixnum index new-index))
423     (setf (schar newstring new-index)
424     (char-downcase (schar string index))))
425     (do ((index end (1+ index))
426     (new-index (- (the fixnum end) offset) (1+ new-index)))
427     ((= index offset-slen))
428     (declare (fixnum index new-index))
429     (setf (schar newstring new-index) (schar string index)))
430     newstring))))
431    
432     (defun string-capitalize (string &key (start 0) end)
433     "Given a string, returns a copy of the string with the first
434     character of each ``word'' converted to upper-case, and remaining
435     chars in the word converted to lower case. A ``word'' is defined
436     to be a string of case-modifiable characters delimited by
437     non-case-modifiable chars."
438     (declare (fixnum start))
439 ram 1.5 (let* ((string (if (stringp string) string (string string)))
440     (slen (length string)))
441 ram 1.4 (declare (fixnum slen))
442 ram 1.1 (with-one-string string start end offset
443     (let ((offset-slen (+ slen offset))
444     (newstring (make-string slen)))
445     (declare (fixnum offset-slen))
446     (do ((index offset (1+ index))
447     (new-index 0 (1+ new-index)))
448     ((= index start))
449     (declare (fixnum index new-index))
450     (setf (schar newstring new-index) (schar string index)))
451     (do ((index start (1+ index))
452     (new-index (- start offset) (1+ new-index))
453     (newword t)
454     (char ()))
455     ((= index (the fixnum end)))
456     (declare (fixnum index new-index))
457     (setq char (schar string index))
458     (cond ((not (alphanumericp char))
459     (setq newword t))
460     (newword
461     ;;char is first case-modifiable after non-case-modifiable
462     (setq char (char-upcase char))
463     (setq newword ()))
464     ;;char is case-modifiable, but not first
465     (t (setq char (char-downcase char))))
466     (setf (schar newstring new-index) char))
467     (do ((index end (1+ index))
468     (new-index (- (the fixnum end) offset) (1+ new-index)))
469     ((= index offset-slen))
470     (declare (fixnum index new-index))
471     (setf (schar newstring new-index) (schar string index)))
472     newstring))))
473    
474     (defun nstring-upcase (string &key (start 0) end)
475     "Given a string, returns that string with all lower case alphabetic
476     characters converted to uppercase."
477     (declare (fixnum start))
478 ram 1.4 (let ((save-header string))
479 ram 1.1 (with-one-string string start end offset
480     (do ((index start (1+ index)))
481     ((= index (the fixnum end)))
482     (declare (fixnum index))
483     (setf (schar string index) (char-upcase (schar string index)))))
484     save-header))
485    
486     (defun nstring-downcase (string &key (start 0) end)
487     "Given a string, returns that string with all upper case alphabetic
488     characters converted to lowercase."
489     (declare (fixnum start))
490 ram 1.4 (let ((save-header string))
491 ram 1.1 (with-one-string string start end offset
492     (do ((index start (1+ index)))
493     ((= index (the fixnum end)))
494     (declare (fixnum index))
495     (setf (schar string index) (char-downcase (schar string index)))))
496 wlott 1.6 save-header))
497 ram 1.1
498     (defun nstring-capitalize (string &key (start 0) end)
499     "Given a string, returns that string with the first
500     character of each ``word'' converted to upper-case, and remaining
501     chars in the word converted to lower case. A ``word'' is defined
502     to be a string of case-modifiable characters delimited by
503     non-case-modifiable chars."
504     (declare (fixnum start))
505 ram 1.4 (let ((save-header string))
506 ram 1.1 (with-one-string string start end offset
507     (do ((index start (1+ index))
508     (newword t)
509     (char ()))
510     ((= index (the fixnum end)))
511     (declare (fixnum index))
512     (setq char (schar string index))
513     (cond ((not (alphanumericp char))
514     (setq newword t))
515     (newword
516     ;;char is first case-modifiable after non-case-modifiable
517     (setf (schar string index) (char-upcase char))
518     (setq newword ()))
519     (t
520     (setf (schar string index) (char-downcase char))))))
521     save-header))
522    
523     (defun string-left-trim (char-bag string)
524     "Given a set of characters (a list or string) and a string, returns
525     a copy of the string with the characters in the set removed from the
526     left end."
527     (with-string string
528     (do ((index start (1+ index)))
529     ((or (= index (the fixnum end))
530     (not (find (schar string index) char-bag)))
531     (subseq (the simple-string string) index end))
532     (declare (fixnum index)))))
533    
534     (defun string-right-trim (char-bag string)
535     "Given a set of characters (a list or string) and a string, returns
536     a copy of the string with the characters in the set removed from the
537     right end."
538     (with-string string
539     (do ((index (1- (the fixnum end)) (1- index)))
540     ((or (< index start) (not (find (schar string index) char-bag)))
541     (subseq (the simple-string string) start (1+ index)))
542     (declare (fixnum index)))))
543    
544     (defun string-trim (char-bag string)
545     "Given a set of characters (a list or string) and a string, returns a
546     copy of the string with the characters in the set removed from both
547     ends."
548     (with-string string
549     (let* ((left-end (do ((index start (1+ index)))
550     ((or (= index (the fixnum end))
551     (not (find (schar string index) char-bag)))
552     index)
553     (declare (fixnum index))))
554     (right-end (do ((index (1- (the fixnum end)) (1- index)))
555     ((or (< index left-end)
556     (not (find (schar string index) char-bag)))
557     (1+ index))
558     (declare (fixnum index)))))
559     (subseq (the simple-string string) left-end right-end))))

  ViewVC Help
Powered by ViewVC 1.1.5