/[cmucl]/src/code/string.lisp
ViewVC logotype

Contents of /src/code/string.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5