Speed up array word size calculation.
[sbcl.git] / src / code / string.lisp
blobabc35c887f190c2cb9ba422e77812c46205497be
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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))))
15 (defun string (x)
16 #!+sb-doc
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."
21 (declare (explicit-check))
22 (cond ((stringp x) x)
23 ((symbolp x) (symbol-name x))
24 ((characterp x)
25 (let ((res (make-string 1)))
26 (setf (schar res 0) x) res))
28 (error 'simple-type-error
29 :datum x
30 :expected-type 'string-designator
31 :format-control "~S is not a string designator."
32 :format-arguments (list x)))))
34 ;;; %CHECK-VECTOR-SEQUENCE-BOUNDS is used to verify that the START and
35 ;;; END arguments are valid bounding indices.
36 (defun %check-vector-sequence-bounds (vector start end)
37 (%check-vector-sequence-bounds vector start end))
39 (eval-when (:compile-toplevel)
40 ;;; WITH-ONE-STRING is used to set up some string hacking things. The
41 ;;; keywords are parsed, and the string is hacked into a
42 ;;; simple-string.
43 (sb!xc:defmacro with-one-string ((string start end) &body forms)
44 `(let ((,string (%string ,string)))
45 (with-array-data ((,string ,string)
46 (,start ,start)
47 (,end ,end)
48 :check-fill-pointer t)
49 ,@forms)))
50 ;;; WITH-TWO-STRINGS is used to set up string comparison operations. The
51 ;;; keywords are parsed, and the strings are hacked into SIMPLE-STRINGs.
52 (sb!xc:defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
53 start2 end2 &rest forms)
54 `(let ((,string1 (%string ,string1))
55 (,string2 (%string ,string2)))
56 (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
57 (,start1 ,start1)
58 (,end1 ,end1)
59 :check-fill-pointer t)
60 (with-array-data ((,string2 ,string2)
61 (,start2 ,start2)
62 (,end2 ,end2)
63 :check-fill-pointer t)
64 ,@forms))))
66 (sb!xc:defmacro with-two-arg-strings (string1 string2 end1 end2 cum-offset-1
67 &rest forms)
68 (let ((start-var (gensym)))
69 `(let ((,string1 (%string ,string1))
70 (,string2 (%string ,string2)))
71 (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
72 (,start-var)
73 (,end1)
74 :check-fill-pointer t)
75 (declare (ignore ,start-var))
76 (with-array-data ((,string2 ,string2)
77 (,start-var)
78 (,end2)
79 :check-fill-pointer t)
80 (declare (ignore ,start-var))
81 ,@forms)))))
83 ) ; EVAL-WHEN
85 (defun char (string index)
86 #!+sb-doc
87 "Given a string and a non-negative integer index less than the length of
88 the string, returns the character object representing the character at
89 that position in the string."
90 (declare (optimize (safety 1)))
91 (char string index))
93 (defun %charset (string index new-el)
94 (declare (optimize (safety 1)))
95 (setf (char string index) new-el))
97 (defun schar (string index)
98 #!+sb-doc
99 "SCHAR returns the character object at an indexed position in a string
100 just as CHAR does, except the string must be a simple-string."
101 (declare (optimize (safety 1)))
102 (schar string index))
104 (defun %scharset (string index new-el)
105 (declare (optimize (safety 1)))
106 (setf (schar string index) new-el))
108 (defun string=* (string1 string2 start1 end1 start2 end2)
109 (declare (optimize speed))
110 (with-two-strings string1 string2 start1 end1 nil start2 end2
111 (let ((len (- end1 start1)))
112 (unless (= len (- end2 start2)) ; trivial
113 (return-from string=* nil))
114 ;; Optimizing the non-unicode builds is not terribly important
115 ;; because no per-character test for base/UCS4 is needed.
116 #!+sb-unicode
117 (let* ((widetag1 (%other-pointer-widetag string1))
118 (widetag2 (%other-pointer-widetag string2))
119 (char-shift
120 #!+(or x86 x86-64)
121 ;; The cost of WITH-PINNED-OBJECTS is near nothing on x86,
122 ;; and memcmp() is much faster except below a cutoff point.
123 ;; The threshold is higher on x86-32 because the overhead
124 ;; of a foreign call is higher due to FPU stack save/restore.
125 (if (and (= widetag1 widetag2)
126 (>= len #!+x86 16
127 #!+x86-64 8))
128 (case widetag1
129 (#.sb!vm:simple-base-string-widetag 0)
130 (#.sb!vm:simple-character-string-widetag 2)))))
131 (when char-shift
132 (return-from string=*
133 ;; Efficiently compute byte indices. Derive-type on ASH isn't
134 ;; good enough. For 32-bit, it should be ok because
135 ;; (TYPEP (ASH ARRAY-TOTAL-SIZE-LIMIT 2) 'SB-VM:SIGNED-WORD) => T
136 ;; For 63-bit fixnums, that's false in theory, but true in practice.
137 ;; ARRAY-TOTAL-SIZE-LIMIT is too large for a 48-bit address space.
138 (macrolet ((sap (string start)
139 `(sap+ (vector-sap (truly-the string ,string))
140 (scale ,start)))
141 (scale (index)
142 `(truly-the sb!vm:signed-word
143 (ash (truly-the index ,index) char-shift))))
144 (declare (optimize (sb!c:alien-funcall-saves-fp-and-pc 0)))
145 (with-pinned-objects (string1 string2)
146 (zerop (alien-funcall
147 (extern-alien "memcmp"
148 (function int (* char) (* char) long))
149 (sap string1 start1) (sap string2 start2)
150 (scale len)))))))
151 (macrolet
152 ((char-loop (type1 type2)
153 `(return-from string=*
154 (let ((string1 (truly-the (simple-array ,type1 1) string1))
155 (string2 (truly-the (simple-array ,type2 1) string2)))
156 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
157 (do ((index1 start1 (1+ index1))
158 (index2 start2 (1+ index2)))
159 ((>= index1 end1) t)
160 (declare (index index1 index2))
161 (unless (char= (schar string1 index1)
162 (schar string2 index2))
163 (return nil)))))))
164 ;; On x86-64, short strings with same widetag use the general case.
165 ;; Why not always have cases for equal widetags and short strings?
166 ;; Because the code below deals with comparison when memcpy _can't_
167 ;; be used and is essential to this logic. No major speed gain is had
168 ;; with extra cases where memcpy would do, but was avoided.
169 ;; On non-x86, Lisp code is used always because I did not profile
170 ;; memcmp(), and this code is at least as good as %SP-STRING-COMPARE.
171 ;; Also, (ARRAY NIL) always punts.
172 (cond #!-x86-64
173 ((= widetag1 widetag2)
174 (case widetag1
175 (#.sb!vm:simple-base-string-widetag
176 (char-loop base-char base-char))
177 (#.sb!vm:simple-character-string-widetag
178 (char-loop character character))))
179 ((or (and (= widetag1 sb!vm:simple-character-string-widetag)
180 (= widetag2 sb!vm:simple-base-string-widetag))
181 (and (= widetag2 sb!vm:simple-character-string-widetag)
182 (= widetag1 sb!vm:simple-base-string-widetag)
183 (progn (rotatef start1 start2)
184 (rotatef end1 end2)
185 (rotatef string1 string2)
186 t)))
187 (char-loop character base-char))))))
188 (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
190 (defun string/=* (string1 string2 start1 end1 start2 end2)
191 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
192 (let ((comparison (%sp-string-compare string1 start1 end1
193 string2 start2 end2)))
194 (if comparison (- (the fixnum comparison) offset1)))))
196 (eval-when (:compile-toplevel :execute)
198 ;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
199 ;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
200 (sb!xc:defmacro string<>=*-body (lessp equalp)
201 (let ((offset1 (gensym)))
202 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
203 (let ((index (%sp-string-compare string1 start1 end1
204 string2 start2 end2)))
205 (if index
206 (cond ((= (the fixnum index) (the fixnum end1))
207 ,(if lessp
208 `(- (the fixnum index) ,offset1)
209 `nil))
210 ((= (+ (the fixnum index) (- start2 start1))
211 (the fixnum end2))
212 ,(if lessp
213 `nil
214 `(- (the fixnum index) ,offset1)))
215 ((,(if lessp 'char< 'char>)
216 (schar string1 index)
217 (schar string2 (+ (the fixnum index) (- start2 start1))))
218 (- (the fixnum index) ,offset1))
219 (t nil))
220 ,(if equalp `(- (the fixnum end1) ,offset1) nil))))))
221 ) ; EVAL-WHEN
223 (defun string<* (string1 string2 start1 end1 start2 end2)
224 (declare (fixnum start1 start2))
225 (string<>=*-body t nil))
227 (defun string>* (string1 string2 start1 end1 start2 end2)
228 (declare (fixnum start1 start2))
229 (string<>=*-body nil nil))
231 (defun string<=* (string1 string2 start1 end1 start2 end2)
232 (declare (fixnum start1 start2))
233 (string<>=*-body t t))
235 (defun string>=* (string1 string2 start1 end1 start2 end2)
236 (declare (fixnum start1 start2))
237 (string<>=*-body nil t))
239 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
240 #!+sb-doc
241 "Given two strings, if the first string is lexicographically less than
242 the second string, returns the longest common prefix (using char=)
243 of the two strings. Otherwise, returns ()."
244 (string<* string1 string2 start1 end1 start2 end2))
246 (defun two-arg-string< (string1 string2)
247 (string<* string1 string2 0 nil 0 nil))
249 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
250 #!+sb-doc
251 "Given two strings, if the first string is lexicographically greater than
252 the second string, returns the longest common prefix (using char=)
253 of the two strings. Otherwise, returns ()."
254 (string>* string1 string2 start1 end1 start2 end2))
256 (defun two-arg-string> (string1 string2)
257 (string>* string1 string2 0 nil 0 nil))
259 (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
260 #!+sb-doc
261 "Given two strings, if the first string is lexicographically less than
262 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 (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
270 #!+sb-doc
271 "Given two strings, if the first string is lexicographically greater
272 than or equal to the second string, returns the longest common prefix
273 (using char=) of the two strings. Otherwise, returns ()."
274 (string>=* string1 string2 start1 end1 start2 end2))
276 (defun two-arg-string>= (string1 string2)
277 (string>=* string1 string2 0 nil 0 nil))
279 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
280 ;;; an error instead of returning NIL as I would have expected.
281 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
282 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
283 ;;; this is conforming (and required) behavior, because any index
284 ;;; out of range is an error. (So there seems to be no concise and
285 ;;; efficient way to test for strings which begin with a particular
286 ;;; pattern. Alas..) -- WHN 19991206
287 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
288 #!+sb-doc
289 "Given two strings (string1 and string2), and optional integers start1,
290 start2, end1 and end2, compares characters in string1 to characters in
291 string2 (using char=)."
292 (string=* string1 string2 start1 end1 start2 end2))
294 (defun two-arg-string= (string1 string2)
295 (string=* string1 string2 0 nil 0 nil))
297 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
298 #!+sb-doc
299 "Given two strings, if the first string is not lexicographically equal
300 to the second string, returns the longest common prefix (using char=)
301 of the two strings. Otherwise, returns ()."
302 (string/=* string1 string2 start1 end1 start2 end2))
304 (defun two-arg-string/= (string1 string2)
305 (string/=* string1 string2 0 nil 0 nil))
307 (eval-when (:compile-toplevel :execute)
309 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
310 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
311 (sb!xc:defmacro string-not-equal-loop (end
312 end-value
313 &optional (abort-value nil abortp))
314 (declare (fixnum end))
315 (let ((end-test (if (= end 1)
316 `(= index1 (the fixnum end1))
317 `(= index2 (the fixnum end2)))))
318 `(do ((index1 start1 (1+ index1))
319 (index2 start2 (1+ index2)))
320 (,(if abortp
321 end-test
322 `(or ,end-test
323 (not (char-equal (schar string1 index1)
324 (schar string2 index2)))))
325 ,end-value)
326 (declare (fixnum index1 index2))
327 ,@(if abortp
328 `((if (not (char-equal (schar string1 index1)
329 (schar string2 index2)))
330 (return ,abort-value)))))))
332 ) ; EVAL-WHEN
334 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
335 #!+sb-doc
336 "Given two strings (string1 and string2), and optional integers start1,
337 start2, end1 and end2, compares characters in string1 to characters in
338 string2 (using char-equal)."
339 (declare (fixnum start1 start2))
340 (with-two-strings string1 string2 start1 end1 nil start2 end2
341 (let ((slen1 (- (the fixnum end1) start1))
342 (slen2 (- (the fixnum end2) start2)))
343 (declare (fixnum slen1 slen2))
344 (when (= slen1 slen2)
345 ;;return NIL immediately if lengths aren't equal.
346 (string-not-equal-loop 1 t nil)))))
348 (defun two-arg-string-equal (string1 string2)
349 (let ((start1 0)
350 (start2 0))
351 (with-two-arg-strings string1 string2 end1 end2 nil
352 (when (= end1 end2)
353 (string-not-equal-loop 1 t nil)))))
355 (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
356 #!+sb-doc
357 "Given two strings, if the first string is not lexicographically equal
358 to the second string, returns the longest common prefix (using char-equal)
359 of the two strings. Otherwise, returns ()."
360 (with-two-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)))
366 ((< slen1 slen2)
367 (string-not-equal-loop 1 (- index1 offset1)))
369 (string-not-equal-loop 2 (- index1 offset1)))))))
371 (defun two-arg-string-not-equal (string1 string2)
372 (let ((start1 0)
373 (start2 0))
374 (with-two-arg-strings string1 string2 end1 end2 offset1
375 (cond ((= end1 end2)
376 (string-not-equal-loop 1 nil (- index1 offset1)))
377 ((< end1 end2)
378 (string-not-equal-loop 1 (- index1 offset1)))
380 (string-not-equal-loop 2 (- index1 offset1)))))))
382 (eval-when (:compile-toplevel :execute)
384 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
385 ;;; and string2 and a test on the current characters from string1 and string2
386 ;;; for the following macro.
387 (defun string-less-greater-equal-tests (lessp equalp)
388 (if lessp
389 (if equalp
390 ;; STRING-NOT-GREATERP
391 (values '<= `(not (char-greaterp char1 char2)))
392 ;; STRING-LESSP
393 (values '< `(char-lessp char1 char2)))
394 (if equalp
395 ;; STRING-NOT-LESSP
396 (values '>= `(not (char-lessp char1 char2)))
397 ;; STRING-GREATERP
398 (values '> `(char-greaterp char1 char2)))))
400 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
401 (multiple-value-bind (length-test character-test)
402 (string-less-greater-equal-tests lessp equalp)
403 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
404 (let ((slen1 (- (the fixnum end1) start1))
405 (slen2 (- (the fixnum end2) start2)))
406 (declare (fixnum slen1 slen2))
407 (do ((index1 start1 (1+ index1))
408 (index2 start2 (1+ index2))
409 (char1)
410 (char2))
411 ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
412 (if (,length-test slen1 slen2) (- index1 offset1)))
413 (declare (fixnum index1 index2))
414 (setq char1 (schar string1 index1))
415 (setq char2 (schar string2 index2))
416 (if (not (char-equal char1 char2))
417 (if ,character-test
418 (return (- index1 offset1))
419 (return ()))))))))
421 ) ; EVAL-WHEN
423 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
424 (declare (fixnum start1 start2))
425 (string-less-greater-equal t nil))
427 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
428 (declare (fixnum start1 start2))
429 (string-less-greater-equal nil nil))
431 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
432 (declare (fixnum start1 start2))
433 (string-less-greater-equal nil t))
435 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
436 (declare (fixnum start1 start2))
437 (string-less-greater-equal t t))
439 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
440 #!+sb-doc
441 "Given two strings, if the first string is lexicographically less than
442 the second string, returns the longest common prefix (using char-equal)
443 of the two strings. Otherwise, returns ()."
444 (string-lessp* string1 string2 start1 end1 start2 end2))
446 (defun two-arg-string-lessp (string1 string2)
447 (string-lessp* string1 string2 0 nil 0 nil))
449 (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
450 #!+sb-doc
451 "Given two strings, if the first string is lexicographically greater than
452 the second string, returns the longest common prefix (using char-equal)
453 of the two strings. Otherwise, returns ()."
454 (string-greaterp* string1 string2 start1 end1 start2 end2))
456 (defun two-arg-string-greaterp (string1 string2)
457 (string-greaterp* string1 string2 0 nil 0 nil))
459 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
460 #!+sb-doc
461 "Given two strings, if the first string is lexicographically greater
462 than or equal to the second string, returns the longest common prefix
463 (using char-equal) of the two strings. Otherwise, returns ()."
464 (string-not-lessp* string1 string2 start1 end1 start2 end2))
466 (defun two-arg-string-not-lessp (string1 string2)
467 (string-not-lessp* string1 string2 0 nil 0 nil))
469 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
470 end2)
471 #!+sb-doc
472 "Given two strings, if the first string is lexicographically less than
473 or equal to the second string, returns the longest common prefix
474 (using char-equal) of the two strings. Otherwise, returns ()."
475 (string-not-greaterp* string1 string2 start1 end1 start2 end2))
478 (defun two-arg-string-not-greaterp (string1 string2)
479 (string-not-greaterp* string1 string2 0 nil 0 nil))
481 (defun make-string (count &key
482 (element-type 'character)
483 ((:initial-element fill-char)))
484 #!+sb-doc
485 "Given a character count and an optional fill character, makes and returns a
486 new string COUNT long filled with the fill character."
487 (declare (index count))
488 (declare (explicit-check))
489 ;; FIXME: while this is a correct implementation relying on an IR1 transform,
490 ;; it would be better if in the following example (assuming NOTINLINE):
491 ;; (MAKE-STRING 1000 :ELEMENT-TYPE 'BIT :INITIAL-element #\a)
492 ;; we could report that "BIT is not a subtype of CHARACTER"
493 ;; instead of "#\a is not of type BIT". Additionally, in this case:
494 ;; (MAKE-STRING 200000000 :ELEMENT-TYPE 'WORD :INITIAL-ELEMENT #\a)
495 ;; the error reported is heap exhaustion rather than type mismatch.
496 (if fill-char
497 (make-string count :element-type element-type
498 :initial-element (the character fill-char))
499 (make-string count :element-type element-type)))
501 (flet ((%upcase (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) (char-upcase (schar string index)))))
509 saved-header)))
510 (defun string-upcase (string &key (start 0) end)
511 (%upcase (copy-seq (string string)) start end))
512 (defun nstring-upcase (string &key (start 0) end)
513 (%upcase string start end))
514 ) ; FLET
516 (flet ((%downcase (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)))
521 ((= index (the fixnum end)))
522 (declare (fixnum index))
523 (setf (schar string index)
524 (char-downcase (schar string index)))))
525 saved-header)))
526 (defun string-downcase (string &key (start 0) end)
527 (%downcase (copy-seq (string string)) start end))
528 (defun nstring-downcase (string &key (start 0) end)
529 (%downcase string start end))
530 ) ; FLET
532 (defun generic-string-trim (char-bag string left-p right-p)
533 (let ((header (%string string)))
534 (with-array-data ((string header)
535 (start)
536 (end)
537 :check-fill-pointer t)
538 (let* ((left-end (if left-p
539 (do ((index start (1+ index)))
540 ((or (= index (the fixnum end))
541 (not (find (schar string index)
542 char-bag
543 :test #'char=)))
544 index)
545 (declare (fixnum index)))
546 start))
547 (right-end (if right-p
548 (do ((index (1- (the fixnum end)) (1- index)))
549 ((or (< index left-end)
550 (not (find (schar string index)
551 char-bag
552 :test #'char=)))
553 (1+ index))
554 (declare (fixnum index)))
555 end)))
556 (if (and (eql left-end start)
557 (eql right-end end))
558 header
559 (subseq (the simple-string string) left-end right-end))))))
561 (defun string-left-trim (char-bag string)
562 (generic-string-trim char-bag string t nil))
564 (defun string-right-trim (char-bag string)
565 (generic-string-trim char-bag string nil t))
567 (defun string-trim (char-bag string)
568 (generic-string-trim char-bag string t t))