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 (eval-when (:compile-toplevel)
31 ;;; WITH-ONE-STRING is used to set up some string hacking things. The
32 ;;; keywords are parsed, and the string is hacked into a
34 (sb!xc:defmacro with-one-string ((string start end) &body forms)
35 `(let ((,string (if (stringp ,string) ,string (string ,string))))
36 (with-array-data ((,string ,string)
38 (,end (or ,end (length (the vector ,string)))))
40 ;;; WITH-STRING is like WITH-ONE-STRING, but doesn't parse keywords.
41 (sb!xc:defmacro with-string (string &rest forms)
42 `(let ((,string (if (stringp ,string) ,string (string ,string))))
43 (with-array-data ((,string ,string)
45 (end (length (the vector ,string))))
47 ;;; WITH-TWO-STRINGS is used to set up string comparison operations. The
48 ;;; keywords are parsed, and the strings are hacked into SIMPLE-STRINGs.
49 (sb!xc:defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
50 start2 end2 &rest forms)
51 `(let ((,string1 (if (stringp ,string1) ,string1 (string ,string1)))
52 (,string2 (if (stringp ,string2) ,string2 (string ,string2))))
53 (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
55 (,end1 (or ,end1 (length (the vector ,string1)))))
56 (with-array-data ((,string2 ,string2)
58 (,end2 (or ,end2 (length (the vector ,string2)))))
62 (defun char (string index)
64 "Given a string and a non-negative integer index less than the length of
65 the string, returns the character object representing the character at
66 that position in the string."
67 (declare (optimize (safety 1)))
70 (defun %charset (string index new-el)
71 (declare (optimize (safety 1)))
72 (setf (char string index) new-el))
74 (defun schar (string index)
76 "SCHAR returns the character object at an indexed position in a string
77 just as CHAR does, except the string must be a simple-string."
78 (declare (optimize (safety 1)))
81 (defun %scharset (string index new-el)
82 (declare (optimize (safety 1)))
83 (setf (schar string index) new-el))
85 (defun string=* (string1 string2 start1 end1 start2 end2)
86 (with-two-strings string1 string2 start1 end1 nil start2 end2
87 (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
89 (defun string/=* (string1 string2 start1 end1 start2 end2)
90 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
91 (let ((comparison (%sp-string-compare string1 start1 end1
92 string2 start2 end2)))
93 (if comparison (- (the fixnum comparison) offset1)))))
95 (eval-when (:compile-toplevel :execute)
97 ;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
98 ;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
99 (sb!xc:defmacro string<>=*-body (lessp equalp)
100 (let ((offset1 (gensym)))
101 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
102 (let ((index (%sp-string-compare string1 start1 end1
103 string2 start2 end2)))
105 (cond ((= (the fixnum index) (the fixnum end1))
107 `(- (the fixnum index) ,offset1)
109 ((= (+ (the fixnum index) (- start2 start1))
113 `(- (the fixnum index) ,offset1)))
114 ((,(if lessp 'char< 'char>)
115 (schar string1 index)
116 (schar string2 (+ (the fixnum index) (- start2 start1))))
117 (- (the fixnum index) ,offset1))
119 ,(if equalp `(- (the fixnum end1) ,offset1) nil))))))
122 (defun string<* (string1 string2 start1 end1 start2 end2)
123 (declare (fixnum start1 start2))
124 (string<>=*-body t nil))
126 (defun string>* (string1 string2 start1 end1 start2 end2)
127 (declare (fixnum start1 start2))
128 (string<>=*-body nil nil))
130 (defun string<=* (string1 string2 start1 end1 start2 end2)
131 (declare (fixnum start1 start2))
132 (string<>=*-body t t))
134 (defun string>=* (string1 string2 start1 end1 start2 end2)
135 (declare (fixnum start1 start2))
136 (string<>=*-body nil t))
138 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
140 "Given two strings, if the first string is lexicographically less than
141 the second string, returns the longest common prefix (using char=)
142 of the two strings. Otherwise, returns ()."
143 (string<* string1 string2 start1 end1 start2 end2))
145 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
147 "Given two strings, if the first string is lexicographically greater than
148 the second string, returns the longest common prefix (using char=)
149 of the two strings. Otherwise, returns ()."
150 (string>* string1 string2 start1 end1 start2 end2))
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 or equal to the second string, returns the longest common prefix
156 (using char=) 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)
160 "Given two strings, if the first string is lexicographically greater
161 than or equal to the second string, returns the longest common prefix
162 (using char=) of the two strings. Otherwise, returns ()."
163 (string>=* string1 string2 start1 end1 start2 end2))
165 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
166 ;;; an error instead of returning NIL as I would have expected.
167 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
168 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
169 ;;; this is conforming (and required) behavior, because any index
170 ;;; out of range is an error. (So there seems to be no concise and
171 ;;; efficient way to test for strings which begin with a particular
172 ;;; pattern. Alas..) -- WHN 19991206
173 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
175 "Given two strings (string1 and string2), and optional integers start1,
176 start2, end1 and end2, compares characters in string1 to characters in
177 string2 (using char=)."
178 (string=* string1 string2 start1 end1 start2 end2))
180 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
182 "Given two strings, if the first string is not lexicographically equal
183 to the second string, returns the longest common prefix (using char=)
184 of the two strings. Otherwise, returns ()."
185 (string/=* string1 string2 start1 end1 start2 end2))
187 (eval-when (:compile-toplevel :execute)
189 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
190 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
191 (sb!xc:defmacro string-not-equal-loop (end
193 &optional (abort-value nil abortp))
194 (declare (fixnum end))
195 (let ((end-test (if (= end 1)
196 `(= index1 (the fixnum end1))
197 `(= index2 (the fixnum end2)))))
198 `(do ((index1 start1 (1+ index1))
199 (index2 start2 (1+ index2)))
203 (not (char-equal (schar string1 index1)
204 (schar string2 index2)))))
206 (declare (fixnum index1 index2))
208 `((if (not (char-equal (schar string1 index1)
209 (schar string2 index2)))
210 (return ,abort-value)))))))
214 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
216 "Given two strings (string1 and string2), and optional integers start1,
217 start2, end1 and end2, compares characters in string1 to characters in
218 string2 (using char-equal)."
219 (declare (fixnum start1 start2))
220 (with-two-strings string1 string2 start1 end1 nil start2 end2
221 (let ((slen1 (- (the fixnum end1) start1))
222 (slen2 (- (the fixnum end2) start2)))
223 (declare (fixnum slen1 slen2))
224 (if (or (minusp slen1) (minusp slen2))
225 ;;prevent endless looping later.
226 (error "Improper bounds for string comparison."))
228 ;;return () immediately if lengths aren't equal.
229 (string-not-equal-loop 1 t nil)))))
231 (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
233 "Given two strings, if the first string is not lexicographically equal
234 to the second string, returns the longest common prefix (using char-equal)
235 of the two strings. Otherwise, returns ()."
236 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
237 (let ((slen1 (- end1 start1))
238 (slen2 (- end2 start2)))
239 (declare (fixnum slen1 slen2))
240 (if (or (minusp slen1) (minusp slen2))
241 ;; Prevent endless looping later.
242 (error "Improper bounds for string comparison."))
243 (cond ((or (minusp slen1) (or (minusp slen2)))
244 (error "Improper substring for comparison."))
246 (string-not-equal-loop 1 nil (- index1 offset1)))
248 (string-not-equal-loop 1 (- index1 offset1)))
250 (string-not-equal-loop 2 (- index1 offset1)))))))
252 (eval-when (:compile-toplevel :execute)
254 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
255 ;;; and string2 and a test on the current characters from string1 and string2
256 ;;; for the following macro.
257 (defun string-less-greater-equal-tests (lessp equalp)
260 ;; STRING-NOT-GREATERP
261 (values '<= `(not (char-greaterp char1 char2)))
263 (values '< `(char-lessp char1 char2)))
266 (values '>= `(not (char-lessp char1 char2)))
268 (values '> `(char-greaterp char1 char2)))))
270 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
271 (multiple-value-bind (length-test character-test)
272 (string-less-greater-equal-tests lessp equalp)
273 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
274 (let ((slen1 (- (the fixnum end1) start1))
275 (slen2 (- (the fixnum end2) start2)))
276 (declare (fixnum slen1 slen2))
277 (if (or (minusp slen1) (minusp slen2))
278 ;;prevent endless looping later.
279 (error "Improper bounds for string comparison."))
280 (do ((index1 start1 (1+ index1))
281 (index2 start2 (1+ index2))
284 ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
285 (if (,length-test slen1 slen2) (- index1 offset1)))
286 (declare (fixnum index1 index2))
287 (setq char1 (schar string1 index1))
288 (setq char2 (schar string2 index2))
289 (if (not (char-equal char1 char2))
291 (return (- index1 offset1))
296 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
297 (declare (fixnum start1 start2))
298 (string-less-greater-equal t nil))
300 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
301 (declare (fixnum start1 start2))
302 (string-less-greater-equal nil nil))
304 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
305 (declare (fixnum start1 start2))
306 (string-less-greater-equal nil t))
308 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
309 (declare (fixnum start1 start2))
310 (string-less-greater-equal t t))
312 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
314 "Given two strings, if the first string is lexicographically less than
315 the second string, returns the longest common prefix (using char-equal)
316 of the two strings. Otherwise, returns ()."
317 (string-lessp* string1 string2 start1 end1 start2 end2))
319 (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
321 "Given two strings, if the first string is lexicographically greater than
322 the second string, returns the longest common prefix (using char-equal)
323 of the two strings. Otherwise, returns ()."
324 (string-greaterp* string1 string2 start1 end1 start2 end2))
326 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
328 "Given two strings, if the first string is lexicographically greater
329 than or equal to the second string, returns the longest common prefix
330 (using char-equal) of the two strings. Otherwise, returns ()."
331 (string-not-lessp* string1 string2 start1 end1 start2 end2))
333 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
336 "Given two strings, if the first string is lexicographically less than
337 or equal to the second string, returns the longest common prefix
338 (using char-equal) of the two strings. Otherwise, returns ()."
339 (string-not-greaterp* string1 string2 start1 end1 start2 end2))
341 (defun make-string (count &key element-type ((:initial-element fill-char)))
343 "Given a character count and an optional fill character, makes and returns
344 a new string Count long filled with the fill character."
345 (declare (fixnum count)
346 (ignore element-type))
349 (string (make-string count)))
352 (setf (schar string i) fill-char))
353 (make-string count)))
355 (flet ((%upcase (string start end)
356 (declare (string string) (index start) (type sequence-end end))
357 (let ((saved-header string))
358 (with-one-string (string start end)
359 (do ((index start (1+ index)))
360 ((= index (the fixnum end)))
361 (declare (fixnum index))
362 (setf (schar string index) (char-upcase (schar string index)))))
364 (defun string-upcase (string &key (start 0) end)
365 (%upcase (copy-seq (string string)) start end))
366 (defun nstring-upcase (string &key (start 0) end)
367 (%upcase string start end))
370 (flet ((%downcase (string start end)
371 (declare (string string) (index start) (type sequence-end end))
372 (let ((saved-header string))
373 (with-one-string (string start end)
374 (do ((index start (1+ index)))
375 ((= index (the fixnum end)))
376 (declare (fixnum index))
377 (setf (schar string index)
378 (char-downcase (schar string index)))))
380 (defun string-downcase (string &key (start 0) end)
381 (%downcase (copy-seq (string string)) start end))
382 (defun nstring-downcase (string &key (start 0) end)
383 (%downcase string start end))
386 (flet ((%capitalize (string start end)
387 (declare (string string) (index start) (type sequence-end end))
388 (let ((saved-header string))
389 (with-one-string (string start end)
390 (do ((index start (1+ index))
393 ((= index (the fixnum end)))
394 (declare (fixnum index))
395 (setq char (schar string index))
396 (cond ((not (alphanumericp char))
399 ;; CHAR is the first case-modifiable character after
400 ;; a sequence of non-case-modifiable characters.
401 (setf (schar string index) (char-upcase char))
402 (setq new-word? nil))
404 (setf (schar string index) (char-downcase char))))))
406 (defun string-capitalize (string &key (start 0) end)
407 (%capitalize (copy-seq (string string)) start end))
408 (defun nstring-capitalize (string &key (start 0) end)
409 (%capitalize string start end))
412 (defun string-left-trim (char-bag string)
414 (do ((index start (1+ index)))
415 ((or (= index (the fixnum end))
416 (not (find (schar string index) char-bag :test #'char=)))
417 (subseq (the simple-string string) index end))
418 (declare (fixnum index)))))
420 (defun string-right-trim (char-bag string)
422 (do ((index (1- (the fixnum end)) (1- index)))
424 (not (find (schar string index) char-bag :test #'char=)))
425 (subseq (the simple-string string) start (1+ index)))
426 (declare (fixnum index)))))
428 (defun string-trim (char-bag string)
430 (let* ((left-end (do ((index start (1+ index)))
431 ((or (= index (the fixnum end))
432 (not (find (schar string index)
436 (declare (fixnum index))))
437 (right-end (do ((index (1- (the fixnum end)) (1- index)))
438 ((or (< index left-end)
439 (not (find (schar string index)
443 (declare (fixnum index)))))
444 (subseq (the simple-string string) left-end right-end))))