2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 03:46:26 -04:00
|
|
|
|
|
|
|
(library (ikarus strings)
|
2007-09-03 00:17:15 -04:00
|
|
|
(export string-length string-ref string-set! make-string string->list
|
2007-08-28 12:46:12 -04:00
|
|
|
string-append substring string list->string uuid
|
2007-09-03 00:17:15 -04:00
|
|
|
string-copy string-for-each string-fill!
|
2007-12-10 07:28:03 -05:00
|
|
|
string=? string<? string<=? string>? string>=?
|
|
|
|
string-copy!)
|
2007-05-05 03:46:26 -04:00
|
|
|
(import
|
2007-05-06 17:41:59 -04:00
|
|
|
(ikarus system $strings)
|
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $chars)
|
2007-05-18 22:31:00 -04:00
|
|
|
(ikarus system $bytevectors)
|
2007-05-06 17:41:59 -04:00
|
|
|
(ikarus system $pairs)
|
2007-05-05 03:58:40 -04:00
|
|
|
(except (ikarus) string-length string-ref string-set! make-string
|
2007-09-03 00:17:15 -04:00
|
|
|
string->list string-append substring string
|
2007-09-02 02:22:23 -04:00
|
|
|
list->string uuid string-copy string-for-each
|
2007-09-03 00:17:15 -04:00
|
|
|
string=? string<? string<=? string>? string>=?
|
2007-12-10 07:28:03 -05:00
|
|
|
string-fill! string-copy!))
|
2007-05-05 03:49:51 -04:00
|
|
|
|
2007-05-05 03:51:38 -04:00
|
|
|
|
|
|
|
(define string-length
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-length "not a string" x))
|
2007-05-05 03:51:38 -04:00
|
|
|
($string-length x)))
|
|
|
|
|
|
|
|
|
2007-05-05 03:46:26 -04:00
|
|
|
(define (string-ref s i)
|
|
|
|
(unless (string? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-ref "not a string" s))
|
2007-05-05 03:46:26 -04:00
|
|
|
(unless (fixnum? i)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-ref "not a valid index" i))
|
2007-05-05 03:46:26 -04:00
|
|
|
(unless (and ($fx< i ($string-length s))
|
|
|
|
($fx<= 0 i))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-ref "index is out of range" i s))
|
2007-05-19 14:38:45 -04:00
|
|
|
(let ([c ($string-ref s i)])
|
|
|
|
(unless (char? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-ref "BUG: got a non-char"))
|
2007-05-19 14:38:45 -04:00
|
|
|
c))
|
2007-05-05 03:49:51 -04:00
|
|
|
|
2007-05-05 03:58:40 -04:00
|
|
|
|
|
|
|
(define string-set!
|
|
|
|
(lambda (s i c)
|
|
|
|
(unless (string? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-set! "not a string" s))
|
2007-05-05 03:58:40 -04:00
|
|
|
(unless (fixnum? i)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-set! "not a valid index" i))
|
2007-05-05 03:58:40 -04:00
|
|
|
(unless (and ($fx< i ($string-length s))
|
|
|
|
($fx>= i 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-set! "index is out of range" i s))
|
2007-05-05 03:58:40 -04:00
|
|
|
(unless (char? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-set! "not a character" c))
|
2007-05-05 03:58:40 -04:00
|
|
|
($string-set! s i c)))
|
2007-05-05 03:49:51 -04:00
|
|
|
|
|
|
|
(define make-string
|
|
|
|
(let ()
|
|
|
|
(define fill!
|
|
|
|
(lambda (s i n c)
|
|
|
|
(cond
|
|
|
|
[($fx= i n) s]
|
|
|
|
[else
|
|
|
|
($string-set! s i c)
|
|
|
|
(fill! s ($fx+ i 1) n c)])))
|
|
|
|
(define make-string
|
|
|
|
(case-lambda
|
|
|
|
[(n)
|
|
|
|
(unless (and (fixnum? n) (fx>= n 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'make-string "not a valid length" n))
|
2007-09-02 02:22:23 -04:00
|
|
|
(fill! ($make-string n) 0 n (integer->char 0))]
|
2007-05-05 03:49:51 -04:00
|
|
|
[(n c)
|
|
|
|
(unless (and (fixnum? n) (fx>= n 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'make-string "not a valid length" n))
|
2007-05-05 03:49:51 -04:00
|
|
|
(unless (char? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'make-string "not a character" c))
|
2007-05-05 03:49:51 -04:00
|
|
|
(fill! ($make-string n) 0 n c)]))
|
|
|
|
make-string))
|
2007-05-05 03:53:19 -04:00
|
|
|
|
|
|
|
|
2007-05-05 04:04:58 -04:00
|
|
|
(define string
|
|
|
|
;;; FIXME: add case-lambda
|
|
|
|
(letrec ([length
|
|
|
|
(lambda (ls n)
|
|
|
|
(cond
|
|
|
|
[(null? ls) n]
|
|
|
|
[(char? ($car ls)) (length ($cdr ls) ($fx+ n 1))]
|
2007-12-15 08:22:49 -05:00
|
|
|
[else (die 'string "not a character" ($car ls))]))]
|
2007-05-05 04:04:58 -04:00
|
|
|
[loop
|
|
|
|
(lambda (s ls i n)
|
|
|
|
(cond
|
|
|
|
[($fx= i n) s]
|
|
|
|
[else
|
|
|
|
($string-set! s i ($car ls))
|
|
|
|
(loop s ($cdr ls) ($fx+ i 1) n)]))])
|
|
|
|
(lambda ls
|
|
|
|
(let ([n (length ls 0)])
|
|
|
|
(let ([s (make-string n)])
|
|
|
|
(loop s ls 0 n))))))
|
|
|
|
|
2007-05-05 04:02:27 -04:00
|
|
|
(module (substring)
|
|
|
|
(define fill
|
|
|
|
(lambda (s d si sj di)
|
|
|
|
(cond
|
|
|
|
[($fx= si sj) d]
|
|
|
|
[else
|
|
|
|
($string-set! d di ($string-ref s si))
|
|
|
|
(fill s d ($fxadd1 si) sj ($fxadd1 di))])))
|
|
|
|
(define substring
|
|
|
|
(lambda (s n m)
|
|
|
|
(unless (string? s)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'substring "not a string" s))
|
2007-05-05 04:02:27 -04:00
|
|
|
(let ([len ($string-length s)])
|
|
|
|
(unless (and (fixnum? n)
|
|
|
|
($fx>= n 0)
|
2007-11-13 12:21:27 -05:00
|
|
|
($fx<= n len))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'substring "not a valid start index" n s))
|
2007-05-05 04:02:27 -04:00
|
|
|
(unless (and (fixnum? m)
|
|
|
|
($fx>= m 0)
|
|
|
|
($fx<= m len))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'substring "not a valid end index" m s))
|
2007-11-13 12:21:27 -05:00
|
|
|
(unless ($fx<= n m)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'substring "indices are in decreasing order" n m))
|
2007-05-05 04:02:27 -04:00
|
|
|
(let ([len ($fx- m n)])
|
2007-11-13 12:21:27 -05:00
|
|
|
(if ($fx> len 0)
|
|
|
|
(fill s ($make-string len) n m 0)
|
|
|
|
""))))))
|
2007-05-05 04:02:27 -04:00
|
|
|
|
2007-08-28 12:46:12 -04:00
|
|
|
(define string-copy
|
|
|
|
(lambda (s)
|
|
|
|
(if (string? s)
|
|
|
|
(substring s 0 (string-length s))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy "not a string" s))))
|
2007-08-28 12:46:12 -04:00
|
|
|
|
2007-05-05 03:57:14 -04:00
|
|
|
(module (string=?)
|
|
|
|
(define bstring=?
|
|
|
|
(lambda (s1 s2 i j)
|
|
|
|
(or ($fx= i j)
|
|
|
|
(and ($char= ($string-ref s1 i) ($string-ref s2 i))
|
|
|
|
(bstring=? s1 s2 ($fxadd1 i) j)))))
|
|
|
|
(define check-strings-and-return-false
|
|
|
|
(lambda (s*)
|
|
|
|
(cond
|
|
|
|
[(null? s*) #f]
|
|
|
|
[(string? ($car s*))
|
|
|
|
(check-strings-and-return-false ($cdr s*))]
|
|
|
|
[else (err ($car s*))])))
|
|
|
|
(define strings=?
|
|
|
|
(lambda (s s* n)
|
|
|
|
(or (null? s*)
|
|
|
|
(let ([a ($car s*)])
|
|
|
|
(unless (string? a)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string=? "not a string" a))
|
2007-05-05 03:57:14 -04:00
|
|
|
(if ($fx= n ($string-length a))
|
|
|
|
(and (strings=? s ($cdr s*) n)
|
|
|
|
(bstring=? s a 0 n))
|
|
|
|
(check-strings-and-return-false ($cdr s*)))))))
|
|
|
|
(define (err x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string=? "not a string" x))
|
2007-05-05 03:57:14 -04:00
|
|
|
(define string=?
|
|
|
|
(case-lambda
|
|
|
|
[(s s1)
|
|
|
|
(if (string? s)
|
|
|
|
(if (string? s1)
|
|
|
|
(let ([n ($string-length s)])
|
|
|
|
(and ($fx= n ($string-length s1))
|
|
|
|
(bstring=? s s1 0 n)))
|
|
|
|
(err s1))
|
|
|
|
(err s))]
|
|
|
|
[(s . s*)
|
|
|
|
(if (string? s)
|
|
|
|
(strings=? s s* ($string-length s))
|
|
|
|
(err s))])))
|
|
|
|
|
2007-09-03 00:17:15 -04:00
|
|
|
|
|
|
|
(define string-cmp
|
2007-09-04 21:01:30 -04:00
|
|
|
(lambda (who cmp s1 s*)
|
|
|
|
(if (string? s1)
|
|
|
|
(let f ([s1 s1] [s* s*])
|
|
|
|
(cond
|
|
|
|
[(null? s*) #t]
|
|
|
|
[else
|
|
|
|
(let ([s2 (car s*)])
|
|
|
|
(if (string? s2)
|
|
|
|
(if (cmp s1 s2)
|
|
|
|
(f s2 (cdr s*))
|
|
|
|
(let f ([s* (cdr s*)])
|
|
|
|
(cond
|
|
|
|
[(null? s*) #f]
|
|
|
|
[(string? (car s*))
|
|
|
|
(f (cdr s*))]
|
|
|
|
[else
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string"
|
2008-11-15 10:59:01 -05:00
|
|
|
(car s*))])))
|
|
|
|
(die who "not a string" s2)))]))
|
2008-11-15 13:26:23 -05:00
|
|
|
(die who "not a string" s1))))
|
2007-09-03 00:17:15 -04:00
|
|
|
|
|
|
|
(define ($string<? s1 s2)
|
|
|
|
(let ([n1 ($string-length s1)]
|
|
|
|
[n2 ($string-length s2)])
|
|
|
|
(if ($fx< n1 n2)
|
|
|
|
(let f ([i 0] [n n1] [s1 s1] [s2 s2])
|
|
|
|
(if ($fx= i n)
|
|
|
|
#t
|
|
|
|
(let ([c1 ($string-ref s1 i)]
|
|
|
|
[c2 ($string-ref s2 i)])
|
|
|
|
(if ($char< c1 c2)
|
|
|
|
#t
|
|
|
|
(if ($char= c1 c2)
|
|
|
|
(f ($fxadd1 i) n s1 s2)
|
|
|
|
#f)))))
|
|
|
|
(let f ([i 0] [n n2] [s1 s1] [s2 s2])
|
|
|
|
(if ($fx= i n)
|
|
|
|
#f
|
|
|
|
(let ([c1 ($string-ref s1 i)]
|
|
|
|
[c2 ($string-ref s2 i)])
|
|
|
|
(if ($char< c1 c2)
|
|
|
|
#t
|
|
|
|
(if ($char= c1 c2)
|
|
|
|
(f ($fxadd1 i) n s1 s2)
|
|
|
|
#f))))))))
|
|
|
|
|
|
|
|
(define ($string<=? s1 s2)
|
|
|
|
(let ([n1 ($string-length s1)]
|
|
|
|
[n2 ($string-length s2)])
|
|
|
|
(if ($fx<= n1 n2)
|
|
|
|
(let f ([i 0] [n n1] [s1 s1] [s2 s2])
|
|
|
|
(if ($fx= i n)
|
|
|
|
#t
|
|
|
|
(let ([c1 ($string-ref s1 i)]
|
|
|
|
[c2 ($string-ref s2 i)])
|
|
|
|
(if ($char< c1 c2)
|
|
|
|
#t
|
|
|
|
(if ($char= c1 c2)
|
|
|
|
(f ($fxadd1 i) n s1 s2)
|
|
|
|
#f)))))
|
|
|
|
(let f ([i 0] [n n2] [s1 s1] [s2 s2])
|
|
|
|
(if ($fx= i n)
|
|
|
|
#f
|
|
|
|
(let ([c1 ($string-ref s1 i)]
|
|
|
|
[c2 ($string-ref s2 i)])
|
|
|
|
(if ($char< c1 c2)
|
|
|
|
#t
|
|
|
|
(if ($char= c1 c2)
|
|
|
|
(f ($fxadd1 i) n s1 s2)
|
|
|
|
#f))))))))
|
2007-09-04 21:01:30 -04:00
|
|
|
|
2007-09-03 00:17:15 -04:00
|
|
|
(define ($string>? s1 s2)
|
|
|
|
($string<? s2 s1))
|
2007-09-04 21:01:30 -04:00
|
|
|
|
2007-09-03 00:17:15 -04:00
|
|
|
(define ($string>=? s1 s2)
|
|
|
|
($string<=? s2 s1))
|
|
|
|
|
2007-09-04 21:01:30 -04:00
|
|
|
(define string<?
|
|
|
|
(case-lambda
|
|
|
|
[(s1 s2)
|
|
|
|
(if (string? s1)
|
|
|
|
(if (string? s2)
|
|
|
|
($string<? s1 s2)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string<? "not a string" s2))
|
|
|
|
(die 'string<? "not a string" s2))]
|
2007-09-04 21:01:30 -04:00
|
|
|
[(s . s*)
|
|
|
|
(string-cmp 'string<? $string<? s s*)]))
|
|
|
|
|
|
|
|
(define string<=?
|
|
|
|
(case-lambda
|
|
|
|
[(s1 s2)
|
|
|
|
(if (string? s1)
|
|
|
|
(if (string? s2)
|
|
|
|
($string<=? s1 s2)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string<=? "not a string" s2))
|
|
|
|
(die 'string<=? "not a string" s2))]
|
2007-09-04 21:01:30 -04:00
|
|
|
[(s . s*)
|
|
|
|
(string-cmp 'string<=? $string<=? s s*)]))
|
|
|
|
|
|
|
|
(define string>?
|
|
|
|
(case-lambda
|
|
|
|
[(s1 s2)
|
|
|
|
(if (string? s1)
|
|
|
|
(if (string? s2)
|
|
|
|
($string>? s1 s2)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string>? "not a string" s2))
|
|
|
|
(die 'string>? "not a string" s2))]
|
2007-09-04 21:01:30 -04:00
|
|
|
[(s . s*)
|
|
|
|
(string-cmp 'string>? $string>? s s*)]))
|
|
|
|
|
|
|
|
(define string>=?
|
|
|
|
(case-lambda
|
|
|
|
[(s1 s2)
|
|
|
|
(if (string? s1)
|
|
|
|
(if (string? s2)
|
|
|
|
($string>=? s1 s2)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string>=? "not a string" s2))
|
|
|
|
(die 'string>=? "not a string" s2))]
|
2007-09-04 21:01:30 -04:00
|
|
|
[(s . s*)
|
|
|
|
(string-cmp 'string>=? $string>=? s s*)]))
|
2007-09-03 00:17:15 -04:00
|
|
|
|
2007-05-05 03:53:19 -04:00
|
|
|
(define string->list
|
|
|
|
(lambda (x)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string->list "not a string" x))
|
2007-05-05 03:53:19 -04:00
|
|
|
(let f ([x x] [i ($string-length x)] [ac '()])
|
|
|
|
(cond
|
|
|
|
[($fxzero? i) ac]
|
|
|
|
[else
|
|
|
|
(let ([i ($fxsub1 i)])
|
|
|
|
(f x i (cons ($string-ref x i) ac)))]))))
|
|
|
|
|
|
|
|
|
2007-05-05 04:06:23 -04:00
|
|
|
(define list->string
|
|
|
|
(letrec ([race
|
|
|
|
(lambda (h t ls n)
|
|
|
|
(if (pair? h)
|
|
|
|
(let ([h ($cdr h)])
|
|
|
|
(if (pair? h)
|
|
|
|
(if (not (eq? h t))
|
|
|
|
(race ($cdr h) ($cdr t) ls ($fx+ n 2))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'reverse "circular list" ls))
|
2007-05-05 04:06:23 -04:00
|
|
|
(if (null? h)
|
|
|
|
($fx+ n 1)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'reverse "not a proper list" ls))))
|
2007-05-05 04:06:23 -04:00
|
|
|
(if (null? h)
|
|
|
|
n
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'reverse "not a proper list" ls))))]
|
2007-05-05 04:06:23 -04:00
|
|
|
[fill
|
|
|
|
(lambda (s i ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) s]
|
|
|
|
[else
|
|
|
|
(let ([c ($car ls)])
|
|
|
|
(unless (char? c)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'list->string "not a character" c))
|
2007-05-05 04:06:23 -04:00
|
|
|
($string-set! s i c)
|
|
|
|
(fill s ($fxadd1 i) (cdr ls)))]))])
|
|
|
|
(lambda (ls)
|
|
|
|
(let ([n (race ls ls ls 0)])
|
|
|
|
(let ([s ($make-string n)])
|
|
|
|
(fill s 0 ls))))))
|
|
|
|
|
2007-05-05 04:00:32 -04:00
|
|
|
(module (string-append)
|
|
|
|
;; FIXME: make nonconsing on 0,1,2, and 3 args
|
|
|
|
(define length*
|
|
|
|
(lambda (s* n)
|
|
|
|
(cond
|
|
|
|
[(null? s*) n]
|
|
|
|
[else
|
|
|
|
(let ([a ($car s*)])
|
|
|
|
(unless (string? a)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-append "not a string" a))
|
2007-05-05 04:00:32 -04:00
|
|
|
(length* ($cdr s*) ($fx+ n ($string-length a))))])))
|
|
|
|
(define fill-string
|
|
|
|
(lambda (s a si sj ai)
|
|
|
|
(unless ($fx= si sj)
|
|
|
|
($string-set! s si ($string-ref a ai))
|
|
|
|
(fill-string s a ($fxadd1 si) sj ($fxadd1 ai)))))
|
|
|
|
(define fill-strings
|
|
|
|
(lambda (s s* i)
|
|
|
|
(cond
|
|
|
|
[(null? s*) s]
|
|
|
|
[else
|
|
|
|
(let ([a ($car s*)])
|
|
|
|
(let ([n ($string-length a)])
|
|
|
|
(let ([j ($fx+ i n)])
|
|
|
|
(fill-string s a i j 0)
|
|
|
|
(fill-strings s ($cdr s*) j))))])))
|
|
|
|
(define string-append
|
|
|
|
(lambda s*
|
|
|
|
(let ([n (length* s* 0)])
|
|
|
|
(let ([s ($make-string n)])
|
|
|
|
(fill-strings s s* 0))))))
|
2007-05-05 03:53:19 -04:00
|
|
|
|
2007-05-05 04:07:48 -04:00
|
|
|
|
2007-08-28 17:45:54 -04:00
|
|
|
(module (string-for-each)
|
|
|
|
(define who 'string-for-each)
|
|
|
|
(define string-for-each
|
|
|
|
(case-lambda
|
|
|
|
[(p v)
|
|
|
|
(unless (procedure? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a procedure" p))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless (string? v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" v))
|
2007-08-28 17:45:54 -04:00
|
|
|
(let f ([p p] [v v] [i 0] [n (string-length v)])
|
|
|
|
(cond
|
|
|
|
[($fx= i n) (void)]
|
|
|
|
[else
|
|
|
|
(p (string-ref v i))
|
|
|
|
(f p v ($fxadd1 i) n)]))]
|
|
|
|
[(p v0 v1)
|
|
|
|
(unless (procedure? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a procedure" p))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless (string? v0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" v0))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless (string? v1)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" v1))
|
2007-08-28 17:45:54 -04:00
|
|
|
(let ([n (string-length v0)])
|
|
|
|
(unless ($fx= n ($string-length v1))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "length mismatch" v0 v1))
|
2007-08-28 17:45:54 -04:00
|
|
|
(let f ([p p] [v0 v0] [v1 v1] [i 0] [n n])
|
|
|
|
(cond
|
|
|
|
[($fx= i n) (void)]
|
|
|
|
[else
|
|
|
|
(p ($string-ref v0 i) ($string-ref v1 i))
|
|
|
|
(f p v0 v1 ($fxadd1 i) n)])))]
|
|
|
|
[(p v0 v1 . v*)
|
|
|
|
(unless (procedure? p)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a procedure" p))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless (string? v0)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" v0))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless (string? v1)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" v1))
|
2007-08-28 17:45:54 -04:00
|
|
|
(let ([n (string-length v0)])
|
|
|
|
(unless ($fx= n ($string-length v1))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "length mismatch" v0 v1))
|
2007-08-28 17:45:54 -04:00
|
|
|
(let f ([v* v*] [n n])
|
|
|
|
(unless (null? v*)
|
|
|
|
(let ([a ($car v*)])
|
|
|
|
(unless (string? a)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "not a string" a))
|
2007-08-28 17:45:54 -04:00
|
|
|
(unless ($fx= ($string-length a) n)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die who "length mismatch")))
|
2007-08-28 17:45:54 -04:00
|
|
|
(f ($cdr v*) n)))
|
|
|
|
(let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n])
|
|
|
|
(cond
|
|
|
|
[($fx= i n) (void)]
|
|
|
|
[else
|
|
|
|
(apply p ($string-ref v0 i) ($string-ref v1 i)
|
|
|
|
(let f ([i i] [v* v*])
|
|
|
|
(if (null? v*)
|
|
|
|
'()
|
|
|
|
(cons ($string-ref ($car v*) i)
|
|
|
|
(f i ($cdr v*))))))
|
|
|
|
(f p v0 v1 v* ($fxadd1 i) n)])))])))
|
|
|
|
|
2007-09-02 02:22:23 -04:00
|
|
|
(define (string-fill! v fill)
|
|
|
|
(unless (string? v)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-fill! "not a vector" v))
|
2007-09-02 02:22:23 -04:00
|
|
|
(unless (char? fill)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-fill! "not a character" fill))
|
2007-09-02 02:22:23 -04:00
|
|
|
(let f ([v v] [i 0] [n ($string-length v)] [fill fill])
|
|
|
|
(unless ($fx= i n)
|
|
|
|
($string-set! v i fill)
|
|
|
|
(f v ($fxadd1 i) n fill))))
|
|
|
|
|
2007-12-10 07:28:03 -05:00
|
|
|
|
|
|
|
(define string-copy!
|
|
|
|
(lambda (src src-start dst dst-start k)
|
|
|
|
(cond
|
|
|
|
[(or (not (fixnum? src-start)) ($fx< src-start 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "not a valid starting index" src-start)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(or (not (fixnum? dst-start)) ($fx< dst-start 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "not a valid starting index" dst-start)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(or (not (fixnum? k)) ($fx< k 0))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "not a valid length" k)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(not (string? src))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "not a string" src)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(not (string? dst))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "not a string" dst)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(let ([n ($fx+ src-start k)])
|
|
|
|
(or ($fx< n 0) ($fx> n ($string-length src))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "out of range" src-start k)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(let ([n ($fx+ dst-start k)])
|
|
|
|
(or ($fx< n 0) ($fx> n ($string-length dst))))
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'string-copy! "out of range" dst-start k)]
|
2007-12-10 07:28:03 -05:00
|
|
|
[(eq? src dst)
|
|
|
|
(cond
|
|
|
|
[($fx< dst-start src-start)
|
|
|
|
(let f ([src src] [si src-start] [di dst-start] [sj ($fx+ src-start k)])
|
|
|
|
(unless ($fx= si sj)
|
|
|
|
($string-set! src di ($string-ref src si))
|
|
|
|
(f src ($fxadd1 si) ($fxadd1 di) sj)))]
|
|
|
|
[($fx< src-start dst-start)
|
|
|
|
(let f ([src src] [si ($fx+ src-start k)] [di ($fx+ dst-start k)] [sj src-start])
|
|
|
|
(unless ($fx= si sj)
|
|
|
|
(let ([si ($fxsub1 si)] [di ($fxsub1 di)])
|
|
|
|
($string-set! src di ($string-ref src si))
|
|
|
|
(f src si di sj))))]
|
|
|
|
[else (void)])]
|
|
|
|
[else
|
|
|
|
(let f ([src src] [si src-start] [dst dst] [di dst-start] [sj ($fx+ src-start k)])
|
|
|
|
(unless ($fx= si sj)
|
|
|
|
($string-set! dst di ($string-ref src si))
|
|
|
|
(f src ($fxadd1 si) dst ($fxadd1 di) sj)))])))
|
|
|
|
|
2007-05-05 04:07:48 -04:00
|
|
|
(define uuid
|
|
|
|
(lambda ()
|
2007-05-18 22:31:00 -04:00
|
|
|
(let ([s ($make-bytevector 16)])
|
2008-06-13 08:43:17 -04:00
|
|
|
(let ([r (foreign-call "ik_uuid" s)])
|
|
|
|
(if (bytevector? r)
|
|
|
|
(utf8->string r)
|
|
|
|
(error 'uuid "cannot obtain unique id"))))))
|
2007-05-05 03:49:51 -04:00
|
|
|
)
|