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")
17 "Coerces X into a string. If X is a string, X is returned. If X is a
18 symbol, X's pname is returned. If X is a character then a one element
19 string containing that character is returned. If X cannot be coerced
20 into a string, an error occurs."
22 ((symbolp x) (symbol-name x))
24 (let ((res (make-string 1)))
25 (setf (schar res 0) x) res))
27 (error 'simple-type-error
29 :expected-type 'stringable
30 :format-control "~S cannot be coerced to a string."
31 :format-arguments (list x)))))
33 ;;; With-One-String is used to set up some string hacking things. The keywords
34 ;;; are parsed, and the string is hacked into a simple-string.
36 (eval-when (:compile-toplevel)
38 (sb!xc:defmacro with-one-string (string start end cum-offset &rest forms)
39 `(let ((,string (if (stringp ,string) ,string (string ,string))))
40 (with-array-data ((,string ,string :offset-var ,cum-offset)
42 (,end (or ,end (length (the vector ,string)))))
47 ;;; With-String is like With-One-String, but doesn't parse keywords.
49 (eval-when (:compile-toplevel)
51 (sb!xc:defmacro with-string (string &rest forms)
52 `(let ((,string (if (stringp ,string) ,string (string ,string))))
53 (with-array-data ((,string ,string)
55 (end (length (the vector ,string))))
60 ;;; With-Two-Strings is used to set up string comparison operations. The
61 ;;; keywords are parsed, and the strings are hacked into simple-strings.
63 (eval-when (:compile-toplevel)
65 (sb!xc:defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
66 start2 end2 &rest forms)
67 `(let ((,string1 (if (stringp ,string1) ,string1 (string ,string1)))
68 (,string2 (if (stringp ,string2) ,string2 (string ,string2))))
69 (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
71 (,end1 (or ,end1 (length (the vector ,string1)))))
72 (with-array-data ((,string2 ,string2)
74 (,end2 (or ,end2 (length (the vector ,string2)))))
79 (defun char (string index)
81 "Given a string and a non-negative integer index less than the length of
82 the string, returns the character object representing the character at
83 that position in the string."
84 (declare (optimize (safety 1)))
87 (defun %charset (string index new-el)
88 (declare (optimize (safety 1)))
89 (setf (char string index) new-el))
91 (defun schar (string index)
93 "SCHAR returns the character object at an indexed position in a string
94 just as CHAR does, except the string must be a simple-string."
95 (declare (optimize (safety 1)))
98 (defun %scharset (string index new-el)
99 (declare (optimize (safety 1)))
100 (setf (schar string index) new-el))
102 (defun string=* (string1 string2 start1 end1 start2 end2)
103 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
104 (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
106 (defun string/=* (string1 string2 start1 end1 start2 end2)
107 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
108 (let ((comparison (%sp-string-compare string1 start1 end1
109 string2 start2 end2)))
110 (if comparison (- (the fixnum comparison) offset1)))))
112 (eval-when (:compile-toplevel :execute)
114 ;;; Lessp is true if the desired expansion is for string<* or string<=*.
115 ;;; Equalp is true if the desired expansion is for string<=* or string>=*.
116 (sb!xc:defmacro string<>=*-body (lessp equalp)
117 (let ((offset1 (gensym)))
118 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
119 (let ((index (%sp-string-compare string1 start1 end1
120 string2 start2 end2)))
122 (cond ((= (the fixnum index) (the fixnum end1))
124 `(- (the fixnum index) ,offset1)
126 ((= (+ (the fixnum index) (- start2 start1))
130 `(- (the fixnum index) ,offset1)))
131 ((,(if lessp 'char< 'char>)
132 (schar string1 index)
133 (schar string2 (+ (the fixnum index) (- start2 start1))))
134 (- (the fixnum index) ,offset1))
136 ,(if equalp `(- (the fixnum end1) ,offset1) 'nil))))))
139 (defun string<* (string1 string2 start1 end1 start2 end2)
140 (declare (fixnum start1 start2))
141 (string<>=*-body t nil))
143 (defun string>* (string1 string2 start1 end1 start2 end2)
144 (declare (fixnum start1 start2))
145 (string<>=*-body nil nil))
147 (defun string<=* (string1 string2 start1 end1 start2 end2)
148 (declare (fixnum start1 start2))
149 (string<>=*-body t t))
151 (defun string>=* (string1 string2 start1 end1 start2 end2)
152 (declare (fixnum start1 start2))
153 (string<>=*-body nil t))
155 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
157 "Given two strings, if the first string is lexicographically less than
158 the second string, returns the longest common prefix (using char=)
159 of the two strings. Otherwise, returns ()."
160 (string<* string1 string2 start1 end1 start2 end2))
162 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
164 "Given two strings, if the first string is lexicographically greater than
165 the second string, returns the longest common prefix (using char=)
166 of the two strings. Otherwise, returns ()."
167 (string>* string1 string2 start1 end1 start2 end2))
169 (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
171 "Given two strings, if the first string is lexicographically less than
172 or equal to the second string, returns the longest common prefix
173 (using char=) of the two strings. Otherwise, returns ()."
174 (string<=* string1 string2 start1 end1 start2 end2))
176 (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
177 "Given two strings, if the first string is lexicographically greater
178 than or equal to the second string, returns the longest common prefix
179 (using char=) of the two strings. Otherwise, returns ()."
180 (string>=* string1 string2 start1 end1 start2 end2))
182 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
183 ;;; an error instead of returning NIL as I would have expected.
184 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
185 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
186 ;;; this is conforming (and required) behavior, because any index
187 ;;; out of range is an error. (So there seems to be no concise and
188 ;;; efficient way to test for strings which begin with a particular
189 ;;; pattern. Alas..) -- WHN 19991206
190 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
192 "Given two strings (string1 and string2), and optional integers start1,
193 start2, end1 and end2, compares characters in string1 to characters in
194 string2 (using char=)."
195 (string=* string1 string2 start1 end1 start2 end2))
197 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
199 "Given two strings, if the first string is not lexicographically equal
200 to the second string, returns the longest common prefix (using char=)
201 of the two strings. Otherwise, returns ()."
202 (string/=* string1 string2 start1 end1 start2 end2))
204 (eval-when (:compile-toplevel :execute)
206 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
207 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
208 (sb!xc:defmacro string-not-equal-loop (end
210 &optional (abort-value nil abortp))
211 (declare (fixnum end))
212 (let ((end-test (if (= end 1)
213 `(= index1 (the fixnum end1))
214 `(= index2 (the fixnum end2)))))
215 `(do ((index1 start1 (1+ index1))
216 (index2 start2 (1+ index2)))
220 (not (char-equal (schar string1 index1)
221 (schar string2 index2)))))
223 (declare (fixnum index1 index2))
225 `((if (not (char-equal (schar string1 index1)
226 (schar string2 index2)))
227 (return ,abort-value)))))))
231 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
233 "Given two strings (string1 and string2), and optional integers start1,
234 start2, end1 and end2, compares characters in string1 to characters in
235 string2 (using char-equal)."
236 (declare (fixnum start1 start2))
237 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
238 (let ((slen1 (- (the fixnum end1) start1))
239 (slen2 (- (the fixnum end2) start2)))
240 (declare (fixnum slen1 slen2))
241 (if (or (minusp slen1) (minusp slen2))
242 ;;prevent endless looping later.
243 (error "Improper bounds for string comparison."))
245 ;;return () immediately if lengths aren't equal.
246 (string-not-equal-loop 1 t nil)))))
248 (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."))
263 (string-not-equal-loop 1 nil (- index1 offset1)))
265 (string-not-equal-loop 1 (- index1 offset1)))
267 (string-not-equal-loop 2 (- index1 offset1)))))))
269 (eval-when (:compile-toplevel :execute)
271 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
272 ;;; and string2 and a test on the current characters from string1 and string2
273 ;;; for the following macro.
274 (defun string-less-greater-equal-tests (lessp equalp)
277 ;; STRING-NOT-GREATERP
278 (values '<= `(not (char-greaterp char1 char2)))
280 (values '< `(char-lessp char1 char2)))
283 (values '>= `(not (char-lessp char1 char2)))
285 (values '> `(char-greaterp char1 char2)))))
287 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
288 (multiple-value-bind (length-test character-test)
289 (string-less-greater-equal-tests lessp equalp)
290 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
291 (let ((slen1 (- (the fixnum end1) start1))
292 (slen2 (- (the fixnum end2) start2)))
293 (declare (fixnum slen1 slen2))
294 (if (or (minusp slen1) (minusp slen2))
295 ;;prevent endless looping later.
296 (error "Improper bounds for string comparison."))
297 (do ((index1 start1 (1+ index1))
298 (index2 start2 (1+ index2))
301 ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
302 (if (,length-test slen1 slen2) (- index1 offset1)))
303 (declare (fixnum index1 index2))
304 (setq char1 (schar string1 index1))
305 (setq char2 (schar string2 index2))
306 (if (not (char-equal char1 char2))
308 (return (- index1 offset1))
313 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
314 (declare (fixnum start1 start2))
315 (string-less-greater-equal t nil))
317 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
318 (declare (fixnum start1 start2))
319 (string-less-greater-equal nil nil))
321 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
322 (declare (fixnum start1 start2))
323 (string-less-greater-equal nil t))
325 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
326 (declare (fixnum start1 start2))
327 (string-less-greater-equal t t))
329 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
331 "Given two strings, if the first string is lexicographically less than
332 the second string, returns the longest common prefix (using char-equal)
333 of the two strings. Otherwise, returns ()."
334 (string-lessp* string1 string2 start1 end1 start2 end2))
336 (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))
343 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
345 "Given two strings, if the first string is lexicographically greater
346 than or equal to the second string, returns the longest common prefix
347 (using char-equal) of the two strings. Otherwise, returns ()."
348 (string-not-lessp* string1 string2 start1 end1 start2 end2))
350 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
353 "Given two strings, if the first string is lexicographically less than
354 or equal to the second string, returns the longest common prefix
355 (using char-equal) of the two strings. Otherwise, returns ()."
356 (string-not-greaterp* string1 string2 start1 end1 start2 end2))
358 (defun make-string (count &key element-type ((:initial-element fill-char)))
360 "Given a character count and an optional fill character, makes and returns
361 a new string Count long filled with the fill character."
362 (declare (fixnum count)
363 (ignore element-type))
366 (string (make-string count)))
369 (setf (schar string i) fill-char))
370 (make-string count)))
372 (defun string-upcase (string &key (start 0) end)
374 "Given a string, returns a new string that is a copy of it with
375 all lower case alphabetic characters converted to uppercase."
376 (declare (fixnum start))
377 (let* ((string (if (stringp string) string (string string)))
378 (slen (length string)))
379 (declare (fixnum slen))
380 (with-one-string string start end offset
381 (let ((offset-slen (+ slen offset))
382 (newstring (make-string slen)))
383 (declare (fixnum offset-slen))
384 (do ((index offset (1+ index))
385 (new-index 0 (1+ new-index)))
387 (declare (fixnum index new-index))
388 (setf (schar newstring new-index) (schar string index)))
389 (do ((index start (1+ index))
390 (new-index (- start offset) (1+ new-index)))
391 ((= index (the fixnum end)))
392 (declare (fixnum index new-index))
393 (setf (schar newstring new-index)
394 (char-upcase (schar string index))))
395 (do ((index end (1+ index))
396 (new-index (- (the fixnum end) offset) (1+ new-index)))
397 ((= index offset-slen))
398 (declare (fixnum index new-index))
399 (setf (schar newstring new-index) (schar string index)))
402 (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 (let* ((string (if (stringp string) string (string string)))
408 (slen (length string)))
409 (declare (fixnum slen))
410 (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)))
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)))
432 (defun string-capitalize (string &key (start 0) end)
434 "Given a string, returns a copy of the string with the first
435 character of each ``word'' converted to upper-case, and remaining
436 chars in the word converted to lower case. A ``word'' is defined
437 to be a string of case-modifiable characters delimited by
438 non-case-modifiable chars."
439 (declare (fixnum start))
440 (let* ((string (if (stringp string) string (string string)))
441 (slen (length string)))
442 (declare (fixnum slen))
443 (with-one-string string start end offset
444 (let ((offset-slen (+ slen offset))
445 (newstring (make-string slen)))
446 (declare (fixnum offset-slen))
447 (do ((index offset (1+ index))
448 (new-index 0 (1+ new-index)))
450 (declare (fixnum index new-index))
451 (setf (schar newstring new-index) (schar string index)))
452 (do ((index start (1+ index))
453 (new-index (- start offset) (1+ new-index))
456 ((= index (the fixnum end)))
457 (declare (fixnum index new-index))
458 (setq char (schar string index))
459 (cond ((not (alphanumericp char))
462 ;;char is first case-modifiable after non-case-modifiable
463 (setq char (char-upcase char))
465 ;;char is case-modifiable, but not first
466 (t (setq char (char-downcase char))))
467 (setf (schar newstring new-index) char))
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)))
475 (defun nstring-upcase (string &key (start 0) end)
477 "Given a string, returns that string with all lower case alphabetic
478 characters converted to uppercase."
479 (declare (fixnum start))
480 (let ((save-header string))
481 (with-one-string string start end offset
482 (do ((index start (1+ index)))
483 ((= index (the fixnum end)))
484 (declare (fixnum index))
485 (setf (schar string index) (char-upcase (schar string index)))))
488 (defun nstring-downcase (string &key (start 0) end)
490 "Given a string, returns that string with all upper case alphabetic
491 characters converted to lowercase."
492 (declare (fixnum start))
493 (let ((save-header string))
494 (with-one-string string start end offset
495 (do ((index start (1+ index)))
496 ((= index (the fixnum end)))
497 (declare (fixnum index))
498 (setf (schar string index) (char-downcase (schar string index)))))
501 (defun nstring-capitalize (string &key (start 0) end)
503 "Given a string, returns that string with the first
504 character of each ``word'' converted to upper-case, and remaining
505 chars in the word converted to lower case. A ``word'' is defined
506 to be a string of case-modifiable characters delimited by
507 non-case-modifiable chars."
508 (declare (fixnum start))
509 (let ((save-header string))
510 (with-one-string string start end offset
511 (do ((index start (1+ index))
514 ((= index (the fixnum end)))
515 (declare (fixnum index))
516 (setq char (schar string index))
517 (cond ((not (alphanumericp char))
520 ;;char is first case-modifiable after non-case-modifiable
521 (setf (schar string index) (char-upcase char))
524 (setf (schar string index) (char-downcase char))))))
527 (defun string-left-trim (char-bag string)
529 "Given a set of characters (a list or string) and a string, returns
530 a copy of the string with the characters in the set removed from the
533 (do ((index start (1+ index)))
534 ((or (= index (the fixnum end))
535 (not (find (schar string index) char-bag :test #'char=)))
536 (subseq (the simple-string string) index end))
537 (declare (fixnum index)))))
539 (defun string-right-trim (char-bag string)
541 "Given a set of characters (a list or string) and a string, returns
542 a copy of the string with the characters in the set removed from the
545 (do ((index (1- (the fixnum end)) (1- index)))
547 (not (find (schar string index) char-bag :test #'char=)))
548 (subseq (the simple-string string) start (1+ index)))
549 (declare (fixnum index)))))
551 (defun string-trim (char-bag string)
553 "Given a set of characters (a list or string) and a string, returns a
554 copy of the string with the characters in the set removed from both
557 (let* ((left-end (do ((index start (1+ index)))
558 ((or (= index (the fixnum end))
559 (not (find (schar string index)
563 (declare (fixnum index))))
564 (right-end (do ((index (1- (the fixnum end)) (1- index)))
565 ((or (< index left-end)
566 (not (find (schar string index)
570 (declare (fixnum index)))))
571 (subseq (the simple-string string) left-end right-end))))