| 1 |
(define (string-fill! s char . maybe-start+end) |
| 2 |
(check-arg char? char string-fill!) |
| 3 |
(let-string-start+end (start end) string-fill! s maybe-start+end |
| 4 |
(do ((i (- end 1) (- i 1))) |
| 5 |
((< i start)) |
| 6 |
(string-set! s i char)))) |
| 7 |
|
| 8 |
(define (string-copy! to tstart from . maybe-fstart+fend) |
| 9 |
(let-string-start+end (fstart fend) string-copy! from maybe-fstart+fend |
| 10 |
(check-arg integer? tstart string-copy!) |
| 11 |
(check-substring-spec string-copy! to tstart (+ tstart (- fend fstart))) |
| 12 |
(%string-copy! to tstart from fstart fend))) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
i fstart+ i 1 |
| 18 |
j tstart+ j 1 |
| 19 |
>= i fend |
| 20 |
string-set! to jstring-ref from i |
| 21 |
|
| 22 |
i- fend 1- i 1 |
| 23 |
j+ -1 tstart- fend fstart- j 1 |
| 24 |
< i fstart |
| 25 |
string-set! to jstring-ref from i |
| 26 |
|
| 27 |
(define (string->list s . maybe-start+end) |
| 28 |
(let-string-start+end (start end) string->list s maybe-start+end |
| 29 |
(do ((i (- end 1) (- i 1)) |
| 30 |
(ans '() (cons (string-ref s i) ans))) |
| 31 |
((< i start) ans)))) |
| 32 |
|
| 33 |
(define (string->vector s . maybe-start+end) |
| 34 |
(let-string-start+end (start end) string->vector s maybe-start+end |
| 35 |
(let ((vector (make-vector (- end start)))) |
| 36 |
(do ((i (- end 1) (- i 1))) |
| 37 |
((< i start) vector) |
| 38 |
(vector-set! vector (- i start) (string-ref s i)))))) |
| 39 |
|
| 40 |
(define (vector->string vector . maybe-start+end) |
| 41 |
(let ((start 0) (end (vector-length vector))) |
| 42 |
(case (length maybe-start+end) |
| 43 |
((1) (set! start (car maybe-start+end))) |
| 44 |
((2) (set! end (cadr maybe-start+end)))) |
| 45 |
(let ((s (make-string (- end start)))) |
| 46 |
(do ((i (- end 1) (- i 1))) |
| 47 |
((< i start) s) |
| 48 |
(string-set! s (- i start) (vector-ref vector i)))))) |
| 49 |
|
| 50 |
(define (string-map f x . rest) |
| 51 |
|
| 52 |
(define (string-map1 f x) |
| 53 |
(list->string (map f (string->list x)))) |
| 54 |
|
| 55 |
(define (string-map2 f x y) |
| 56 |
(list->string (map f (string->list x) (string->list y)))) |
| 57 |
|
| 58 |
(define (string-mapn f lists) |
| 59 |
(list->string (apply map f (map string->list lists)))) |
| 60 |
|
| 61 |
(case (length rest) |
| 62 |
((0) (string-map1 f x)) |
| 63 |
((1) (string-map2 f x (car rest))) |
| 64 |
(else (string-mapn f (cons x rest))))) |
| 65 |
|
| 66 |
(define (string-for-each f s . rest) |
| 67 |
|
| 68 |
(define (for-each1 i n) |
| 69 |
(if (< i n) |
| 70 |
(begin (f (string-ref s i)) |
| 71 |
(for-each1 (+ i 1) n)) |
| 72 |
(if #f #f))) |
| 73 |
|
| 74 |
(define (for-each2 s2 i n) |
| 75 |
(if (< i n) |
| 76 |
(begin (f (string-ref s i) (string-ref s2 i)) |
| 77 |
(for-each2 s2 (+ i 1) n)) |
| 78 |
(if #f #f))) |
| 79 |
|
| 80 |
(define (for-each-n revstrings i n) |
| 81 |
(if (< i n) |
| 82 |
(do ((rev revstrings (cdr rev)) |
| 83 |
(chars '() (cons (string-ref (car rev) i) chars))) |
| 84 |
((null? rev) |
| 85 |
(apply f chars) |
| 86 |
(for-each-n revstrings (+ i 1) n))) |
| 87 |
(if #f #f))) |
| 88 |
|
| 89 |
(let ((n (string-length s))) |
| 90 |
(cond ((null? rest) |
| 91 |
(for-each1 0 n)) |
| 92 |
((and (null? (cdr rest)) |
| 93 |
(string? (car rest)) |
| 94 |
(= n (string-length (car rest)))) |
| 95 |
(for-each2 (car rest) 0 n)) |
| 96 |
(else |
| 97 |
(let ((args (cons s rest))) |
| 98 |
(do ((ss rest (cdr ss))) |
| 99 |
((null? ss) |
| 100 |
(for-each-n (reverse args) 0 n)) |
| 101 |
(let ((x (car ss))) |
| 102 |
(if (or (not (string? x)) |
| 103 |
(not (= n (string-length x)))) |
| 104 |
(error |
| 105 |
"illegal-arguments" |
| 106 |
(cons f args)))))))))) |
| 107 |
|
| 108 |
(define (string-copy s . maybe-start+end) |
| 109 |
(let-string-start+end (start end) string-copy! s maybe-start+end |
| 110 |
(%substring s start end))) |
| 111 |
|
| 112 |
(cond-expand |
| 113 |
(chicken |
| 114 |
) |
| 115 |
(else |
| 116 |
(define read-string |
| 117 |
(case-lambda |
| 118 |
((k) (read-string k (current-input-port))) |
| 119 |
((k port) |
| 120 |
(let loop ((i 0) (o '())) |
| 121 |
(if (>= i k) |
| 122 |
(list->string (reverse o)) |
| 123 |
(let ((c (read-char port))) |
| 124 |
(if (eof-object? c) |
| 125 |
(if (= i 0) |
| 126 |
c |
| 127 |
(list->string (reverse o))) |
| 128 |
(loop (+ i 1) (cons c o))))))))))) |
| 129 |
|
| 130 |
|
| 131 |
(define write-string |
| 132 |
(case-lambda |
| 133 |
((str) (display str)) |
| 134 |
((str port) (display str port)) |
| 135 |
((str port start) (write-string str port start (string-length str))) |
| 136 |
((str port start end) (display (%substring str start end) port)))) |
| 137 |
|
| 138 |
|
| 139 |
portopen-input-string "" |
| 140 |
|
| 141 |
lambda#f |
| 142 |
lambdaread-char port |
| 143 |
lambdaclose-input-port port |