Debian Codegraph @

scheme/ikarus.strings.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus strings)
18 (export string-length string-ref string-set! make-string string->list
19 string-append substring string list->string uuid
20 string-copy string-for-each string-fill!
21 string=? string<? string<=? string>? string>=?
22 string-copy!)
23 (import
24 (ikarus system $strings)
25 (ikarus system $fx)
26 (ikarus system $chars)
27 (ikarus system $bytevectors)
28 (ikarus system $pairs)
29 (except (ikarus) string-length string-ref string-set! make-string
30 string->list string-append substring string
31 list->string uuid string-copy string-for-each
32 string=? string<? string<=? string>? string>=?
33 string-fill! string-copy!))
34
35
36 (define string-length
37 (lambda (x)
38 (unless (string? x)
39 (die 'string-length "not a string" x))
40 ($string-length x)))
41
42
43 (define (string-ref s i)
44 (unless (string? s)
45 (die 'string-ref "not a string" s))
46 (unless (fixnum? i)
47 (die 'string-ref "not a valid index" i))
48 (unless (and ($fx< i ($string-length s))
49 ($fx<= 0 i))
50 (die 'string-ref "index is out of range" i s))
51 (let ([c ($string-ref s i)])
52 (unless (char? c)
53 (die 'string-ref "BUG: got a non-char"))
54 c))
55
56
57 (define string-set!
58 (lambda (s i c)
59 (unless (string? s)
60 (die 'string-set! "not a string" s))
61 (unless (fixnum? i)
62 (die 'string-set! "not a valid index" i))
63 (unless (and ($fx< i ($string-length s))
64 ($fx>= i 0))
65 (die 'string-set! "index is out of range" i s))
66 (unless (char? c)
67 (die 'string-set! "not a character" c))
68 ($string-set! s i c)))
69
70 (define make-string
71 (let ()
72 (define fill!
73 (lambda (s i n c)
74 (cond
75 [($fx= i n) s]
76 [else
77 ($string-set! s i c)
78 (fill! s ($fx+ i 1) n c)])))
79 (define (make-string* n c)
80 (unless (fixnum? n)
81 (die 'make-string "length is not a fixnum" n))
82 (unless (eqv? 0 (fxsra n (fx- (fixnum-width) 2)))
83 (die 'make-string "length is out of range" n))
84 (fill! ($make-string n) 0 n c))
85 (define make-string
86 (case-lambda
87 [(n) (make-string* n (integer->char 0))]
88 [(n c)
89 (if (char? c)
90 (make-string* n c)
91 (die 'make-string "not a character" c))]))
92 make-string))
93
94
95 (define string
96 ;;; FIXME: add case-lambda
97 (letrec ([length
98 (lambda (ls n)
99 (cond
100 [(null? ls) n]
101 [(char? ($car ls)) (length ($cdr ls) ($fx+ n 1))]
102 [else (die 'string "not a character" ($car ls))]))]
103 [loop
104 (lambda (s ls i n)
105 (cond
106 [($fx= i n) s]
107 [else
108 ($string-set! s i ($car ls))
109 (loop s ($cdr ls) ($fx+ i 1) n)]))])
110 (lambda ls
111 (let ([n (length ls 0)])
112 (let ([s (make-string n)])
113 (loop s ls 0 n))))))
114
115 (module (substring)
116 (define fill
117 (lambda (s d si sj di)
118 (cond
119 [($fx= si sj) d]
120 [else
121 ($string-set! d di ($string-ref s si))
122 (fill s d ($fxadd1 si) sj ($fxadd1 di))])))
123 (define substring
124 (lambda (s n m)
125 (unless (string? s)
126 (die 'substring "not a string" s))
127 (let ([len ($string-length s)])
128 (unless (and (fixnum? n)
129 ($fx>= n 0)
130 ($fx<= n len))
131 (die 'substring "not a valid start index" n s))
132 (unless (and (fixnum? m)
133 ($fx>= m 0)
134 ($fx<= m len))
135 (die 'substring "not a valid end index" m s))
136 (unless ($fx<= n m)
137 (die 'substring "indices are in decreasing order" n m))
138 (let ([len ($fx- m n)])
139 (if ($fx> len 0)
140 (fill s ($make-string len) n m 0)
141 ""))))))
142
143 (define string-copy
144 (lambda (s)
145 (if (string? s)
146 (substring s 0 (string-length s))
147 (die 'string-copy "not a string" s))))
148
149 (module (string=?)
150 (define bstring=?
151 (lambda (s1 s2 i j)
152 (or ($fx= i j)
153 (and ($char= ($string-ref s1 i) ($string-ref s2 i))
154 (bstring=? s1 s2 ($fxadd1 i) j)))))
155 (define check-strings-and-return-false
156 (lambda (s*)
157 (cond
158 [(null? s*) #f]
159 [(string? ($car s*))
160 (check-strings-and-return-false ($cdr s*))]
161 [else (err ($car s*))])))
162 (define strings=?
163 (lambda (s s* n)
164 (or (null? s*)
165 (let ([a ($car s*)])
166 (unless (string? a)
167 (die 'string=? "not a string" a))
168 (if ($fx= n ($string-length a))
169 (and (strings=? s ($cdr s*) n)
170 (bstring=? s a 0 n))
171 (check-strings-and-return-false ($cdr s*)))))))
172 (define (err x)
173 (die 'string=? "not a string" x))
174 (define string=?
175 (case-lambda
176 [(s s1)
177 (if (string? s)
178 (if (string? s1)
179 (let ([n ($string-length s)])
180 (and ($fx= n ($string-length s1))
181 (bstring=? s s1 0 n)))
182 (err s1))
183 (err s))]
184 [(s . s*)
185 (if (string? s)
186 (strings=? s s* ($string-length s))
187 (err s))])))
188
189
190 (define string-cmp
191 (lambda (who cmp s1 s*)
192 (if (string? s1)
193 (let f ([s1 s1] [s* s*])
194 (cond
195 [(null? s*) #t]
196 [else
197 (let ([s2 (car s*)])
198 (if (string? s2)
199 (if (cmp s1 s2)
200 (f s2 (cdr s*))
201 (let f ([s* (cdr s*)])
202 (cond
203 [(null? s*) #f]
204 [(string? (car s*))
205 (f (cdr s*))]
206 [else
207 (die who "not a string"
208 (car s*))])))
209 (die who "not a string" s2)))]))
210 (die who "not a string" s1))))
211
212 (define ($string<? s1 s2)
213 (let ([n1 ($string-length s1)]
214 [n2 ($string-length s2)])
215 (if ($fx< n1 n2)
216 (let f ([i 0] [n n1] [s1 s1] [s2 s2])
217 (if ($fx= i n)
218 #t
219 (let ([c1 ($string-ref s1 i)]
220 [c2 ($string-ref s2 i)])
221 (if ($char< c1 c2)
222 #t
223 (if ($char= c1 c2)
224 (f ($fxadd1 i) n s1 s2)
225 #f)))))
226 (let f ([i 0] [n n2] [s1 s1] [s2 s2])
227 (if ($fx= i n)
228 #f
229 (let ([c1 ($string-ref s1 i)]
230 [c2 ($string-ref s2 i)])
231 (if ($char< c1 c2)
232 #t
233 (if ($char= c1 c2)
234 (f ($fxadd1 i) n s1 s2)
235 #f))))))))
236
237 (define ($string<=? s1 s2)
238 (let ([n1 ($string-length s1)]
239 [n2 ($string-length s2)])
240 (if ($fx<= n1 n2)
241 (let f ([i 0] [n n1] [s1 s1] [s2 s2])
242 (if ($fx= i n)
243 #t
244 (let ([c1 ($string-ref s1 i)]
245 [c2 ($string-ref s2 i)])
246 (if ($char< c1 c2)
247 #t
248 (if ($char= c1 c2)
249 (f ($fxadd1 i) n s1 s2)
250 #f)))))
251 (let f ([i 0] [n n2] [s1 s1] [s2 s2])
252 (if ($fx= i n)
253 #f
254 (let ([c1 ($string-ref s1 i)]
255 [c2 ($string-ref s2 i)])
256 (if ($char< c1 c2)
257 #t
258 (if ($char= c1 c2)
259 (f ($fxadd1 i) n s1 s2)
260 #f))))))))
261
262 (define ($string>? s1 s2)
263 ($string<? s2 s1))
264
265 (define ($string>=? s1 s2)
266 ($string<=? s2 s1))
267
268 (define string<?
269 (case-lambda
270 [(s1 s2)
271 (if (string? s1)
272 (if (string? s2)
273 ($string<? s1 s2)
274 (die 'string<? "not a string" s2))
275 (die 'string<? "not a string" s2))]
276 [(s . s*)
277 (string-cmp 'string<? $string<? s s*)]))
278
279 (define string<=?
280 (case-lambda
281 [(s1 s2)
282 (if (string? s1)
283 (if (string? s2)
284 ($string<=? s1 s2)
285 (die 'string<=? "not a string" s2))
286 (die 'string<=? "not a string" s2))]
287 [(s . s*)
288 (string-cmp 'string<=? $string<=? s s*)]))
289
290 (define string>?
291 (case-lambda
292 [(s1 s2)
293 (if (string? s1)
294 (if (string? s2)
295 ($string>? s1 s2)
296 (die 'string>? "not a string" s2))
297 (die 'string>? "not a string" s2))]
298 [(s . s*)
299 (string-cmp 'string>? $string>? s s*)]))
300
301 (define string>=?
302 (case-lambda
303 [(s1 s2)
304 (if (string? s1)
305 (if (string? s2)
306 ($string>=? s1 s2)
307 (die 'string>=? "not a string" s2))
308 (die 'string>=? "not a string" s2))]
309 [(s . s*)
310 (string-cmp 'string>=? $string>=? s s*)]))
311
312 (define string->list
313 (lambda (x)
314 (unless (string? x)
315 (die 'string->list "not a string" x))
316 (let f ([x x] [i ($string-length x)] [ac '()])
317 (cond
318 [($fxzero? i) ac]
319 [else
320 (let ([i ($fxsub1 i)])
321 (f x i (cons ($string-ref x i) ac)))]))))
322
323
324 (define list->string
325 (letrec ([race
326 (lambda (h t ls n)
327 (if (pair? h)
328 (let ([h ($cdr h)])
329 (if (pair? h)
330 (if (not (eq? h t))
331 (race ($cdr h) ($cdr t) ls ($fx+ n 2))
332 (die 'reverse "circular list" ls))
333 (if (null? h)
334 ($fx+ n 1)
335 (die 'reverse "not a proper list" ls))))
336 (if (null? h)
337 n
338 (die 'reverse "not a proper list" ls))))]
339 [fill
340 (lambda (s i ls)
341 (cond
342 [(null? ls) s]
343 [else
344 (let ([c ($car ls)])
345 (unless (char? c)
346 (die 'list->string "not a character" c))
347 ($string-set! s i c)
348 (fill s ($fxadd1 i) (cdr ls)))]))])
349 (lambda (ls)
350 (let ([n (race ls ls ls 0)])
351 (let ([s ($make-string n)])
352 (fill s 0 ls))))))
353
354 (module (string-append)
355 ;; FIXME: make nonconsing on 0,1,2, and 3 args
356 (define length*
357 (lambda (s* n)
358 (cond
359 [(null? s*) n]
360 [else
361 (let ([a ($car s*)])
362 (unless (string? a)
363 (die 'string-append "not a string" a))
364 (length* ($cdr s*) ($fx+ n ($string-length a))))])))
365 (define fill-string
366 (lambda (s a si sj ai)
367 (unless ($fx= si sj)
368 ($string-set! s si ($string-ref a ai))
369 (fill-string s a ($fxadd1 si) sj ($fxadd1 ai)))))
370 (define fill-strings
371 (lambda (s s* i)
372 (cond
373 [(null? s*) s]
374 [else
375 (let ([a ($car s*)])
376 (let ([n ($string-length a)])
377 (let ([j ($fx+ i n)])
378 (fill-string s a i j 0)
379 (fill-strings s ($cdr s*) j))))])))
380 (define string-append
381 (lambda s*
382 (let ([n (length* s* 0)])
383 (let ([s ($make-string n)])
384 (fill-strings s s* 0))))))
385
386
387 (module (string-for-each)
388 (define who 'string-for-each)
389 (define string-for-each
390 (case-lambda
391 [(p v)
392 (unless (procedure? p)
393 (die who "not a procedure" p))
394 (unless (string? v)
395 (die who "not a string" v))
396 (let f ([p p] [v v] [i 0] [n (string-length v)])
397 (cond
398 [($fx= i n) (void)]
399 [else
400 (p (string-ref v i))
401 (f p v ($fxadd1 i) n)]))]
402 [(p v0 v1)
403 (unless (procedure? p)
404 (die who "not a procedure" p))
405 (unless (string? v0)
406 (die who "not a string" v0))
407 (unless (string? v1)
408 (die who "not a string" v1))
409 (let ([n (string-length v0)])
410 (unless ($fx= n ($string-length v1))
411 (die who "length mismatch" v0 v1))
412 (let f ([p p] [v0 v0] [v1 v1] [i 0] [n n])
413 (cond
414 [($fx= i n) (void)]
415 [else
416 (p ($string-ref v0 i) ($string-ref v1 i))
417 (f p v0 v1 ($fxadd1 i) n)])))]
418 [(p v0 v1 . v*)
419 (unless (procedure? p)
420 (die who "not a procedure" p))
421 (unless (string? v0)
422 (die who "not a string" v0))
423 (unless (string? v1)
424 (die who "not a string" v1))
425 (let ([n (string-length v0)])
426 (unless ($fx= n ($string-length v1))
427 (die who "length mismatch" v0 v1))
428 (let f ([v* v*] [n n])
429 (unless (null? v*)
430 (let ([a ($car v*)])
431 (unless (string? a)
432 (die who "not a string" a))
433 (unless ($fx= ($string-length a) n)
434 (die who "length mismatch")))
435 (f ($cdr v*) n)))
436 (let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n])
437 (cond
438 [($fx= i n) (void)]
439 [else
440 (apply p ($string-ref v0 i) ($string-ref v1 i)
441 (let f ([i i] [v* v*])
442 (if (null? v*)
443 '()
444 (cons ($string-ref ($car v*) i)
445 (f i ($cdr v*))))))
446 (f p v0 v1 v* ($fxadd1 i) n)])))])))
447
448 (define (string-fill! v fill)
449 (unless (string? v)
450 (die 'string-fill! "not a vector" v))
451 (unless (char? fill)
452 (die 'string-fill! "not a character" fill))
453 (let f ([v v] [i 0] [n ($string-length v)] [fill fill])
454 (unless ($fx= i n)
455 ($string-set! v i fill)
456 (f v ($fxadd1 i) n fill))))
457
458
459 (define string-copy!
460 (lambda (src src-start dst dst-start k)
461 (cond
462 [(or (not (fixnum? src-start)) ($fx< src-start 0))
463 (die 'string-copy! "not a valid starting index" src-start)]
464 [(or (not (fixnum? dst-start)) ($fx< dst-start 0))
465 (die 'string-copy! "not a valid starting index" dst-start)]
466 [(or (not (fixnum? k)) ($fx< k 0))
467 (die 'string-copy! "not a valid length" k)]
468 [(not (string? src))
469 (die 'string-copy! "not a string" src)]
470 [(not (string? dst))
471 (die 'string-copy! "not a string" dst)]
472 [(let ([n ($fx+ src-start k)])
473 (or ($fx< n 0) ($fx> n ($string-length src))))
474 (die 'string-copy! "out of range" src-start k)]
475 [(let ([n ($fx+ dst-start k)])
476 (or ($fx< n 0) ($fx> n ($string-length dst))))
477 (die 'string-copy! "out of range" dst-start k)]
478 [(eq? src dst)
479 (cond
480 [($fx< dst-start src-start)
481 (let f ([src src] [si src-start] [di dst-start] [sj ($fx+ src-start k)])
482 (unless ($fx= si sj)
483 ($string-set! src di ($string-ref src si))
484 (f src ($fxadd1 si) ($fxadd1 di) sj)))]
485 [($fx< src-start dst-start)
486 (let f ([src src] [si ($fx+ src-start k)] [di ($fx+ dst-start k)] [sj src-start])
487 (unless ($fx= si sj)
488 (let ([si ($fxsub1 si)] [di ($fxsub1 di)])
489 ($string-set! src di ($string-ref src si))
490 (f src si di sj))))]
491 [else (void)])]
492 [else
493 (let f ([src src] [si src-start] [dst dst] [di dst-start] [sj ($fx+ src-start k)])
494 (unless ($fx= si sj)
495 ($string-set! dst di ($string-ref src si))
496 (f src ($fxadd1 si) dst ($fxadd1 di) sj)))])))
497
498 (define uuid
499 (lambda ()
500 (let ([s ($make-bytevector 16)])
501 (let ([r (foreign-call "ik_uuid" s)])
502 (if (bytevector? r)
503 (utf8->string r)
504 (error 'uuid "cannot obtain unique id"))))))
505 )