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
))))
17 "Coerces X into a string. If X is a string, X is returned. If X is a
18 symbol, its name 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
'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
)
66 (defun char (string index
)
68 "Given a string and a non-negative integer index less than the length of
69 the string, returns the character object representing the character at
70 that position in the string."
71 (declare (optimize (safety 1)))
74 (defun %charset
(string index new-el
)
75 (declare (optimize (safety 1)))
76 (setf (char string index
) new-el
))
78 (defun schar (string index
)
80 "SCHAR returns the character object at an indexed position in a string
81 just as CHAR does, except the string must be a simple-string."
82 (declare (optimize (safety 1)))
85 (defun %scharset
(string index new-el
)
86 (declare (optimize (safety 1)))
87 (setf (schar string index
) new-el
))
89 (defun string=* (string1 string2 start1 end1 start2 end2
)
90 (declare (optimize speed
))
91 (with-two-strings string1 string2 start1 end1 nil start2 end2
92 (let ((len (- end1 start1
)))
93 (unless (= len
(- end2 start2
)) ; trivial
94 (return-from string
=* nil
))
95 ;; Optimizing the non-unicode builds is not terribly important
96 ;; because no per-character test for base/UCS4 is needed.
98 (let* ((widetag1 (%other-pointer-widetag string1
))
99 (widetag2 (%other-pointer-widetag string2
))
102 ;; The cost of WITH-PINNED-OBJECTS is near nothing on x86,
103 ;; and memcmp() is much faster except below a cutoff point.
104 ;; The threshold is higher on x86-32 because the overhead
105 ;; of a foreign call is higher due to FPU stack save/restore.
106 (if (and (= widetag1 widetag2
)
110 (#.sb
!vm
:simple-base-string-widetag
0)
111 (#.sb
!vm
:simple-character-string-widetag
2)))))
113 (return-from string
=*
114 ;; Efficiently compute byte indices. Derive-type on ASH isn't
115 ;; good enough. For 32-bit, it should be ok because
116 ;; (TYPEP (ASH ARRAY-TOTAL-SIZE-LIMIT 2) 'SB-VM:SIGNED-WORD) => T
117 ;; For 63-bit fixnums, that's false in theory, but true in practice.
118 ;; ARRAY-TOTAL-SIZE-LIMIT is too large for a 48-bit address space.
119 (macrolet ((sap (string start
)
120 `(sap+ (vector-sap (truly-the string
,string
))
123 `(truly-the sb
!vm
:signed-word
124 (ash (truly-the index
,index
) char-shift
))))
125 (declare (optimize (sb!c
:alien-funcall-saves-fp-and-pc
0)))
126 (with-pinned-objects (string1 string2
)
127 (zerop (alien-funcall
128 (extern-alien "memcmp"
129 (function int
(* char
) (* char
) long
))
130 (sap string1 start1
) (sap string2 start2
)
133 ((char-loop (type1 type2
)
134 `(return-from string
=*
135 (let ((string1 (truly-the (simple-array ,type1
1) string1
))
136 (string2 (truly-the (simple-array ,type2
1) string2
)))
137 (declare (optimize (sb!c
::insert-array-bounds-checks
0)))
138 (do ((index1 start1
(1+ index1
))
139 (index2 start2
(1+ index2
)))
141 (declare (index index1 index2
))
142 (unless (char= (schar string1 index1
)
143 (schar string2 index2
))
145 ;; On x86-64, short strings with same widetag use the general case.
146 ;; Why not always have cases for equal widetags and short strings?
147 ;; Because the code below deals with comparison when memcpy _can't_
148 ;; be used and is essential to this logic. No major speed gain is had
149 ;; with extra cases where memcpy would do, but was avoided.
150 ;; On non-x86, Lisp code is used always because I did not profile
151 ;; memcmp(), and this code is at least as good as %SP-STRING-COMPARE.
152 ;; Also, (ARRAY NIL) always punts.
154 ((= widetag1 widetag2
)
156 (#.sb
!vm
:simple-base-string-widetag
157 (char-loop base-char base-char
))
158 (#.sb
!vm
:simple-character-string-widetag
159 (char-loop character character
))))
160 ((or (and (= widetag1 sb
!vm
:simple-character-string-widetag
)
161 (= widetag2 sb
!vm
:simple-base-string-widetag
))
162 (and (= widetag2 sb
!vm
:simple-character-string-widetag
)
163 (= widetag1 sb
!vm
:simple-base-string-widetag
)
164 (progn (rotatef start1 start2
)
166 (rotatef string1 string2
)
168 (char-loop character base-char
))))))
169 (not (%sp-string-compare string1 start1 end1 string2 start2 end2
))))
171 (defun string/=* (string1 string2 start1 end1 start2 end2
)
172 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
173 (let ((comparison (%sp-string-compare string1 start1 end1
174 string2 start2 end2
)))
175 (if comparison
(- (the fixnum comparison
) offset1
)))))
177 (eval-when (:compile-toplevel
:execute
)
179 ;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
180 ;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
181 (sb!xc
:defmacro string
<>=*-body
(lessp equalp
)
182 (let ((offset1 (gensym)))
183 `(with-two-strings string1 string2 start1 end1
,offset1 start2 end2
184 (let ((index (%sp-string-compare string1 start1 end1
185 string2 start2 end2
)))
187 (cond ((= (the fixnum index
) (the fixnum end1
))
189 `(- (the fixnum index
) ,offset1
)
191 ((= (+ (the fixnum index
) (- start2 start1
))
195 `(- (the fixnum index
) ,offset1
)))
196 ((,(if lessp
'char
< 'char
>)
197 (schar string1 index
)
198 (schar string2
(+ (the fixnum index
) (- start2 start1
))))
199 (- (the fixnum index
) ,offset1
))
201 ,(if equalp
`(- (the fixnum end1
) ,offset1
) nil
))))))
204 (defun string<* (string1 string2 start1 end1 start2 end2
)
205 (declare (fixnum start1 start2
))
206 (string<>=*-body t nil
))
208 (defun string>* (string1 string2 start1 end1 start2 end2
)
209 (declare (fixnum start1 start2
))
210 (string<>=*-body nil nil
))
212 (defun string<=* (string1 string2 start1 end1 start2 end2
)
213 (declare (fixnum start1 start2
))
214 (string<>=*-body t t
))
216 (defun string>=* (string1 string2 start1 end1 start2 end2
)
217 (declare (fixnum start1 start2
))
218 (string<>=*-body nil t
))
220 (defun string< (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
222 "Given two strings, if the first string is lexicographically less than
223 the second string, returns the longest common prefix (using char=)
224 of the two strings. Otherwise, returns ()."
225 (string<* string1 string2 start1 end1 start2 end2
))
227 (defun string> (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
229 "Given two strings, if the first string is lexicographically greater than
230 the second string, returns the longest common prefix (using char=)
231 of the two strings. Otherwise, returns ()."
232 (string>* string1 string2 start1 end1 start2 end2
))
234 (defun string<= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
236 "Given two strings, if the first string is lexicographically less than
237 or equal to the second string, returns the longest common prefix
238 (using char=) of the two strings. Otherwise, returns ()."
239 (string<=* string1 string2 start1 end1 start2 end2
))
241 (defun string>= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
243 "Given two strings, if the first string is lexicographically greater
244 than or equal to the second string, returns the longest common prefix
245 (using char=) of the two strings. Otherwise, returns ()."
246 (string>=* string1 string2 start1 end1 start2 end2
))
248 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
249 ;;; an error instead of returning NIL as I would have expected.
250 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
251 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
252 ;;; this is conforming (and required) behavior, because any index
253 ;;; out of range is an error. (So there seems to be no concise and
254 ;;; efficient way to test for strings which begin with a particular
255 ;;; pattern. Alas..) -- WHN 19991206
256 (defun string= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
258 "Given two strings (string1 and string2), and optional integers start1,
259 start2, end1 and end2, compares characters in string1 to characters in
260 string2 (using char=)."
261 (string=* string1 string2 start1 end1 start2 end2
))
263 (defun string/= (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
265 "Given two strings, if the first string is not lexicographically equal
266 to the second string, returns the longest common prefix (using char=)
267 of the two strings. Otherwise, returns ()."
268 (string/=* string1 string2 start1 end1 start2 end2
))
270 (eval-when (:compile-toplevel
:execute
)
272 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
273 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
274 (sb!xc
:defmacro string-not-equal-loop
(end
276 &optional
(abort-value nil abortp
))
277 (declare (fixnum end
))
278 (let ((end-test (if (= end
1)
279 `(= index1
(the fixnum end1
))
280 `(= index2
(the fixnum end2
)))))
281 `(do ((index1 start1
(1+ index1
))
282 (index2 start2
(1+ index2
)))
286 (not (char-equal (schar string1 index1
)
287 (schar string2 index2
)))))
289 (declare (fixnum index1 index2
))
291 `((if (not (char-equal (schar string1 index1
)
292 (schar string2 index2
)))
293 (return ,abort-value
)))))))
297 (defun string-equal (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
299 "Given two strings (string1 and string2), and optional integers start1,
300 start2, end1 and end2, compares characters in string1 to characters in
301 string2 (using char-equal)."
302 (declare (fixnum start1 start2
))
303 (with-two-strings string1 string2 start1 end1 nil start2 end2
304 (let ((slen1 (- (the fixnum end1
) start1
))
305 (slen2 (- (the fixnum end2
) start2
)))
306 (declare (fixnum slen1 slen2
))
308 ;;return () immediately if lengths aren't equal.
309 (string-not-equal-loop 1 t nil
)))))
311 (defun string-not-equal (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
313 "Given two strings, if the first string is not lexicographically equal
314 to the second string, returns the longest common prefix (using char-equal)
315 of the two strings. Otherwise, returns ()."
316 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
317 (let ((slen1 (- end1 start1
))
318 (slen2 (- end2 start2
)))
319 (declare (fixnum slen1 slen2
))
320 (cond ((= slen1 slen2
)
321 (string-not-equal-loop 1 nil
(- index1 offset1
)))
323 (string-not-equal-loop 1 (- index1 offset1
)))
325 (string-not-equal-loop 2 (- index1 offset1
)))))))
327 (eval-when (:compile-toplevel
:execute
)
329 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
330 ;;; and string2 and a test on the current characters from string1 and string2
331 ;;; for the following macro.
332 (defun string-less-greater-equal-tests (lessp equalp
)
335 ;; STRING-NOT-GREATERP
336 (values '<= `(not (char-greaterp char1 char2
)))
338 (values '< `(char-lessp char1 char2
)))
341 (values '>= `(not (char-lessp char1 char2
)))
343 (values '> `(char-greaterp char1 char2
)))))
345 (sb!xc
:defmacro string-less-greater-equal
(lessp equalp
)
346 (multiple-value-bind (length-test character-test
)
347 (string-less-greater-equal-tests lessp equalp
)
348 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
349 (let ((slen1 (- (the fixnum end1
) start1
))
350 (slen2 (- (the fixnum end2
) start2
)))
351 (declare (fixnum slen1 slen2
))
352 (do ((index1 start1
(1+ index1
))
353 (index2 start2
(1+ index2
))
356 ((or (= index1
(the fixnum end1
)) (= index2
(the fixnum end2
)))
357 (if (,length-test slen1 slen2
) (- index1 offset1
)))
358 (declare (fixnum index1 index2
))
359 (setq char1
(schar string1 index1
))
360 (setq char2
(schar string2 index2
))
361 (if (not (char-equal char1 char2
))
363 (return (- index1 offset1
))
368 (defun string-lessp* (string1 string2 start1 end1 start2 end2
)
369 (declare (fixnum start1 start2
))
370 (string-less-greater-equal t nil
))
372 (defun string-greaterp* (string1 string2 start1 end1 start2 end2
)
373 (declare (fixnum start1 start2
))
374 (string-less-greater-equal nil nil
))
376 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2
)
377 (declare (fixnum start1 start2
))
378 (string-less-greater-equal nil t
))
380 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2
)
381 (declare (fixnum start1 start2
))
382 (string-less-greater-equal t t
))
384 (defun string-lessp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
386 "Given two strings, if the first string is lexicographically less than
387 the second string, returns the longest common prefix (using char-equal)
388 of the two strings. Otherwise, returns ()."
389 (string-lessp* string1 string2 start1 end1 start2 end2
))
391 (defun string-greaterp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
393 "Given two strings, if the first string is lexicographically greater than
394 the second string, returns the longest common prefix (using char-equal)
395 of the two strings. Otherwise, returns ()."
396 (string-greaterp* string1 string2 start1 end1 start2 end2
))
398 (defun string-not-lessp (string1 string2
&key
(start1 0) end1
(start2 0) end2
)
400 "Given two strings, if the first string is lexicographically greater
401 than or equal to the second string, returns the longest common prefix
402 (using char-equal) of the two strings. Otherwise, returns ()."
403 (string-not-lessp* string1 string2 start1 end1 start2 end2
))
405 (defun string-not-greaterp (string1 string2
&key
(start1 0) end1
(start2 0)
408 "Given two strings, if the first string is lexicographically less than
409 or equal to the second string, returns the longest common prefix
410 (using char-equal) of the two strings. Otherwise, returns ()."
411 (string-not-greaterp* string1 string2 start1 end1 start2 end2
))
413 (defun make-string (count &key
414 (element-type 'character
)
415 ((:initial-element fill-char
)))
417 "Given a character count and an optional fill character, makes and returns a
418 new string COUNT long filled with the fill character."
419 (declare (index count
))
420 ;; FIXME: while this is a correct implementation relying on an IR1 transform,
421 ;; it would be better if in the following example (assuming NOTINLINE):
422 ;; (MAKE-STRING 1000 :ELEMENT-TYPE 'BIT :INITIAL-element #\a)
423 ;; we could report that "BIT is not a subtype of CHARACTER"
424 ;; instead of "#\a is not of type BIT". Additionally, in this case:
425 ;; (MAKE-STRING 200000000 :ELEMENT-TYPE 'WORD :INITIAL-ELEMENT #\a)
426 ;; the error reported is heap exhaustion rather than type mismatch.
428 (make-string count
:element-type element-type
429 :initial-element
(the character fill-char
))
430 (make-string count
:element-type element-type
)))
432 (flet ((%upcase
(string start end
)
433 (declare (string string
) (index start
) (type sequence-end end
))
434 (let ((saved-header string
))
435 (with-one-string (string start end
)
436 (do ((index start
(1+ index
)))
437 ((= index
(the fixnum end
)))
438 (declare (fixnum index
))
439 (setf (schar string index
) (char-upcase (schar string index
)))))
441 (defun string-upcase (string &key
(start 0) end
)
442 (%upcase
(copy-seq (string string
)) start end
))
443 (defun nstring-upcase (string &key
(start 0) end
)
444 (%upcase string start end
))
447 (flet ((%downcase
(string start end
)
448 (declare (string string
) (index start
) (type sequence-end end
))
449 (let ((saved-header string
))
450 (with-one-string (string start end
)
451 (do ((index start
(1+ index
)))
452 ((= index
(the fixnum end
)))
453 (declare (fixnum index
))
454 (setf (schar string index
)
455 (char-downcase (schar string index
)))))
457 (defun string-downcase (string &key
(start 0) end
)
458 (%downcase
(copy-seq (string string
)) start end
))
459 (defun nstring-downcase (string &key
(start 0) end
)
460 (%downcase string start end
))
463 ;; Moved from 'target-char' because of inline-ness
464 (declaim (inline alphanumericp
))
465 (defun alphanumericp (char)
467 "Given a character-object argument, ALPHANUMERICP returns T if the argument
468 is either numeric or alphabetic."
469 (let ((gc (ucd-general-category char
)))
473 (flet ((%capitalize
(string start end
)
474 (declare (string string
) (index start
) (type sequence-end end
))
475 (let ((saved-header string
))
476 (with-one-string (string start end
)
477 (do ((index start
(1+ index
))
480 ((= index
(the fixnum end
)))
481 (declare (fixnum index
))
482 (setq char
(schar string index
))
483 (cond ((not (alphanumericp char
))
486 ;; CHAR is the first case-modifiable character after
487 ;; a sequence of non-case-modifiable characters.
488 (setf (schar string index
) (char-upcase char
))
489 (setq new-word? nil
))
491 (setf (schar string index
) (char-downcase char
))))))
493 (defun string-capitalize (string &key
(start 0) end
)
494 (%capitalize
(copy-seq (string string
)) start end
))
495 (defun nstring-capitalize (string &key
(start 0) end
)
496 (%capitalize string start end
))
499 (defun generic-string-trim (char-bag string left-p right-p
)
500 (let ((header (%string string
)))
501 (with-array-data ((string header
)
504 :check-fill-pointer t
)
505 (let* ((left-end (if left-p
506 (do ((index start
(1+ index
)))
507 ((or (= index
(the fixnum end
))
508 (not (find (schar string index
)
512 (declare (fixnum index
)))
514 (right-end (if right-p
515 (do ((index (1- (the fixnum end
)) (1- index
)))
516 ((or (< index left-end
)
517 (not (find (schar string index
)
521 (declare (fixnum index
)))
523 (if (and (eql left-end start
)
526 (subseq (the simple-string string
) left-end right-end
))))))
528 (defun string-left-trim (char-bag string
)
529 (generic-string-trim char-bag string t nil
))
531 (defun string-right-trim (char-bag string
)
532 (generic-string-trim char-bag string nil t
))
534 (defun string-trim (char-bag string
)
535 (generic-string-trim char-bag string t t
))