Initial revision
[sbcl.git] / src / code / string.lisp
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.
9
10 (in-package "SB!IMPL")
11
12 (file-comment
13   "$Header$")
14
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, X's pname 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   (cond ((stringp x) x)
22         ((symbolp x) (symbol-name x))
23         ((characterp x)
24          (let ((res (make-string 1)))
25            (setf (schar res 0) x) res))
26         (t
27          (error 'simple-type-error
28                 :datum x
29                 :expected-type 'stringable
30                 :format-control "~S cannot be coerced to a string."
31                 :format-arguments (list x)))))
32
33 ;;; With-One-String is used to set up some string hacking things. The keywords
34 ;;; are parsed, and the string is hacked into a simple-string.
35
36 (eval-when (:compile-toplevel)
37
38 (sb!xc:defmacro with-one-string (string start end cum-offset &rest forms)
39   `(let ((,string (if (stringp ,string) ,string (string ,string))))
40      (with-array-data ((,string ,string :offset-var ,cum-offset)
41                        (,start ,start)
42                        (,end (or ,end (length (the vector ,string)))))
43        ,@forms)))
44
45 ) ; EVAN-WHEN
46
47 ;;; With-String is like With-One-String, but doesn't parse keywords.
48
49 (eval-when (:compile-toplevel)
50
51 (sb!xc:defmacro with-string (string &rest forms)
52   `(let ((,string (if (stringp ,string) ,string (string ,string))))
53      (with-array-data ((,string ,string)
54                        (start)
55                        (end (length (the vector ,string))))
56        ,@forms)))
57
58 ) ; EVAL-WHEN
59
60 ;;; With-Two-Strings is used to set up string comparison operations. The
61 ;;; keywords are parsed, and the strings are hacked into simple-strings.
62
63 (eval-when (:compile-toplevel)
64
65 (sb!xc:defmacro with-two-strings (string1 string2 start1 end1 cum-offset-1
66                                             start2 end2 &rest forms)
67   `(let ((,string1 (if (stringp ,string1) ,string1 (string ,string1)))
68          (,string2 (if (stringp ,string2) ,string2 (string ,string2))))
69      (with-array-data ((,string1 ,string1 :offset-var ,cum-offset-1)
70                        (,start1 ,start1)
71                        (,end1 (or ,end1 (length (the vector ,string1)))))
72        (with-array-data ((,string2 ,string2)
73                          (,start2 ,start2)
74                          (,end2 (or ,end2 (length (the vector ,string2)))))
75          ,@forms))))
76
77 ) ; EVAL-WHEN
78
79 (defun char (string index)
80   #!+sb-doc
81   "Given a string and a non-negative integer index less than the length of
82   the string, returns the character object representing the character at
83   that position in the string."
84   (declare (optimize (safety 1)))
85   (char string index))
86
87 (defun %charset (string index new-el)
88   (declare (optimize (safety 1)))
89   (setf (char string index) new-el))
90
91 (defun schar (string index)
92   #!+sb-doc
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)))
96   (schar string index))
97
98 (defun %scharset (string index new-el)
99   (declare (optimize (safety 1)))
100   (setf (schar string index) new-el))
101
102 (defun string=* (string1 string2 start1 end1 start2 end2)
103   (with-two-strings string1 string2 start1 end1 offset1 start2 end2
104     (not (%sp-string-compare string1 start1 end1 string2 start2 end2))))
105
106 (defun string/=* (string1 string2 start1 end1 start2 end2)
107   (with-two-strings string1 string2 start1 end1 offset1 start2 end2
108     (let ((comparison (%sp-string-compare string1 start1 end1
109                                           string2 start2 end2)))
110       (if comparison (- (the fixnum comparison) offset1)))))
111
112 (eval-when (:compile-toplevel :execute)
113
114 ;;; Lessp is true if the desired expansion is for string<* or string<=*.
115 ;;; Equalp is true if the desired expansion is for string<=* or string>=*.
116 (sb!xc:defmacro string<>=*-body (lessp equalp)
117   (let ((offset1 (gensym)))
118     `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
119        (let ((index (%sp-string-compare string1 start1 end1
120                                         string2 start2 end2)))
121          (if index
122              (cond ((= (the fixnum index) (the fixnum end1))
123                     ,(if lessp
124                          `(- (the fixnum index) ,offset1)
125                        `nil))
126                    ((= (+ (the fixnum index) (- start2 start1))
127                        (the fixnum end2))
128                     ,(if lessp
129                          `nil
130                        `(- (the fixnum index) ,offset1)))
131                    ((,(if lessp 'char< 'char>)
132                      (schar string1 index)
133                      (schar string2 (+ (the fixnum index) (- start2 start1))))
134                     (- (the fixnum index) ,offset1))
135                    (t nil))
136              ,(if equalp `(- (the fixnum end1) ,offset1) 'nil))))))
137 ) ; eval-when
138
139 (defun string<* (string1 string2 start1 end1 start2 end2)
140   (declare (fixnum start1 start2))
141   (string<>=*-body t nil))
142
143 (defun string>* (string1 string2 start1 end1 start2 end2)
144   (declare (fixnum start1 start2))
145   (string<>=*-body nil nil))
146
147 (defun string<=* (string1 string2 start1 end1 start2 end2)
148   (declare (fixnum start1 start2))
149   (string<>=*-body t t))
150
151 (defun string>=* (string1 string2 start1 end1 start2 end2)
152   (declare (fixnum start1 start2))
153   (string<>=*-body nil t))
154
155 (defun string< (string1 string2 &key (start1 0) end1 (start2 0) end2)
156   #!+sb-doc
157   "Given two strings, if the first string is lexicographically less than
158   the second string, returns the longest common prefix (using char=)
159   of the two strings. Otherwise, returns ()."
160   (string<* string1 string2 start1 end1 start2 end2))
161
162 (defun string> (string1 string2 &key (start1 0) end1 (start2 0) end2)
163   #!+sb-doc
164   "Given two strings, if the first string is lexicographically greater than
165   the second string, returns the longest common prefix (using char=)
166   of the two strings. Otherwise, returns ()."
167   (string>* string1 string2 start1 end1 start2 end2))
168
169 (defun string<= (string1 string2 &key (start1 0) end1 (start2 0) end2)
170   #!+sb-doc
171   "Given two strings, if the first string is lexicographically less than
172   or equal to the second string, returns the longest common prefix
173   (using char=) of the two strings. Otherwise, returns ()."
174   (string<=* string1 string2 start1 end1 start2 end2))
175
176 (defun string>= (string1 string2 &key (start1 0) end1 (start2 0) end2)
177   "Given two strings, if the first string is lexicographically greater
178   than or equal to the second string, returns the longest common prefix
179   (using char=) of the two strings. Otherwise, returns ()."
180   (string>=* string1 string2 start1 end1 start2 end2))
181
182 ;;; Note: (STRING= "PREFIX" "SHORT" :END2 (LENGTH "PREFIX")) gives
183 ;;; an error instead of returning NIL as I would have expected.
184 ;;; The ANSI spec for STRING= itself doesn't seem to clarify this
185 ;;; much, but the SUBSEQ-OUT-OF-BOUNDS writeup seems to say that
186 ;;; this is conforming (and required) behavior, because any index
187 ;;; out of range is an error. (So there seems to be no concise and
188 ;;; efficient way to test for strings which begin with a particular
189 ;;; pattern. Alas..) -- WHN 19991206
190 (defun string= (string1 string2 &key (start1 0) end1 (start2 0) end2)
191   #!+sb-doc
192   "Given two strings (string1 and string2), and optional integers start1,
193   start2, end1 and end2, compares characters in string1 to characters in
194   string2 (using char=)."
195   (string=* string1 string2 start1 end1 start2 end2))
196
197 (defun string/= (string1 string2 &key (start1 0) end1 (start2 0) end2)
198   #!+sb-doc
199   "Given two strings, if the first string is not lexicographically equal
200   to the second string, returns the longest common prefix (using char=)
201   of the two strings. Otherwise, returns ()."
202   (string/=* string1 string2 start1 end1 start2 end2))
203
204 (eval-when (:compile-toplevel :execute)
205
206 ;;; STRING-NOT-EQUAL-LOOP is used to generate character comparison loops for
207 ;;; STRING-EQUAL and STRING-NOT-EQUAL.
208 (sb!xc:defmacro string-not-equal-loop (end
209                                          end-value
210                                          &optional (abort-value nil abortp))
211   (declare (fixnum end))
212   (let ((end-test (if (= end 1)
213                       `(= index1 (the fixnum end1))
214                       `(= index2 (the fixnum end2)))))
215     `(do ((index1 start1 (1+ index1))
216           (index2 start2 (1+ index2)))
217          (,(if abortp
218                end-test
219                `(or ,end-test
220                     (not (char-equal (schar string1 index1)
221                                      (schar string2 index2)))))
222           ,end-value)
223        (declare (fixnum index1 index2))
224        ,@(if abortp
225              `((if (not (char-equal (schar string1 index1)
226                                     (schar string2 index2)))
227                    (return ,abort-value)))))))
228
229 ) ; EVAL-WHEN
230
231 (defun string-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
232   #!+sb-doc
233   "Given two strings (string1 and string2), and optional integers start1,
234   start2, end1 and end2, compares characters in string1 to characters in
235   string2 (using char-equal)."
236   (declare (fixnum start1 start2))
237   (with-two-strings string1 string2 start1 end1 offset1 start2 end2
238     (let ((slen1 (- (the fixnum end1) start1))
239           (slen2 (- (the fixnum end2) start2)))
240       (declare (fixnum slen1 slen2))
241       (if (or (minusp slen1) (minusp slen2))
242           ;;prevent endless looping later.
243           (error "Improper bounds for string comparison."))
244       (if (= slen1 slen2)
245           ;;return () immediately if lengths aren't equal.
246           (string-not-equal-loop 1 t nil)))))
247
248 (defun string-not-equal (string1 string2 &key (start1 0) end1 (start2 0) end2)
249   #!+sb-doc
250   "Given two strings, if the first string is not lexicographically equal
251   to the second string, returns the longest common prefix (using char-equal)
252   of the two strings. Otherwise, returns ()."
253   (with-two-strings string1 string2 start1 end1 offset1 start2 end2
254     (let ((slen1 (- end1 start1))
255           (slen2 (- end2 start2)))
256       (declare (fixnum slen1 slen2))
257       (if (or (minusp slen1) (minusp slen2))
258           ;;prevent endless looping later.
259           (error "Improper bounds for string comparison."))
260       (cond ((or (minusp slen1) (or (minusp slen2)))
261              (error "Improper substring for comparison."))
262             ((= slen1 slen2)
263              (string-not-equal-loop 1 nil (- index1 offset1)))
264             ((< slen1 slen2)
265              (string-not-equal-loop 1 (- index1 offset1)))
266             (t
267              (string-not-equal-loop 2 (- index1 offset1)))))))
268
269 (eval-when (:compile-toplevel :execute)
270
271 ;;; STRING-LESS-GREATER-EQUAL-TESTS returns a test on the lengths of string1
272 ;;; and string2 and a test on the current characters from string1 and string2
273 ;;; for the following macro.
274 (defun string-less-greater-equal-tests (lessp equalp)
275   (if lessp
276       (if equalp
277           ;; STRING-NOT-GREATERP
278           (values '<= `(not (char-greaterp char1 char2)))
279           ;; STRING-LESSP
280           (values '< `(char-lessp char1 char2)))
281       (if equalp
282           ;; STRING-NOT-LESSP
283           (values '>= `(not (char-lessp char1 char2)))
284           ;; STRING-GREATERP
285           (values '> `(char-greaterp char1 char2)))))
286
287 (sb!xc:defmacro string-less-greater-equal (lessp equalp)
288   (multiple-value-bind (length-test character-test)
289       (string-less-greater-equal-tests lessp equalp)
290     `(with-two-strings string1 string2 start1 end1 offset1 start2 end2
291        (let ((slen1 (- (the fixnum end1) start1))
292              (slen2 (- (the fixnum end2) start2)))
293          (declare (fixnum slen1 slen2))
294          (if (or (minusp slen1) (minusp slen2))
295              ;;prevent endless looping later.
296              (error "Improper bounds for string comparison."))
297          (do ((index1 start1 (1+ index1))
298               (index2 start2 (1+ index2))
299               (char1)
300               (char2))
301              ((or (= index1 (the fixnum end1)) (= index2 (the fixnum end2)))
302               (if (,length-test slen1 slen2) (- index1 offset1)))
303            (declare (fixnum index1 index2))
304            (setq char1 (schar string1 index1))
305            (setq char2 (schar string2 index2))
306            (if (not (char-equal char1 char2))
307                (if ,character-test
308                    (return (- index1 offset1))
309                    (return ()))))))))
310
311 ) ; EVAL-WHEN
312
313 (defun string-lessp* (string1 string2 start1 end1 start2 end2)
314   (declare (fixnum start1 start2))
315   (string-less-greater-equal t nil))
316
317 (defun string-greaterp* (string1 string2 start1 end1 start2 end2)
318   (declare (fixnum start1 start2))
319   (string-less-greater-equal nil nil))
320
321 (defun string-not-lessp* (string1 string2 start1 end1 start2 end2)
322   (declare (fixnum start1 start2))
323   (string-less-greater-equal nil t))
324
325 (defun string-not-greaterp* (string1 string2 start1 end1 start2 end2)
326   (declare (fixnum start1 start2))
327   (string-less-greater-equal t t))
328
329 (defun string-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
330   #!+sb-doc
331   "Given two strings, if the first string is lexicographically less than
332   the second string, returns the longest common prefix (using char-equal)
333   of the two strings. Otherwise, returns ()."
334   (string-lessp* string1 string2 start1 end1 start2 end2))
335
336 (defun string-greaterp (string1 string2 &key (start1 0) end1 (start2 0) end2)
337   #!+sb-doc
338   "Given two strings, if the first string is lexicographically greater than
339   the second string, returns the longest common prefix (using char-equal)
340   of the two strings. Otherwise, returns ()."
341   (string-greaterp* string1 string2 start1 end1 start2 end2))
342
343 (defun string-not-lessp (string1 string2 &key (start1 0) end1 (start2 0) end2)
344   #!+sb-doc
345   "Given two strings, if the first string is lexicographically greater
346   than or equal to the second string, returns the longest common prefix
347   (using char-equal) of the two strings. Otherwise, returns ()."
348   (string-not-lessp* string1 string2 start1 end1 start2 end2))
349
350 (defun string-not-greaterp (string1 string2 &key (start1 0) end1 (start2 0)
351                                     end2)
352   #!+sb-doc
353   "Given two strings, if the first string is lexicographically less than
354   or equal to the second string, returns the longest common prefix
355   (using char-equal) of the two strings. Otherwise, returns ()."
356   (string-not-greaterp* string1 string2 start1 end1 start2 end2))
357
358 (defun make-string (count &key element-type ((:initial-element fill-char)))
359   #!+sb-doc
360   "Given a character count and an optional fill character, makes and returns
361    a new string Count long filled with the fill character."
362   (declare (fixnum count)
363            (ignore element-type))
364   (if fill-char
365       (do ((i 0 (1+ i))
366            (string (make-string count)))
367           ((= i count) string)
368         (declare (fixnum i))
369         (setf (schar string i) fill-char))
370       (make-string count)))
371
372 (defun string-upcase (string &key (start 0) end)
373   #!+sb-doc
374   "Given a string, returns a new string that is a copy of it with
375   all lower case alphabetic characters converted to uppercase."
376   (declare (fixnum start))
377   (let* ((string (if (stringp string) string (string string)))
378          (slen (length string)))
379     (declare (fixnum slen))
380     (with-one-string string start end offset
381       (let ((offset-slen (+ slen offset))
382             (newstring (make-string slen)))
383         (declare (fixnum offset-slen))
384         (do ((index offset (1+ index))
385              (new-index 0 (1+ new-index)))
386             ((= index start))
387           (declare (fixnum index new-index))
388           (setf (schar newstring new-index) (schar string index)))
389         (do ((index start (1+ index))
390              (new-index (- start offset) (1+ new-index)))
391             ((= index (the fixnum end)))
392           (declare (fixnum index new-index))
393           (setf (schar newstring new-index)
394                 (char-upcase (schar string index))))
395         (do ((index end (1+ index))
396              (new-index (- (the fixnum end) offset) (1+ new-index)))
397             ((= index offset-slen))
398           (declare (fixnum index new-index))
399           (setf (schar newstring new-index) (schar string index)))
400         newstring))))
401
402 (defun string-downcase (string &key (start 0) end)
403   #!+sb-doc
404   "Given a string, returns a new string that is a copy of it with
405   all upper case alphabetic characters converted to lowercase."
406   (declare (fixnum start))
407   (let* ((string (if (stringp string) string (string string)))
408          (slen (length string)))
409     (declare (fixnum slen))
410     (with-one-string string start end offset
411       (let ((offset-slen (+ slen offset))
412             (newstring (make-string slen)))
413         (declare (fixnum offset-slen))
414         (do ((index offset (1+ index))
415              (new-index 0 (1+ new-index)))
416             ((= index start))
417           (declare (fixnum index new-index))
418           (setf (schar newstring new-index) (schar string index)))
419         (do ((index start (1+ index))
420              (new-index (- start offset) (1+ new-index)))
421             ((= index (the fixnum end)))
422           (declare (fixnum index new-index))
423           (setf (schar newstring new-index)
424                 (char-downcase (schar string index))))
425         (do ((index end (1+ index))
426              (new-index (- (the fixnum end) offset) (1+ new-index)))
427             ((= index offset-slen))
428           (declare (fixnum index new-index))
429           (setf (schar newstring new-index) (schar string index)))
430         newstring))))
431
432 (defun string-capitalize (string &key (start 0) end)
433   #!+sb-doc
434   "Given a string, returns a copy of the string with the first
435   character of each ``word'' converted to upper-case, and remaining
436   chars in the word converted to lower case. A ``word'' is defined
437   to be a string of case-modifiable characters delimited by
438   non-case-modifiable chars."
439   (declare (fixnum start))
440   (let* ((string (if (stringp string) string (string string)))
441          (slen (length string)))
442     (declare (fixnum slen))
443     (with-one-string string start end offset
444       (let ((offset-slen (+ slen offset))
445             (newstring (make-string slen)))
446         (declare (fixnum offset-slen))
447         (do ((index offset (1+ index))
448              (new-index 0 (1+ new-index)))
449             ((= index start))
450           (declare (fixnum index new-index))
451           (setf (schar newstring new-index) (schar string index)))
452         (do ((index start (1+ index))
453              (new-index (- start offset) (1+ new-index))
454              (newword t)
455              (char ()))
456             ((= index (the fixnum end)))
457           (declare (fixnum index new-index))
458           (setq char (schar string index))
459           (cond ((not (alphanumericp char))
460                  (setq newword t))
461                 (newword
462                  ;;char is first case-modifiable after non-case-modifiable
463                  (setq char (char-upcase char))
464                  (setq newword ()))
465                 ;;char is case-modifiable, but not first
466                 (t (setq char (char-downcase char))))
467           (setf (schar newstring new-index) char))
468         (do ((index end (1+ index))
469              (new-index (- (the fixnum end) offset) (1+ new-index)))
470             ((= index offset-slen))
471           (declare (fixnum index new-index))
472           (setf (schar newstring new-index) (schar string index)))
473         newstring))))
474
475 (defun nstring-upcase (string &key (start 0) end)
476   #!+sb-doc
477   "Given a string, returns that string with all lower case alphabetic
478   characters converted to uppercase."
479   (declare (fixnum start))
480   (let ((save-header string))
481     (with-one-string string start end offset
482       (do ((index start (1+ index)))
483           ((= index (the fixnum end)))
484         (declare (fixnum index))
485         (setf (schar string index) (char-upcase (schar string index)))))
486     save-header))
487
488 (defun nstring-downcase (string &key (start 0) end)
489   #!+sb-doc
490   "Given a string, returns that string with all upper case alphabetic
491   characters converted to lowercase."
492   (declare (fixnum start))
493   (let ((save-header string))
494     (with-one-string string start end offset
495       (do ((index start (1+ index)))
496           ((= index (the fixnum end)))
497         (declare (fixnum index))
498         (setf (schar string index) (char-downcase (schar string index)))))
499     save-header))
500
501 (defun nstring-capitalize (string &key (start 0) end)
502   #!+sb-doc
503   "Given a string, returns that string with the first
504   character of each ``word'' converted to upper-case, and remaining
505   chars in the word converted to lower case. A ``word'' is defined
506   to be a string of case-modifiable characters delimited by
507   non-case-modifiable chars."
508   (declare (fixnum start))
509   (let ((save-header string))
510     (with-one-string string start end offset
511       (do ((index start (1+ index))
512            (newword t)
513            (char ()))
514           ((= index (the fixnum end)))
515         (declare (fixnum index))
516         (setq char (schar string index))
517         (cond ((not (alphanumericp char))
518                (setq newword t))
519               (newword
520                ;;char is first case-modifiable after non-case-modifiable
521                (setf (schar string index) (char-upcase char))
522                (setq newword ()))
523               (t
524                (setf (schar string index) (char-downcase char))))))
525     save-header))
526
527 (defun string-left-trim (char-bag string)
528   #!+sb-doc
529   "Given a set of characters (a list or string) and a string, returns
530   a copy of the string with the characters in the set removed from the
531   left end."
532   (with-string string
533     (do ((index start (1+ index)))
534         ((or (= index (the fixnum end))
535              (not (find (schar string index) char-bag :test #'char=)))
536          (subseq (the simple-string string) index end))
537       (declare (fixnum index)))))
538
539 (defun string-right-trim (char-bag string)
540   #!+sb-doc
541   "Given a set of characters (a list or string) and a string, returns
542   a copy of the string with the characters in the set removed from the
543   right end."
544   (with-string string
545     (do ((index (1- (the fixnum end)) (1- index)))
546         ((or (< index start)
547              (not (find (schar string index) char-bag :test #'char=)))
548          (subseq (the simple-string string) start (1+ index)))
549       (declare (fixnum index)))))
550
551 (defun string-trim (char-bag string)
552   #!+sb-doc
553   "Given a set of characters (a list or string) and a string, returns a
554   copy of the string with the characters in the set removed from both
555   ends."
556   (with-string string
557     (let* ((left-end (do ((index start (1+ index)))
558                          ((or (= index (the fixnum end))
559                               (not (find (schar string index)
560                                          char-bag
561                                          :test #'char=)))
562                           index)
563                        (declare (fixnum index))))
564            (right-end (do ((index (1- (the fixnum end)) (1- index)))
565                           ((or (< index left-end)
566                                (not (find (schar string index)
567                                           char-bag
568                                           :test #'char=)))
569                            (1+ index))
570                         (declare (fixnum index)))))
571       (subseq (the simple-string string) left-end right-end))))