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

Contents of /src/code/string.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5