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")
12 (eval-when (:compile-toplevel
)
13 (sb!xc
:defmacro %string
(x) `(if (stringp ,x
) ,x
(string ,x
))))
16 "Coerces X into a string. If X is a string, X is returned. If X is a
17 symbol, its name is returned. If X is a character then a one element
18 string containing that character is returned. If X cannot be coerced
19 into a string, an error occurs."
20 (declare (explicit-check))
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
'string-designator
30 :format-control
"~S is not a string designator."
31 :format-arguments
(list x
)))))
33 ;;; %CHECK-VECTOR-SEQUENCE-BOUNDS is used to verify that the START and
34 ;;; END arguments are valid bounding indices.
35 (defun %check-vector-sequence-bounds
(vector start end
)
36 (%check-vector-sequence-bounds vector start end
))
38 (eval-when (:compile-toplevel
)
39 ;;; WITH-ONE-STRING is used to set up some string hacking things. The
40 ;;; keywords are parsed, and the string is hacked into a
42 (sb!xc
:defmacro with-one-string
((string start end
) &body forms
)
43 `(let ((,string
(%string
,string
)))
44 (with-array-data ((,string
,string
)
47 :check-fill-pointer t
)
49 ;;; WITH-TWO-STRINGS is used to set up string comparison operations. The
50 ;;; keywords are parsed, and the strings are hacked into SIMPLE-STRINGs.
51 (sb!xc
:defmacro with-two-strings
(string1 string2 start1 end1 cum-offset-1
52 start2 end2
&rest forms
)
53 `(let ((,string1
(%string
,string1
))
54 (,string2
(%string
,string2
)))
55 (with-array-data ((,string1
,string1
:offset-var
,cum-offset-1
)
58 :check-fill-pointer t
)
59 (with-array-data ((,string2
,string2
)
62 :check-fill-pointer t
)
65 (sb!xc
:defmacro with-two-arg-strings
(string1 string2 start1 end1 cum-offset-1
66 start2 end2
&rest forms
)
67 `(let ((,string1
(%string
,string1
))
68 (,string2
(%string
,string2
)))
69 (with-array-data ((,string1
,string1
:offset-var
,cum-offset-1
)
72 :check-fill-pointer t
)
73 (with-array-data ((,string2
,string2
)
76 :check-fill-pointer t
)
81 (defun char (string index
)
82 "Given a string and a non-negative integer index less than the length of
83 the string, returns the character object representing the character at
84 that position in the string."
85 (declare (optimize (safety 1)))
88 (defun %charset
(string index new-el
)
89 (declare (optimize (safety 1)))
90 (setf (char string index
) new-el
))
92 (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 (declare (optimize speed
))
104 (with-two-strings string1 string2 start1 end1 nil start2 end2
105 (let ((len (- end1 start1
)))
106 (unless (= len
(- end2 start2
)) ; trivial
107 (return-from string
=* nil
))
108 ;; Optimizing the non-unicode builds is not terribly important
109 ;; because no per-character test for base/UCS4 is needed.
111 (let* ((widetag1 (%other-pointer-widetag string1
))
112 (widetag2 (%other-pointer-widetag string2
))
115 ;; The cost of WITH-PINNED-OBJECTS is near nothing on x86,
116 ;; and memcmp() is much faster except below a cutoff point.
117 ;; The threshold is higher on x86-32 because the overhead
118 ;; of a foreign call is higher due to FPU stack save/restore.
119 (if (and (= widetag1 widetag2
)
123 (#.sb
!vm
:simple-base-string-widetag
0)
124 (#.sb
!vm
:simple-character-string-widetag
2)))))
126 (return-from string
=*
127 ;; Efficiently compute byte indices. Derive-type on ASH isn't
128 ;; good enough. For 32-bit, it should be ok because
129 ;; (TYPEP (ASH ARRAY-TOTAL-SIZE-LIMIT 2) 'SB-VM:SIGNED-WORD) => T
130 ;; For 63-bit fixnums, that's false in theory, but true in practice.
131 ;; ARRAY-TOTAL-SIZE-LIMIT is too large for a 48-bit address space.
132 (macrolet ((sap (string start
)
133 `(sap+ (vector-sap (truly-the string
,string
))
136 `(truly-the sb
!vm
:signed-word
137 (ash (truly-the index
,index
) char-shift
))))
138 (declare (optimize (sb!c
:alien-funcall-saves-fp-and-pc
0)))
139 (with-pinned-objects (string1 string2
)
140 (zerop (alien-funcall
141 (extern-alien "memcmp"
142 (function int
(* char
) (* char
) long
))
143 (sap string1 start1
) (sap string2 start2
)
146 ((char-loop (type1 type2
)
147 `(return-from string
=*
148 (let ((string1 (truly-the (simple-array ,type1
1) string1
))
149 (string2 (truly-the (simple-array ,type2
1) string2
)))
150 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
151 (do ((index1 start1
(1+ index1
))
152 (index2 start2
(1+ index2
)))
154 (declare (index index1 index2
))
155 (unless (char= (schar string1 index1
)
156 (schar string2 index2
))
158 ;; On x86-64, short strings with same widetag use the general case.
159 ;; Why not always have cases for equal widetags and short strings?
160 ;; Because the code below deals with comparison when memcpy _can't_
161 ;; be used and is essential to this logic. No major speed gain is had
162 ;; with extra cases where memcpy would do, but was avoided.
163 ;; On non-x86, Lisp code is used always because I did not profile
164 ;; memcmp(), and this code is at least as good as %SP-STRING-COMPARE.
165 ;; Also, (ARRAY NIL) always punts.
167 ((= widetag1 widetag2
)
169 (#.sb
!vm
:simple-base-string-widetag
170 (char-loop base-char base-char
))
171 (#.sb
!vm
:simple-character-string-widetag
172 (char-loop character character
))))
173 ((or (and (= widetag1 sb
!vm
:simple-character-string-widetag
)
174 (= widetag2 sb
!vm
:simple-base-string-widetag
))
175 (and (= widetag2 sb
!vm
:simple-character-string-widetag
)
176 (= widetag1 sb
!vm
:simple-base-string-widetag
)
177 (progn (rotatef start1 start2
)
179 (rotatef string1 string2
)
181 (char-loop character base-char
))))))
182 (not (%sp-string-compare string1 start1 end1 string2 start2 end2
))))
184 (defun string/=* (string1 string2 start1 end1 start2 end2
)
185 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
186 (let ((comparison (%sp-string-compare string1 start1 end1
187 string2 start2 end2
)))
188 (if comparison
(- (the fixnum comparison
) offset1
)))))
190 (eval-when (:compile-toplevel
:execute
)
192 ;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
193 ;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
194 (sb!xc
:defmacro string
<>=*-body
(lessp equalp
)
195 (let ((offset1 (gensym)))
196 `(with-two-strings string1 string2 start1 end1
,offset1 start2 end2
197 (let ((index (%sp-string-compare string1 start1 end1
198 string2 start2 end2
)))
200 (cond ((= (the fixnum index
) (the fixnum end1
))
202 `(- (the fixnum index
) ,offset1
)
204 ((= (+ (the fixnum index
) (- start2 start1
))
208 `(- (the fixnum index
) ,offset1
)))
209 ((,(if lessp
'char
< 'char
>)
210 (schar string1 index
)
211 (schar string2
(+ (the fixnum index
) (- start2 start1
))))
212 (- (the fixnum index
) ,offset1
))
214 ,(if equalp
`(- (the fixnum end1
) ,offset1
) nil
))))))
217 (defun string<* (string1 string2 start1 end1 start2 end2
)
218 (declare (fixnum start1 start2
))
219 (string<>=*-body t nil
))
221 (defun string>* (string1 string2 start1 end1 start2 end2
)
222 (declare (fixnum start1 start2
))
223 (string<>=*-body nil nil
))
225 (defun string<=* (string1 string2 start1 end1 start2 end2
)
226 (declare (fixnum start1 start2
))
227 (string<>=*-body t t
))
229 (defun string>=* (string1 string2 start1 end1 start2 end2
)
230 (declare (fixnum start1 start2
))
231 (string<>=*-body nil t
))
233 (defun string< (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
234 "Given two strings, if the first string is lexicographically less than
235 the second string, returns the longest common prefix (using char=)
236 of the two strings. Otherwise, returns ()."
237 (string<* string1 string2 start1 end1 start2 end2
))
239 (defun two-arg-string< (string1 string2
)
240 (string<* string1 string2
0 nil
0 nil
))
242 (defun string> (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
243 "Given two strings, if the first string is lexicographically greater than
244 the second string, returns the longest common prefix (using char=)
245 of the two strings. Otherwise, returns ()."
246 (string>* string1 string2 start1 end1 start2 end2
))
248 (defun two-arg-string> (string1 string2
)
249 (string>* string1 string2
0 nil
0 nil
))
251 (defun string<= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
252 "Given two strings, if the first string is lexicographically less than
253 or equal to the second string, returns the longest common prefix
254 (using char=) of the two strings. Otherwise, returns ()."
255 (string<=* string1 string2 start1 end1 start2 end2
))
257 (defun two-arg-string<= (string1 string2
)
258 (string<=* string1 string2
0 nil
0 nil
))
260 (defun string>= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
261 "Given two strings, if the first string is lexicographically greater
262 than or equal to the second string, returns the longest common prefix
263 (using char=) of the two strings. Otherwise, returns ()."
264 (string>=* string1 string2 start1 end1 start2 end2
))
266 (defun two-arg-string>= (string1 string2
)
267 (string>=* string1 string2
0 nil
0 nil
))
269 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
270 ;;; an error instead of returning NIL as I would have expected.
271 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
272 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
273 ;;; this is conforming (and required) behavior, because any index
274 ;;; out of range is an error. (So there seems to be no concise and
275 ;;; efficient way to test for strings which begin with a particular
276 ;;; pattern. Alas..) -- WHN 19991206
277 (defun string= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
278 "Given two strings (string1 and string2), and optional integers start1,
279 start2, end1 and end2, compares characters in string1 to characters in
280 string2 (using char=)."
281 (string=* string1 string2 start1 end1 start2 end2
))
283 (defun two-arg-string= (string1 string2
)
284 (string=* string1 string2
0 nil
0 nil
))
286 (defun string/= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
287 "Given two strings, if the first string is not lexicographically equal
288 to the second string, returns the longest common prefix (using char=)
289 of the two strings. Otherwise, returns ()."
290 (string/=* string1 string2 start1 end1 start2 end2
))
292 (defun two-arg-string/= (string1 string2
)
293 (string/=* string1 string2
0 nil
0 nil
))
295 (eval-when (:compile-toplevel
:execute
)
297 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
298 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
299 (sb!xc
:defmacro string-not-equal-loop
(end
301 &optional
(abort-value nil abortp
))
302 (declare (fixnum end
))
303 (let ((end-test (if (= end
1)
304 `(= index1
(the fixnum end1
))
305 `(= index2
(the fixnum end2
)))))
306 `(locally (declare (inline two-arg-char-equal
))
307 (do ((index1 start1
(1+ index1
))
308 (index2 start2
(1+ index2
)))
312 (not (char-equal (schar string1 index1
)
313 (schar string2 index2
)))))
315 (declare (fixnum index1 index2
))
317 `((if (not (char-equal (schar string1 index1
)
318 (schar string2 index2
)))
319 (return ,abort-value
))))))))
323 (defun string-equal (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
324 "Given two strings (string1 and string2), and optional integers start1,
325 start2, end1 and end2, compares characters in string1 to characters in
326 string2 (using char-equal)."
327 (declare (fixnum start1 start2
))
328 (with-two-strings string1 string2 start1 end1 nil start2 end2
329 (let ((slen1 (- (the fixnum end1
) start1
))
330 (slen2 (- (the fixnum end2
) start2
)))
331 (declare (fixnum slen1 slen2
))
332 (when (= slen1 slen2
)
333 ;;return NIL immediately if lengths aren't equal.
334 (string-not-equal-loop 1 t nil
)))))
336 (defun two-arg-string-equal (string1 string2
)
337 (with-two-arg-strings string1 string2 start1 end1 nil start2 end2
338 (let ((slen1 (- (the fixnum end1
) start1
))
339 (slen2 (- (the fixnum end2
) start2
)))
340 (declare (fixnum slen1 slen2
))
341 (when (= slen1 slen2
)
342 (string-not-equal-loop 1 t nil
)))))
344 (defun string-not-equal (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
345 "Given two strings, if the first string is not lexicographically equal
346 to the second string, returns the longest common prefix (using char-equal)
347 of the two strings. Otherwise, returns ()."
348 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
349 (let ((slen1 (- end1 start1
))
350 (slen2 (- end2 start2
)))
351 (declare (fixnum slen1 slen2
))
352 (cond ((= slen1 slen2
)
353 (string-not-equal-loop 1 nil
(- index1 offset1
)))
355 (string-not-equal-loop 1 (- index1 offset1
)))
357 (string-not-equal-loop 2 (- index1 offset1
)))))))
359 (defun two-arg-string-not-equal (string1 string2
)
360 (with-two-arg-strings string1 string2 start1 end1 offset1 start2 end2
361 (let ((slen1 (- end1 start1
))
362 (slen2 (- end2 start2
)))
363 (declare (fixnum slen1 slen2
))
364 (cond ((= slen1 slen2
)
365 (string-not-equal-loop 1 nil
(- index1 offset1
)))
367 (string-not-equal-loop 1 (- index1 offset1
)))
369 (string-not-equal-loop 2 (- index1 offset1
)))))))
371 (eval-when (:compile-toplevel
:execute
)
373 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
374 ;;; and string2 and a test on the current characters from string1 and string2
375 ;;; for the following macro.
376 (defun string-less-greater-equal-tests (lessp equalp
)
379 ;; STRING-NOT-GREATERP
380 (values '<= `(not (char-greaterp char1 char2
)))
382 (values '< `(char-lessp char1 char2
)))
385 (values '>= `(not (char-lessp char1 char2
)))
387 (values '> `(char-greaterp char1 char2
)))))
389 (sb!xc
:defmacro string-less-greater-equal
(lessp equalp
)
390 (multiple-value-bind (length-test character-test
)
391 (string-less-greater-equal-tests lessp equalp
)
392 `(locally (declare (inline two-arg-char-equal
))
393 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
394 (let ((slen1 (- (the fixnum end1
) start1
))
395 (slen2 (- (the fixnum end2
) start2
)))
396 (declare (fixnum slen1 slen2
))
397 (do ((index1 start1
(1+ index1
))
398 (index2 start2
(1+ index2
))
401 ((or (= index1
(the fixnum end1
)) (= index2
(the fixnum end2
)))
402 (if (,length-test slen1 slen2
) (- index1 offset1
)))
403 (declare (fixnum index1 index2
))
404 (setq char1
(schar string1 index1
))
405 (setq char2
(schar string2 index2
))
406 (if (not (char-equal char1 char2
))
408 (return (- index1 offset1
))
413 (defun string-lessp* (string1 string2 start1 end1 start2 end2
)
414 (declare (fixnum start1 start2
))
415 (string-less-greater-equal t nil
))
417 (defun string-greaterp* (string1 string2 start1 end1 start2 end2
)
418 (declare (fixnum start1 start2
))
419 (string-less-greater-equal nil nil
))
421 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2
)
422 (declare (fixnum start1 start2
))
423 (string-less-greater-equal nil t
))
425 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2
)
426 (declare (fixnum start1 start2
))
427 (string-less-greater-equal t t
))
429 (defun string-lessp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
430 "Given two strings, if the first string is lexicographically less than
431 the second string, returns the longest common prefix (using char-equal)
432 of the two strings. Otherwise, returns ()."
433 (string-lessp* string1 string2 start1 end1 start2 end2
))
435 (defun two-arg-string-lessp (string1 string2
)
436 (string-lessp* string1 string2
0 nil
0 nil
))
438 (defun string-greaterp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
439 "Given two strings, if the first string is lexicographically greater than
440 the second string, returns the longest common prefix (using char-equal)
441 of the two strings. Otherwise, returns ()."
442 (string-greaterp* string1 string2 start1 end1 start2 end2
))
444 (defun two-arg-string-greaterp (string1 string2
)
445 (string-greaterp* string1 string2
0 nil
0 nil
))
447 (defun string-not-lessp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
448 "Given two strings, if the first string is lexicographically greater
449 than or equal to the second string, returns the longest common prefix
450 (using char-equal) of the two strings. Otherwise, returns ()."
451 (string-not-lessp* string1 string2 start1 end1 start2 end2
))
453 (defun two-arg-string-not-lessp (string1 string2
)
454 (string-not-lessp* string1 string2
0 nil
0 nil
))
456 (defun string-not-greaterp (string1 string2
&key
(start1 0) end1
(start2 0)
458 "Given two strings, if the first string is lexicographically less than
459 or equal to the second string, returns the longest common prefix
460 (using char-equal) of the two strings. Otherwise, returns ()."
461 (string-not-greaterp* string1 string2 start1 end1 start2 end2
))
464 (defun two-arg-string-not-greaterp (string1 string2
)
465 (string-not-greaterp* string1 string2
0 nil
0 nil
))
467 (defun make-string (count &key
468 (element-type 'character
)
469 ((:initial-element fill-char
)))
470 "Given a character count and an optional fill character, makes and returns a
471 new string COUNT long filled with the fill character."
472 (declare (index count
))
473 (declare (explicit-check))
474 ;; FIXME: while this is a correct implementation relying on an IR1 transform,
475 ;; it would be better if in the following example (assuming NOTINLINE):
476 ;; (MAKE-STRING 1000 :ELEMENT-TYPE 'BIT :INITIAL-element #\a)
477 ;; we could report that "BIT is not a subtype of CHARACTER"
478 ;; instead of "#\a is not of type BIT". Additionally, in this case:
479 ;; (MAKE-STRING 200000000 :ELEMENT-TYPE 'WORD :INITIAL-ELEMENT #\a)
480 ;; the error reported is heap exhaustion rather than type mismatch.
482 (make-string count
:element-type element-type
483 :initial-element
(the character fill-char
))
484 (make-string count
:element-type element-type
)))
486 (flet ((%upcase
(string start end
)
487 (declare (string string
) (index start
) (type sequence-end end
))
488 (let ((saved-header string
))
489 (with-one-string (string start end
)
490 (do ((index start
(1+ index
)))
491 ((= index
(the fixnum end
)))
492 (declare (fixnum index
))
493 (setf (schar string index
) (char-upcase (schar string index
)))))
495 (defun string-upcase (string &key
(start 0) end
)
496 (%upcase
(copy-seq (string string
)) start end
))
497 (defun nstring-upcase (string &key
(start 0) end
)
498 (%upcase string start end
))
501 (flet ((%downcase
(string start end
)
502 (declare (string string
) (index start
) (type sequence-end end
))
503 (let ((saved-header string
))
504 (with-one-string (string start end
)
505 (do ((index start
(1+ index
)))
506 ((= index
(the fixnum end
)))
507 (declare (fixnum index
))
508 (setf (schar string index
)
509 (char-downcase (schar string index
)))))
511 (defun string-downcase (string &key
(start 0) end
)
512 (%downcase
(copy-seq (string string
)) start end
))
513 (defun nstring-downcase (string &key
(start 0) end
)
514 (%downcase string start end
))
516 (flet ((%capitalize
(string start end
)
517 (declare (string string
) (index start
) (type sequence-end end
))
518 (let ((saved-header string
))
519 (with-one-string (string start end
)
520 (do ((index start
(1+ index
))
523 ((= index
(the fixnum end
)))
524 (declare (fixnum index
))
525 (setq char
(schar string index
))
526 (cond ((not (alphanumericp char
))
529 ;; CHAR is the first case-modifiable character after
530 ;; a sequence of non-case-modifiable characters.
531 (setf (schar string index
) (char-upcase char
))
532 (setq new-word? nil
))
534 (setf (schar string index
) (char-downcase char
))))))
536 (defun string-capitalize (string &key
(start 0) end
)
537 (%capitalize
(copy-seq (string string
)) start end
))
538 (defun nstring-capitalize (string &key
(start 0) end
)
539 (%capitalize string start end
))
543 (defun generic-string-trim (char-bag string left-p right-p
)
544 (let ((header (%string string
)))
545 (with-array-data ((string header
)
548 :check-fill-pointer t
)
549 (let* ((left-end (if left-p
550 (do ((index start
(1+ index
)))
551 ((or (= index
(the fixnum end
))
552 (not (find (schar string index
)
556 (declare (fixnum index
)))
558 (right-end (if right-p
559 (do ((index (1- (the fixnum end
)) (1- index
)))
560 ((or (< index left-end
)
561 (not (find (schar string index
)
565 (declare (fixnum index
)))
567 (if (and (eql left-end start
)
570 (subseq (the simple-string string
) left-end right-end
))))))
572 (defun string-left-trim (char-bag string
)
573 (generic-string-trim char-bag string t nil
))
575 (defun string-right-trim (char-bag string
)
576 (generic-string-trim char-bag string nil t
))
578 (defun string-trim (char-bag string
)
579 (generic-string-trim char-bag string t t
))