1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!IMPL")
14 "Coerces X into a string. If X is a string, X is returned. If X is a
15 symbol, X's pname is returned. If X is a character then a one element
16 string containing that character is returned. If X cannot be coerced
17 into a string, an error occurs."
19 ((symbolp x) (symbol-name x))
21 (let ((res (make-string 1)))
22 (setf (schar res 0) x) res))
24 (error 'simple-type-error
26 :expected-type 'stringable
27 :format-control "~S cannot be coerced to a string."
28 :format-arguments (list x)))))
30 ;;; With-One-String is used to set up some string hacking things. The keywords
31 ;;; are parsed, and the string is hacked into a simple-string.
33 (eval-when (:compile-toplevel)
35 (sb!xc:defmacro with-one-string (string start end cum-offset &rest forms)
36 `(let ((,string (if (stringp ,string) ,string (string ,string))))
37 (with-array-data ((,string ,string :offset-var ,cum-offset)
39 (,end (or ,end (length (the vector ,string)))))
44 ;;; With-String is like With-One-String, but doesn't parse keywords.
46 (eval-when (:compile-toplevel)
48 (sb!xc:defmacro with-string (string &rest forms)
49 `(let ((,string (if (stringp ,string) ,string (string ,string))))
50 (with-array-data ((,string ,string)
52 (end (length (the vector ,string))))
57 ;;; With-Two-Strings is used to set up string comparison operations. The
58 ;;; keywords are parsed, and the strings are hacked into simple-strings.
60 (eval-when (:compile-toplevel)
62 (sb!xc:defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
63 start2 end2 &rest forms)
64 `(let ((,string1 (if (stringp ,string1) ,string1 (string ,string1)))
65 (,string2 (if (stringp ,string2) ,string2 (string ,string2))))
66 (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
68 (,end1 (or ,end1 (length (the vector ,string1)))))
69 (with-array-data ((,string2 ,string2)
71 (,end2 (or ,end2 (length (the vector ,string2)))))
76 (defun char (string index)
78 "Given a string and a non-negative integer index less than the length of
79 the string, returns the character object representing the character at
80 that position in the string."
81 (declare (optimize (safety 1)))
84 (defun %charset (string index new-el)
85 (declare (optimize (safety 1)))
86 (setf (char string index) new-el))
88 (defun schar (string index)
90 "SCHAR returns the character object at an indexed position in a string
91 just as CHAR does, except the string must be a simple-string."
92 (declare (optimize (safety 1)))
95 (defun %scharset (string index new-el)
96 (declare (optimize (safety 1)))
97 (setf (schar string index) new-el))
99 (defun string=* (string1 string2 start1 end1 start2 end2)
100 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
101 (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
103 (defun string/=* (string1 string2 start1 end1 start2 end2)
104 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
105 (let ((comparison (%sp-string-compare string1 start1 end1
106 string2 start2 end2)))
107 (if comparison (- (the fixnum comparison) offset1)))))
109 (eval-when (:compile-toplevel :execute)
111 ;;; Lessp is true if the desired expansion is for string<* or string<=*.
112 ;;; Equalp is true if the desired expansion is for string<=* or string>=*.
113 (sb!xc:defmacro string<>=*-body (lessp equalp)
114 (let ((offset1 (gensym)))
115 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
116 (let ((index (%sp-string-compare string1 start1 end1
117 string2 start2 end2)))
119 (cond ((= (the fixnum index) (the fixnum end1))
121 `(- (the fixnum index) ,offset1)
123 ((= (+ (the fixnum index) (- start2 start1))
127 `(- (the fixnum index) ,offset1)))
128 ((,(if lessp 'char< 'char>)
129 (schar string1 index)
130 (schar string2 (+ (the fixnum index) (- start2 start1))))
131 (- (the fixnum index) ,offset1))
133 ,(if equalp `(- (the fixnum end1) ,offset1) 'nil))))))
136 (defun string<* (string1 string2 start1 end1 start2 end2)
137 (declare (fixnum start1 start2))
138 (string<>=*-body t nil))
140 (defun string>* (string1 string2 start1 end1 start2 end2)
141 (declare (fixnum start1 start2))
142 (string<>=*-body nil nil))
144 (defun string<=* (string1 string2 start1 end1 start2 end2)
145 (declare (fixnum start1 start2))
146 (string<>=*-body t t))
148 (defun string>=* (string1 string2 start1 end1 start2 end2)
149 (declare (fixnum start1 start2))
150 (string<>=*-body nil t))
152 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
154 "Given two strings, if the first string is lexicographically less than
155 the second string, returns the longest common prefix (using char=)
156 of the two strings. Otherwise, returns ()."
157 (string<* string1 string2 start1 end1 start2 end2))
159 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
161 "Given two strings, if the first string is lexicographically greater than
162 the second string, returns the longest common prefix (using char=)
163 of the two strings. Otherwise, returns ()."
164 (string>* string1 string2 start1 end1 start2 end2))
166 (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
168 "Given two strings, if the first string is lexicographically less than
169 or equal to the second string, returns the longest common prefix
170 (using char=) of the two strings. Otherwise, returns ()."
171 (string<=* string1 string2 start1 end1 start2 end2))
173 (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
174 "Given two strings, if the first string is lexicographically greater
175 than or equal to the second string, returns the longest common prefix
176 (using char=) of the two strings. Otherwise, returns ()."
177 (string>=* string1 string2 start1 end1 start2 end2))
179 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
180 ;;; an error instead of returning NIL as I would have expected.
181 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
182 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
183 ;;; this is conforming (and required) behavior, because any index
184 ;;; out of range is an error. (So there seems to be no concise and
185 ;;; efficient way to test for strings which begin with a particular
186 ;;; pattern. Alas..) -- WHN 19991206
187 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
189 "Given two strings (string1 and string2), and optional integers start1,
190 start2, end1 and end2, compares characters in string1 to characters in
191 string2 (using char=)."
192 (string=* string1 string2 start1 end1 start2 end2))
194 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
196 "Given two strings, if the first string is not lexicographically equal
197 to the second string, returns the longest common prefix (using char=)
198 of the two strings. Otherwise, returns ()."
199 (string/=* string1 string2 start1 end1 start2 end2))
201 (eval-when (:compile-toplevel :execute)
203 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
204 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
205 (sb!xc:defmacro string-not-equal-loop (end
207 &optional (abort-value nil abortp))
208 (declare (fixnum end))
209 (let ((end-test (if (= end 1)
210 `(= index1 (the fixnum end1))
211 `(= index2 (the fixnum end2)))))
212 `(do ((index1 start1 (1+ index1))
213 (index2 start2 (1+ index2)))
217 (not (char-equal (schar string1 index1)
218 (schar string2 index2)))))
220 (declare (fixnum index1 index2))
222 `((if (not (char-equal (schar string1 index1)
223 (schar string2 index2)))
224 (return ,abort-value)))))))
228 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
230 "Given two strings (string1 and string2), and optional integers start1,
231 start2, end1 and end2, compares characters in string1 to characters in
232 string2 (using char-equal)."
233 (declare (fixnum start1 start2))
234 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
235 (let ((slen1 (- (the fixnum end1) start1))
236 (slen2 (- (the fixnum end2) start2)))
237 (declare (fixnum slen1 slen2))
238 (if (or (minusp slen1) (minusp slen2))
239 ;;prevent endless looping later.
240 (error "Improper bounds for string comparison."))
242 ;;return () immediately if lengths aren't equal.
243 (string-not-equal-loop 1 t nil)))))
245 (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
247 "Given two strings, if the first string is not lexicographically equal
248 to the second string, returns the longest common prefix (using char-equal)
249 of the two strings. Otherwise, returns ()."
250 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
251 (let ((slen1 (- end1 start1))
252 (slen2 (- end2 start2)))
253 (declare (fixnum slen1 slen2))
254 (if (or (minusp slen1) (minusp slen2))
255 ;;prevent endless looping later.
256 (error "Improper bounds for string comparison."))
257 (cond ((or (minusp slen1) (or (minusp slen2)))
258 (error "Improper substring for comparison."))
260 (string-not-equal-loop 1 nil (- index1 offset1)))
262 (string-not-equal-loop 1 (- index1 offset1)))
264 (string-not-equal-loop 2 (- index1 offset1)))))))
266 (eval-when (:compile-toplevel :execute)
268 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
269 ;;; and string2 and a test on the current characters from string1 and string2
270 ;;; for the following macro.
271 (defun string-less-greater-equal-tests (lessp equalp)
274 ;; STRING-NOT-GREATERP
275 (values '<= `(not (char-greaterp char1 char2)))
277 (values '< `(char-lessp char1 char2)))
280 (values '>= `(not (char-lessp char1 char2)))
282 (values '> `(char-greaterp char1 char2)))))
284 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
285 (multiple-value-bind (length-test character-test)
286 (string-less-greater-equal-tests lessp equalp)
287 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
288 (let ((slen1 (- (the fixnum end1) start1))
289 (slen2 (- (the fixnum end2) start2)))
290 (declare (fixnum slen1 slen2))
291 (if (or (minusp slen1) (minusp slen2))
292 ;;prevent endless looping later.
293 (error "Improper bounds for string comparison."))
294 (do ((index1 start1 (1+ index1))
295 (index2 start2 (1+ index2))
298 ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
299 (if (,length-test slen1 slen2) (- index1 offset1)))
300 (declare (fixnum index1 index2))
301 (setq char1 (schar string1 index1))
302 (setq char2 (schar string2 index2))
303 (if (not (char-equal char1 char2))
305 (return (- index1 offset1))
310 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
311 (declare (fixnum start1 start2))
312 (string-less-greater-equal t nil))
314 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
315 (declare (fixnum start1 start2))
316 (string-less-greater-equal nil nil))
318 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
319 (declare (fixnum start1 start2))
320 (string-less-greater-equal nil t))
322 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
323 (declare (fixnum start1 start2))
324 (string-less-greater-equal t t))
326 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
328 "Given two strings, if the first string is lexicographically less than
329 the second string, returns the longest common prefix (using char-equal)
330 of the two strings. Otherwise, returns ()."
331 (string-lessp* string1 string2 start1 end1 start2 end2))
333 (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
335 "Given two strings, if the first string is lexicographically greater than
336 the second string, returns the longest common prefix (using char-equal)
337 of the two strings. Otherwise, returns ()."
338 (string-greaterp* string1 string2 start1 end1 start2 end2))
340 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
342 "Given two strings, if the first string is lexicographically greater
343 than or equal to the second string, returns the longest common prefix
344 (using char-equal) of the two strings. Otherwise, returns ()."
345 (string-not-lessp* string1 string2 start1 end1 start2 end2))
347 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
350 "Given two strings, if the first string is lexicographically less than
351 or equal to the second string, returns the longest common prefix
352 (using char-equal) of the two strings. Otherwise, returns ()."
353 (string-not-greaterp* string1 string2 start1 end1 start2 end2))
355 (defun make-string (count &key element-type ((:initial-element fill-char)))
357 "Given a character count and an optional fill character, makes and returns
358 a new string Count long filled with the fill character."
359 (declare (fixnum count)
360 (ignore element-type))
363 (string (make-string count)))
366 (setf (schar string i) fill-char))
367 (make-string count)))
369 (defun string-upcase (string &key (start 0) end)
371 "Given a string, returns a new string that is a copy of it with
372 all lower case alphabetic characters converted to uppercase."
373 (declare (fixnum start))
374 (let* ((string (if (stringp string) string (string string)))
375 (slen (length string)))
376 (declare (fixnum slen))
377 (with-one-string string start end offset
378 (let ((offset-slen (+ slen offset))
379 (newstring (make-string slen)))
380 (declare (fixnum offset-slen))
381 (do ((index offset (1+ index))
382 (new-index 0 (1+ new-index)))
384 (declare (fixnum index new-index))
385 (setf (schar newstring new-index) (schar string index)))
386 (do ((index start (1+ index))
387 (new-index (- start offset) (1+ new-index)))
388 ((= index (the fixnum end)))
389 (declare (fixnum index new-index))
390 (setf (schar newstring new-index)
391 (char-upcase (schar string index))))
392 (do ((index end (1+ index))
393 (new-index (- (the fixnum end) offset) (1+ new-index)))
394 ((= index offset-slen))
395 (declare (fixnum index new-index))
396 (setf (schar newstring new-index) (schar string index)))
399 (defun string-downcase (string &key (start 0) end)
401 "Given a string, returns a new string that is a copy of it with
402 all upper case alphabetic characters converted to lowercase."
403 (declare (fixnum start))
404 (let* ((string (if (stringp string) string (string string)))
405 (slen (length string)))
406 (declare (fixnum slen))
407 (with-one-string string start end offset
408 (let ((offset-slen (+ slen offset))
409 (newstring (make-string slen)))
410 (declare (fixnum offset-slen))
411 (do ((index offset (1+ index))
412 (new-index 0 (1+ new-index)))
414 (declare (fixnum index new-index))
415 (setf (schar newstring new-index) (schar string index)))
416 (do ((index start (1+ index))
417 (new-index (- start offset) (1+ new-index)))
418 ((= index (the fixnum end)))
419 (declare (fixnum index new-index))
420 (setf (schar newstring new-index)
421 (char-downcase (schar string index))))
422 (do ((index end (1+ index))
423 (new-index (- (the fixnum end) offset) (1+ new-index)))
424 ((= index offset-slen))
425 (declare (fixnum index new-index))
426 (setf (schar newstring new-index) (schar string index)))
429 (defun string-capitalize (string &key (start 0) end)
431 "Given a string, returns a copy of the string with the first
432 character of each ``word'' converted to upper-case, and remaining
433 chars in the word converted to lower case. A ``word'' is defined
434 to be a string of case-modifiable characters delimited by
435 non-case-modifiable chars."
436 (declare (fixnum start))
437 (let* ((string (if (stringp string) string (string string)))
438 (slen (length string)))
439 (declare (fixnum slen))
440 (with-one-string string start end offset
441 (let ((offset-slen (+ slen offset))
442 (newstring (make-string slen)))
443 (declare (fixnum offset-slen))
444 (do ((index offset (1+ index))
445 (new-index 0 (1+ new-index)))
447 (declare (fixnum index new-index))
448 (setf (schar newstring new-index) (schar string index)))
449 (do ((index start (1+ index))
450 (new-index (- start offset) (1+ new-index))
453 ((= index (the fixnum end)))
454 (declare (fixnum index new-index))
455 (setq char (schar string index))
456 (cond ((not (alphanumericp char))
459 ;;char is first case-modifiable after non-case-modifiable
460 (setq char (char-upcase char))
462 ;;char is case-modifiable, but not first
463 (t (setq char (char-downcase char))))
464 (setf (schar newstring new-index) char))
465 (do ((index end (1+ index))
466 (new-index (- (the fixnum end) offset) (1+ new-index)))
467 ((= index offset-slen))
468 (declare (fixnum index new-index))
469 (setf (schar newstring new-index) (schar string index)))
472 (defun nstring-upcase (string &key (start 0) end)
474 "Given a string, returns that string with all lower case alphabetic
475 characters converted to uppercase."
476 (declare (fixnum start))
477 (let ((save-header string))
478 (with-one-string string start end offset
479 (do ((index start (1+ index)))
480 ((= index (the fixnum end)))
481 (declare (fixnum index))
482 (setf (schar string index) (char-upcase (schar string index)))))
485 (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 (let ((save-header string))
491 (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)))))
498 (defun nstring-capitalize (string &key (start 0) end)
500 "Given a string, returns that string with the first
501 character of each ``word'' converted to upper-case, and remaining
502 chars in the word converted to lower case. A ``word'' is defined
503 to be a string of case-modifiable characters delimited by
504 non-case-modifiable chars."
505 (declare (fixnum start))
506 (let ((save-header string))
507 (with-one-string string start end offset
508 (do ((index start (1+ index))
511 ((= index (the fixnum end)))
512 (declare (fixnum index))
513 (setq char (schar string index))
514 (cond ((not (alphanumericp char))
517 ;;char is first case-modifiable after non-case-modifiable
518 (setf (schar string index) (char-upcase char))
521 (setf (schar string index) (char-downcase char))))))
524 (defun string-left-trim (char-bag string)
526 "Given a set of characters (a list or string) and a string, returns
527 a copy of the string with the characters in the set removed from the
530 (do ((index start (1+ index)))
531 ((or (= index (the fixnum end))
532 (not (find (schar string index) char-bag :test #'char=)))
533 (subseq (the simple-string string) index end))
534 (declare (fixnum index)))))
536 (defun string-right-trim (char-bag string)
538 "Given a set of characters (a list or string) and a string, returns
539 a copy of the string with the characters in the set removed from the
542 (do ((index (1- (the fixnum end)) (1- index)))
544 (not (find (schar string index) char-bag :test #'char=)))
545 (subseq (the simple-string string) start (1+ index)))
546 (declare (fixnum index)))))
548 (defun string-trim (char-bag string)
550 "Given a set of characters (a list or string) and a string, returns a
551 copy of the string with the characters in the set removed from both
554 (let* ((left-end (do ((index start (1+ index)))
555 ((or (= index (the fixnum end))
556 (not (find (schar string index)
560 (declare (fixnum index))))
561 (right-end (do ((index (1- (the fixnum end)) (1- index)))
562 ((or (< index left-end)
563 (not (find (schar string index)
567 (declare (fixnum index)))))
568 (subseq (the simple-string string) left-end right-end))))