Debian Codegraph @

%3a54/srfi-54-impl.scm

raw symbols SCIP
1 ;;
2 ;; SRFI 54 Reference Implementation
3 ;;
4 ;; Copyright (C) Joo ChurlSoo (2004). All Rights Reserved.
5 ;;
6 ;; Permission is hereby granted, free of charge, to any person obtaining a copy
7 ;; of this software and associated documentation files (the "Software"), to
8 ;; deal in the Software without restriction, including without limitation the
9 ;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 ;; sell copies of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
12 ;;
13 ;; The above copyright notice and this permission notice shall be included in
14 ;; all copies or substantial portions of the Software.
15 ;;
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ;; IN THE SOFTWARE.
23 ;;
24 ;; 01/07/2018 - AWK - updated get-output-string/open-output-string,
25 ;; exact->inexact, and inexact->exact to R6RS equivalent
26
27 (define (cat object . rest)
28 (let* ((str-rest (part string? rest))
29 (str-list (car str-rest))
30 (rest-list (cdr str-rest)))
31 (if (null? rest-list)
32 (apply string-append
33 (cond
34 ((number? object) (number->string object))
35 ((string? object) object)
36 ((char? object) (string object))
37 ((boolean? object) (if object "#t" "#f"))
38 ((symbol? object) (symbol->string object))
39 (else
40 (let-values (((str-port p) (open-string-output-port)))
41 (write object str-port)
42 (p))))
43 str-list)
44 (alet-cat* rest-list
45 ((width 0 (and (integer? width) (exact? width)))
46 (port #f (or (boolean? port) (output-port? port))
47 (if (eq? port #t) (current-output-port) port))
48 (char #\space (char? char))
49 (converter #f (and (pair? converter)
50 (procedure? (car converter))
51 (procedure? (cdr converter))))
52 (precision #f (and (integer? precision)
53 (inexact? precision)))
54 (sign #f (eq? 'sign sign))
55 (radix 'decimal
56 (memq radix '(decimal octal binary hexadecimal)))
57 (exactness #f (memq exactness '(exact inexact)))
58 (separator #f (and (list? separator)
59 (< 0 (length separator) 3)
60 (char? (car separator))
61 (or (null? (cdr separator))
62 (let ((n (cadr separator)))
63 (and (integer? n) (exact? n)
64 (< 0 n))))))
65 (writer #f (procedure? writer))
66 (pipe #f (and (list? pipe)
67 (not (null? pipe))
68 (every? procedure? pipe)))
69 (take #f (and (list? take)
70 (< 0 (length take) 3)
71 (every? (lambda (x)
72 (and (integer? x) (exact? x)))
73 take))))
74 (let* ((str
75 (cond
76 ((and converter
77 ((car converter) object))
78 (let* ((str ((cdr converter) object))
79 (pad (- (abs width) (string-length str))))
80 (cond
81 ((<= pad 0) str)
82 ((< 0 width) (string-append (make-string pad char) str))
83 (else (string-append str (make-string pad char))))))
84 ((number? object)
85 (and (not (eq? radix 'decimal)) precision
86 (error "cat: non-decimal cannot have a decimal point"))
87 (and precision (< precision 0) (eq? exactness 'exact)
88 (error "cat: exact number cannot have a decimal point without exact sign"))
89 (let* ((exact-sign (and precision
90 (<= 0 precision)
91 (or (eq? exactness 'exact)
92 (and (exact? object)
93 (not (eq? exactness
94 'inexact))))
95 "#e"))
96 (inexact-sign (and (not (eq? radix 'decimal))
97 (or (and (inexact? object)
98 (not (eq? exactness
99 'exact)))
100 (eq? exactness 'inexact))
101 "#i"))
102 (radix-sign (cdr (assq radix
103 '((decimal . #f)
104 (octal . "#o")
105 (binary . "#b")
106 (hexadecimal . "#x")))))
107 (plus-sign (and sign (< 0 (real-part object)) "+"))
108 (exactness-sign (or exact-sign inexact-sign))
109 (str
110 (if precision
111 (let ((precision (exact (abs precision)))
112 (imag (imag-part object)))
113 (if (= 0 imag)
114 (e-mold object precision)
115 (string-append
116 (e-mold (real-part object) precision)
117 (if (< 0 imag) "+" "")
118 (e-mold imag precision)
119 "i")))
120 (number->string
121 (cond
122 (inexact-sign (exact object))
123 (exactness
124 (if (eq? exactness 'exact)
125 (exact object)
126 (inexact object)))
127 (else object))
128 (cdr (assq radix '((decimal . 10)
129 (octal . 8)
130 (binary . 2)
131 (hexadecimal . 16)))))))
132 (str
133 (if (and separator
134 (not (or (and (eq? radix 'decimal)
135 (str-index str #\e))
136 (str-index str #\i)
137 (str-index str #\/))))
138 (let ((sep (string (car separator)))
139 (num (if (null? (cdr separator))
140 3 (cadr separator)))
141 (dot-index (str-index str #\.)))
142 (if dot-index
143 (string-append
144 (separate (substring str 0 dot-index)
145 sep num (if (< object 0)
146 'minus #t))
147 "."
148 (separate (substring
149 str (+ 1 dot-index)
150 (string-length str))
151 sep num #f))
152 (separate str sep num (if (< object 0)
153 'minus #t))))
154 str))
155 (pad (- (abs width)
156 (+ (string-length str)
157 (if exactness-sign 2 0)
158 (if radix-sign 2 0)
159 (if plus-sign 1 0))))
160 (pad (if (< 0 pad) pad 0)))
161 (if (< 0 width)
162 (if (char-numeric? char)
163 (if (< (real-part object) 0)
164 (string-append (or exactness-sign "")
165 (or radix-sign "")
166 "-"
167 (make-string pad char)
168 (substring str 1
169 (string-length
170 str)))
171 (string-append (or exactness-sign "")
172 (or radix-sign "")
173 (or plus-sign "")
174 (make-string pad char)
175 str))
176 (string-append (make-string pad char)
177 (or exactness-sign "")
178 (or radix-sign "")
179 (or plus-sign "")
180 str))
181 (string-append (or exactness-sign "")
182 (or radix-sign "")
183 (or plus-sign "")
184 str
185 (make-string pad char)))))
186 (else
187 (let* ((str (cond
188 (writer (let-values (((str-port p)
189 (open-string-output-port)))
190 (writer object str-port)
191 (p)))
192 ((string? object) object)
193 ((char? object) (string object))
194 ((boolean? object) (if object "#t" "#f"))
195 ((symbol? object) (symbol->string object))
196 (else (let-values (((str-port p) (open-string-output-port)))
197 (write object str-port)
198 (p)))))
199 (str (if pipe
200 (let loop ((str ((car pipe) str))
201 (fns (cdr pipe)))
202 (if (null? fns)
203 str
204 (loop ((car fns) str)
205 (cdr fns))))
206 str))
207 (str
208 (if take
209 (let ((left (car take))
210 (right (if (null? (cdr take))
211 0 (cadr take)))
212 (len (string-length str)))
213 (define (substr str beg end)
214 (let ((end (cond
215 ((< end 0) 0)
216 ((< len end) len)
217 (else end)))
218 (beg (cond
219 ((< beg 0) 0)
220 ((< len beg) len)
221 (else beg))))
222 (if (and (= beg 0) (= end len))
223 str
224 (substring str beg end))))
225 (string-append
226 (if (< left 0)
227 (substr str (abs left) len)
228 (substr str 0 left))
229 (if (< right 0)
230 (substr str 0 (+ len right))
231 (substr str (- len right) len))))
232 str))
233 (pad (- (abs width) (string-length str))))
234 (cond
235 ((<= pad 0) str)
236 ((< 0 width) (string-append (make-string pad char) str))
237 (else (string-append str (make-string pad char))))))))
238 (str (apply string-append str str-list)))
239 (and port (display str port))
240 str)))))
241
242 (define-syntax alet-cat* ; borrowed from SRFI-86
243 (syntax-rules ()
244 ((alet-cat* z (a . e) bd ...)
245 (let ((y z))
246 (%alet-cat* y (a . e) bd ...)))))
247
248 (define-syntax %alet-cat* ; borrowed from SRFI-86
249 (syntax-rules ()
250 ((%alet-cat* z ((n d t ...)) bd ...)
251 (let ((n (if (null? z)
252 d
253 (if (null? (cdr z))
254 (wow-cat-end z n t ...)
255 (error "cat: too many arguments" (cdr z))))))
256 bd ...))
257 ((%alet-cat* z ((n d t ...) . e) bd ...)
258 (let ((n (if (null? z)
259 d
260 (wow-cat! z n d t ...))))
261 (%alet-cat* z e bd ...)))
262 ((%alet-cat* z e bd ...)
263 (let ((e z)) bd ...))))
264
265 (define-syntax wow-cat! ; borrowed from SRFI-86
266 (syntax-rules ()
267 ((wow-cat! z n d)
268 (let ((n (car z)))
269 (set! z (cdr z))
270 n))
271 ((wow-cat! z n d t)
272 (let ((n (car z)))
273 (if t
274 (begin (set! z (cdr z)) n)
275 (let lp ((head (list n)) (tail (cdr z)))
276 (if (null? tail)
277 d
278 (let ((n (car tail)))
279 (if t
280 (begin (set! z (append (reverse head) (cdr tail))) n)
281 (lp (cons n head) (cdr tail)))))))))
282 ((wow-cat! z n d t ts)
283 (let ((n (car z)))
284 (if t
285 (begin (set! z (cdr z)) ts)
286 (let lp ((head (list n)) (tail (cdr z)))
287 (if (null? tail)
288 d
289 (let ((n (car tail)))
290 (if t
291 (begin (set! z (append (reverse head) (cdr tail))) ts)
292 (lp (cons n head) (cdr tail)))))))))
293 ((wow-cat! z n d t ts fs)
294 (let ((n (car z)))
295 (if t
296 (begin (set! z (cdr z)) ts)
297 (begin (set! z (cdr z)) fs))))))
298
299 (define-syntax wow-cat-end ; borrowed from SRFI-86
300 (syntax-rules ()
301 ((wow-cat-end z n)
302 (car z))
303 ((wow-cat-end z n t)
304 (let ((n (car z)))
305 (if t n (error "cat: too many argument" z))))
306 ((wow-cat-end z n t ts)
307 (let ((n (car z)))
308 (if t ts (error "cat: too many argument" z))))
309 ((wow-cat-end z n t ts fs)
310 (let ((n (car z)))
311 (if t ts fs)))))
312
313 (define (str-index str char)
314 (let ((len (string-length str)))
315 (let lp ((n 0))
316 (and (< n len)
317 (if (char=? char (string-ref str n))
318 n
319 (lp (+ n 1)))))))
320
321 (define (every? pred ls)
322 (let lp ((ls ls))
323 (or (null? ls)
324 (and (pred (car ls))
325 (lp (cdr ls))))))
326
327 (define (part pred ls)
328 (let lp ((ls ls) (true '()) (false '()))
329 (cond
330 ((null? ls) (cons (reverse true) (reverse false)))
331 ((pred (car ls)) (lp (cdr ls) (cons (car ls) true) false))
332 (else (lp (cdr ls) true (cons (car ls) false))))))
333
334 (define (e-mold num pre)
335 (let* ((str (number->string (inexact num)))
336 (e-index (str-index str #\e)))
337 (if e-index
338 (string-append (mold (substring str 0 e-index) pre)
339 (substring str e-index (string-length str)))
340 (mold str pre))))
341
342 (define (mold str pre)
343 (let ((ind (str-index str #\.)))
344 (if ind
345 (let ((d-len (- (string-length str) (+ ind 1))))
346 (cond
347 ((= d-len pre) str)
348 ((< d-len pre) (string-append str (make-string (- pre d-len) #\0)))
349 ;;((char<? #\4 (string-ref str (+ 1 ind pre)))
350 ;;(let ((com (expt 10 pre)))
351 ;; (number->string (/ (round (* (string->number str) com)) com))))
352 ((or (char<? #\5 (string-ref str (+ 1 ind pre)))
353 (and (char=? #\5 (string-ref str (+ 1 ind pre)))
354 (or (< (+ 1 pre) d-len)
355 (memv (string-ref str (+ ind (if (= 0 pre) -1 pre)))
356 '(#\1 #\3 #\5 #\7 #\9)))))
357 (apply
358 string
359 (let* ((minus (char=? #\- (string-ref str 0)))
360 (str (substring str (if minus 1 0) (+ 1 ind pre)))
361 (char-list
362 (reverse
363 (let lp ((index (- (string-length str) 1))
364 (raise #t))
365 (if (= -1 index)
366 (if raise '(#\1) '())
367 (let ((chr (string-ref str index)))
368 (if (char=? #\. chr)
369 (cons chr (lp (- index 1) raise))
370 (if raise
371 (if (char=? #\9 chr)
372 (cons #\0 (lp (- index 1) raise))
373 (cons (integer->char
374 (+ 1 (char->integer chr)))
375 (lp (- index 1) #f)))
376 (cons chr (lp (- index 1) raise))))))))))
377 (if minus (cons #\- char-list) char-list))))
378 (else
379 (substring str 0 (+ 1 ind pre)))))
380 (string-append str "." (make-string pre #\0)))))
381
382 (define (separate str sep num opt)
383 (let* ((len (string-length str))
384 (pos (if opt
385 (let ((pos (remainder (if (eq? opt 'minus) (- len 1) len)
386 num)))
387 (if (= 0 pos) num pos))
388 num)))
389 (apply string-append
390 (let loop ((ini 0)
391 (pos (if (eq? opt 'minus) (+ pos 1) pos)))
392 (if (< pos len)
393 (cons (substring str ini pos)
394 (cons sep (loop pos (+ pos num))))
395 (list (substring str ini len)))))))
396