Optimize BIT-VECTOR-= on non-simple arrays.
[sbcl.git] / src / code / string.lisp
blob7675bbcdf4752235fb9bb12772e06114a0e5943d
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))))
65 ) ; EVAL-WHEN
67 (defun char (string index)
68 #!+sb-doc
69 "Given a string and a non-negative integer index less than the length of
70 the string, returns the character object representing the character at
71 that position in the string."
72 (declare (optimize (safety 1)))
73 (char string index))
75 (defun %charset (string index new-el)
76 (declare (optimize (safety 1)))
77 (setf (char string index) new-el))
79 (defun schar (string index)
80 #!+sb-doc
81 "SCHAR returns the character object at an indexed position in a string
82 just as CHAR does, except the string must be a simple-string."
83 (declare (optimize (safety 1)))
84 (schar string index))
86 (defun %scharset (string index new-el)
87 (declare (optimize (safety 1)))
88 (setf (schar string index) new-el))
90 (defun string=* (string1 string2 start1 end1 start2 end2)
91 (declare (optimize speed))
92 (with-two-strings string1 string2 start1 end1 nil start2 end2
93 (let ((len (- end1 start1)))
94 (unless (= len (- end2 start2)) ; trivial
95 (return-from string=* nil))
96 ;; Optimizing the non-unicode builds is not terribly important
97 ;; because no per-character test for base/UCS4 is needed.
98 #!+sb-unicode
99 (let* ((widetag1 (%other-pointer-widetag string1))
100 (widetag2 (%other-pointer-widetag string2))
101 (char-shift
102 #!+(or x86 x86-64)
103 ;; The cost of WITH-PINNED-OBJECTS is near nothing on x86,
104 ;; and memcmp() is much faster except below a cutoff point.
105 ;; The threshold is higher on x86-32 because the overhead
106 ;; of a foreign call is higher due to FPU stack save/restore.
107 (if (and (= widetag1 widetag2)
108 (>= len #!+x86 16
109 #!+x86-64 8))
110 (case widetag1
111 (#.sb!vm:simple-base-string-widetag 0)
112 (#.sb!vm:simple-character-string-widetag 2)))))
113 (when char-shift
114 (return-from string=*
115 ;; Efficiently compute byte indices. Derive-type on ASH isn't
116 ;; good enough. For 32-bit, it should be ok because
117 ;; (TYPEP (ASH ARRAY-TOTAL-SIZE-LIMIT 2) 'SB-VM:SIGNED-WORD) => T
118 ;; For 63-bit fixnums, that's false in theory, but true in practice.
119 ;; ARRAY-TOTAL-SIZE-LIMIT is too large for a 48-bit address space.
120 (macrolet ((sap (string start)
121 `(sap+ (vector-sap (truly-the string ,string))
122 (scale ,start)))
123 (scale (index)
124 `(truly-the sb!vm:signed-word
125 (ash (truly-the index ,index) char-shift))))
126 (declare (optimize (sb!c:alien-funcall-saves-fp-and-pc 0)))
127 (with-pinned-objects (string1 string2)
128 (zerop (alien-funcall
129 (extern-alien "memcmp"
130 (function int (* char) (* char) long))
131 (sap string1 start1) (sap string2 start2)
132 (scale len)))))))
133 (macrolet
134 ((char-loop (type1 type2)
135 `(return-from string=*
136 (let ((string1 (truly-the (simple-array ,type1 1) string1))
137 (string2 (truly-the (simple-array ,type2 1) string2)))
138 (declare (optimize (sb!c::insert-array-bounds-checks 0)))
139 (do ((index1 start1 (1+ index1))
140 (index2 start2 (1+ index2)))
141 ((>= index1 end1) t)
142 (declare (index index1 index2))
143 (unless (char= (schar string1 index1)
144 (schar string2 index2))
145 (return nil)))))))
146 ;; On x86-64, short strings with same widetag use the general case.
147 ;; Why not always have cases for equal widetags and short strings?
148 ;; Because the code below deals with comparison when memcpy _can't_
149 ;; be used and is essential to this logic. No major speed gain is had
150 ;; with extra cases where memcpy would do, but was avoided.
151 ;; On non-x86, Lisp code is used always because I did not profile
152 ;; memcmp(), and this code is at least as good as %SP-STRING-COMPARE.
153 ;; Also, (ARRAY NIL) always punts.
154 (cond #!-x86-64
155 ((= widetag1 widetag2)
156 (case widetag1
157 (#.sb!vm:simple-base-string-widetag
158 (char-loop base-char base-char))
159 (#.sb!vm:simple-character-string-widetag
160 (char-loop character character))))
161 ((or (and (= widetag1 sb!vm:simple-character-string-widetag)
162 (= widetag2 sb!vm:simple-base-string-widetag))
163 (and (= widetag2 sb!vm:simple-character-string-widetag)
164 (= widetag1 sb!vm:simple-base-string-widetag)
165 (progn (rotatef start1 start2)
166 (rotatef end1 end2)
167 (rotatef string1 string2)
168 t)))
169 (char-loop character base-char))))))
170 (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
172 (defun string/=* (string1 string2 start1 end1 start2 end2)
173 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
174 (let ((comparison (%sp-string-compare string1 start1 end1
175 string2 start2 end2)))
176 (if comparison (- (the fixnum comparison) offset1)))))
178 (eval-when (:compile-toplevel :execute)
180 ;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
181 ;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
182 (sb!xc:defmacro string<>=*-body (lessp equalp)
183 (let ((offset1 (gensym)))
184 `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
185 (let ((index (%sp-string-compare string1 start1 end1
186 string2 start2 end2)))
187 (if index
188 (cond ((= (the fixnum index) (the fixnum end1))
189 ,(if lessp
190 `(- (the fixnum index) ,offset1)
191 `nil))
192 ((= (+ (the fixnum index) (- start2 start1))
193 (the fixnum end2))
194 ,(if lessp
195 `nil
196 `(- (the fixnum index) ,offset1)))
197 ((,(if lessp 'char< 'char>)
198 (schar string1 index)
199 (schar string2 (+ (the fixnum index) (- start2 start1))))
200 (- (the fixnum index) ,offset1))
201 (t nil))
202 ,(if equalp `(- (the fixnum end1) ,offset1) nil))))))
203 ) ; EVAL-WHEN
205 (defun string<* (string1 string2 start1 end1 start2 end2)
206 (declare (fixnum start1 start2))
207 (string<>=*-body t nil))
209 (defun string>* (string1 string2 start1 end1 start2 end2)
210 (declare (fixnum start1 start2))
211 (string<>=*-body nil nil))
213 (defun string<=* (string1 string2 start1 end1 start2 end2)
214 (declare (fixnum start1 start2))
215 (string<>=*-body t t))
217 (defun string>=* (string1 string2 start1 end1 start2 end2)
218 (declare (fixnum start1 start2))
219 (string<>=*-body nil t))
221 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
222 #!+sb-doc
223 "Given two strings, if the first string is lexicographically less than
224 the second string, returns the longest common prefix (using char=)
225 of the two strings. Otherwise, returns ()."
226 (string<* string1 string2 start1 end1 start2 end2))
228 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
229 #!+sb-doc
230 "Given two strings, if the first string is lexicographically greater than
231 the second string, returns the longest common prefix (using char=)
232 of the two strings. Otherwise, returns ()."
233 (string>* string1 string2 start1 end1 start2 end2))
235 (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
236 #!+sb-doc
237 "Given two strings, if the first string is lexicographically less than
238 or equal to the second string, returns the longest common prefix
239 (using char=) of the two strings. Otherwise, returns ()."
240 (string<=* string1 string2 start1 end1 start2 end2))
242 (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
243 #!+sb-doc
244 "Given two strings, if the first string is lexicographically greater
245 than or equal to the second string, returns the longest common prefix
246 (using char=) of the two strings. Otherwise, returns ()."
247 (string>=* string1 string2 start1 end1 start2 end2))
249 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
250 ;;; an error instead of returning NIL as I would have expected.
251 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
252 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
253 ;;; this is conforming (and required) behavior, because any index
254 ;;; out of range is an error. (So there seems to be no concise and
255 ;;; efficient way to test for strings which begin with a particular
256 ;;; pattern. Alas..) -- WHN 19991206
257 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
258 #!+sb-doc
259 "Given two strings (string1 and string2), and optional integers start1,
260 start2, end1 and end2, compares characters in string1 to characters in
261 string2 (using char=)."
262 (string=* string1 string2 start1 end1 start2 end2))
264 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
265 #!+sb-doc
266 "Given two strings, if the first string is not lexicographically equal
267 to the second string, returns the longest common prefix (using char=)
268 of the two strings. Otherwise, returns ()."
269 (string/=* string1 string2 start1 end1 start2 end2))
271 (eval-when (:compile-toplevel :execute)
273 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
274 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
275 (sb!xc:defmacro string-not-equal-loop (end
276 end-value
277 &optional (abort-value nil abortp))
278 (declare (fixnum end))
279 (let ((end-test (if (= end 1)
280 `(= index1 (the fixnum end1))
281 `(= index2 (the fixnum end2)))))
282 `(do ((index1 start1 (1+ index1))
283 (index2 start2 (1+ index2)))
284 (,(if abortp
285 end-test
286 `(or ,end-test
287 (not (char-equal (schar string1 index1)
288 (schar string2 index2)))))
289 ,end-value)
290 (declare (fixnum index1 index2))
291 ,@(if abortp
292 `((if (not (char-equal (schar string1 index1)
293 (schar string2 index2)))
294 (return ,abort-value)))))))
296 ) ; EVAL-WHEN
298 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
299 #!+sb-doc
300 "Given two strings (string1 and string2), and optional integers start1,
301 start2, end1 and end2, compares characters in string1 to characters in
302 string2 (using char-equal)."
303 (declare (fixnum start1 start2))
304 (with-two-strings string1 string2 start1 end1 nil start2 end2
305 (let ((slen1 (- (the fixnum end1) start1))
306 (slen2 (- (the fixnum end2) start2)))
307 (declare (fixnum slen1 slen2))
308 (if (= slen1 slen2)
309 ;;return () immediately if lengths aren't equal.
310 (string-not-equal-loop 1 t nil)))))
312 (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
313 #!+sb-doc
314 "Given two strings, if the first string is not lexicographically equal
315 to the second string, returns the longest common prefix (using char-equal)
316 of the two strings. Otherwise, returns ()."
317 (with-two-strings string1 string2 start1 end1 offset1 start2 end2
318 (let ((slen1 (- end1 start1))
319 (slen2 (- end2 start2)))
320 (declare (fixnum slen1 slen2))
321 (cond ((= slen1 slen2)
322 (string-not-equal-loop 1 nil (- index1 offset1)))
323 ((< slen1 slen2)
324 (string-not-equal-loop 1 (- index1 offset1)))
326 (string-not-equal-loop 2 (- index1 offset1)))))))
328 (eval-when (:compile-toplevel :execute)
330 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
331 ;;; and string2 and a test on the current characters from string1 and string2
332 ;;; for the following macro.
333 (defun string-less-greater-equal-tests (lessp equalp)
334 (if lessp
335 (if equalp
336 ;; STRING-NOT-GREATERP
337 (values '<= `(not (char-greaterp char1 char2)))
338 ;; STRING-LESSP
339 (values '< `(char-lessp char1 char2)))
340 (if equalp
341 ;; STRING-NOT-LESSP
342 (values '>= `(not (char-lessp char1 char2)))
343 ;; STRING-GREATERP
344 (values '> `(char-greaterp char1 char2)))))
346 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
347 (multiple-value-bind (length-test character-test)
348 (string-less-greater-equal-tests lessp equalp)
349 `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
350 (let ((slen1 (- (the fixnum end1) start1))
351 (slen2 (- (the fixnum end2) start2)))
352 (declare (fixnum slen1 slen2))
353 (do ((index1 start1 (1+ index1))
354 (index2 start2 (1+ index2))
355 (char1)
356 (char2))
357 ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
358 (if (,length-test slen1 slen2) (- index1 offset1)))
359 (declare (fixnum index1 index2))
360 (setq char1 (schar string1 index1))
361 (setq char2 (schar string2 index2))
362 (if (not (char-equal char1 char2))
363 (if ,character-test
364 (return (- index1 offset1))
365 (return ()))))))))
367 ) ; EVAL-WHEN
369 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
370 (declare (fixnum start1 start2))
371 (string-less-greater-equal t nil))
373 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
374 (declare (fixnum start1 start2))
375 (string-less-greater-equal nil nil))
377 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
378 (declare (fixnum start1 start2))
379 (string-less-greater-equal nil t))
381 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
382 (declare (fixnum start1 start2))
383 (string-less-greater-equal t t))
385 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
386 #!+sb-doc
387 "Given two strings, if the first string is lexicographically less than
388 the second string, returns the longest common prefix (using char-equal)
389 of the two strings. Otherwise, returns ()."
390 (string-lessp* string1 string2 start1 end1 start2 end2))
392 (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
393 #!+sb-doc
394 "Given two strings, if the first string is lexicographically greater than
395 the second string, returns the longest common prefix (using char-equal)
396 of the two strings. Otherwise, returns ()."
397 (string-greaterp* string1 string2 start1 end1 start2 end2))
399 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
400 #!+sb-doc
401 "Given two strings, if the first string is lexicographically greater
402 than or equal to the second string, returns the longest common prefix
403 (using char-equal) of the two strings. Otherwise, returns ()."
404 (string-not-lessp* string1 string2 start1 end1 start2 end2))
406 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
407 end2)
408 #!+sb-doc
409 "Given two strings, if the first string is lexicographically less than
410 or equal to the second string, returns the longest common prefix
411 (using char-equal) of the two strings. Otherwise, returns ()."
412 (string-not-greaterp* string1 string2 start1 end1 start2 end2))
414 (defun make-string (count &key
415 (element-type 'character)
416 ((:initial-element fill-char)))
417 #!+sb-doc
418 "Given a character count and an optional fill character, makes and returns a
419 new string COUNT long filled with the fill character."
420 (declare (index count))
421 (declare (explicit-check))
422 ;; FIXME: while this is a correct implementation relying on an IR1 transform,
423 ;; it would be better if in the following example (assuming NOTINLINE):
424 ;; (MAKE-STRING 1000 :ELEMENT-TYPE 'BIT :INITIAL-element #\a)
425 ;; we could report that "BIT is not a subtype of CHARACTER"
426 ;; instead of "#\a is not of type BIT". Additionally, in this case:
427 ;; (MAKE-STRING 200000000 :ELEMENT-TYPE 'WORD :INITIAL-ELEMENT #\a)
428 ;; the error reported is heap exhaustion rather than type mismatch.
429 (if fill-char
430 (make-string count :element-type element-type
431 :initial-element (the character fill-char))
432 (make-string count :element-type element-type)))
434 (flet ((%upcase (string start end)
435 (declare (string string) (index start) (type sequence-end end))
436 (let ((saved-header string))
437 (with-one-string (string start end)
438 (do ((index start (1+ index)))
439 ((= index (the fixnum end)))
440 (declare (fixnum index))
441 (setf (schar string index) (char-upcase (schar string index)))))
442 saved-header)))
443 (defun string-upcase (string &key (start 0) end)
444 (%upcase (copy-seq (string string)) start end))
445 (defun nstring-upcase (string &key (start 0) end)
446 (%upcase string start end))
447 ) ; FLET
449 (flet ((%downcase (string start end)
450 (declare (string string) (index start) (type sequence-end end))
451 (let ((saved-header string))
452 (with-one-string (string start end)
453 (do ((index start (1+ index)))
454 ((= index (the fixnum end)))
455 (declare (fixnum index))
456 (setf (schar string index)
457 (char-downcase (schar string index)))))
458 saved-header)))
459 (defun string-downcase (string &key (start 0) end)
460 (%downcase (copy-seq (string string)) start end))
461 (defun nstring-downcase (string &key (start 0) end)
462 (%downcase string start end))
463 ) ; FLET
465 (defun generic-string-trim (char-bag string left-p right-p)
466 (let ((header (%string string)))
467 (with-array-data ((string header)
468 (start)
469 (end)
470 :check-fill-pointer t)
471 (let* ((left-end (if left-p
472 (do ((index start (1+ index)))
473 ((or (= index (the fixnum end))
474 (not (find (schar string index)
475 char-bag
476 :test #'char=)))
477 index)
478 (declare (fixnum index)))
479 start))
480 (right-end (if right-p
481 (do ((index (1- (the fixnum end)) (1- index)))
482 ((or (< index left-end)
483 (not (find (schar string index)
484 char-bag
485 :test #'char=)))
486 (1+ index))
487 (declare (fixnum index)))
488 end)))
489 (if (and (eql left-end start)
490 (eql right-end end))
491 header
492 (subseq (the simple-string string) left-end right-end))))))
494 (defun string-left-trim (char-bag string)
495 (generic-string-trim char-bag string t nil))
497 (defun string-right-trim (char-bag string)
498 (generic-string-trim char-bag string nil t))
500 (defun string-trim (char-bag string)
501 (generic-string-trim char-bag string t t))