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

Contents of /src/code/string.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5