-
Notifications
You must be signed in to change notification settings - Fork 0
/
srfi-54.ss
376 lines (365 loc) · 11.1 KB
/
srfi-54.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(import :gerbil/gambit/ports)
(export cat)
(define (cat object . rest)
(let* ((str-rest (part string? rest))
(str-list (car str-rest))
(rest-list (cdr str-rest)))
(if (null? rest-list)
(apply string-append
(cond
((number? object) (number->string object))
((string? object) object)
((char? object) (string object))
((boolean? object) (if object "#t" "#f"))
((symbol? object) (symbol->string object))
(else
(get-output-string
(let ((str-port (open-output-string)))
(write object str-port)
str-port))))
str-list)
(alet-cat* rest-list
((width 0 (and (integer? width) (exact? width)))
(port #f (or (boolean? port) (output-port? port))
(if (eq? port #t) (current-output-port) port))
(char #\space (char? char))
(converter #f (and (pair? converter)
(procedure? (car converter))
(procedure? (cdr converter))))
(precision #f (and (integer? precision)
(inexact? precision)))
(sign #f (eq? 'sign sign))
(radix 'decimal
(memq radix '(decimal octal binary hexadecimal)))
(exactness #f (memq exactness '(exact inexact)))
(separator #f (and (list? separator)
(< 0 (length separator) 3)
(char? (car separator))
(or (null? (cdr separator))
(let ((n (cadr separator)))
(and (integer? n) (exact? n)
(< 0 n))))))
(writer #f (procedure? writer))
(pipe #f (and (list? pipe)
(not (null? pipe))
(every? procedure? pipe)))
(take #f (and (list? take)
(< 0 (length take) 3)
(every? (lambda (x)
(and (integer? x) (exact? x)))
take))))
(let* ((str
(cond
((and converter
((car converter) object))
(let* ((str ((cdr converter) object))
(pad (- (abs width) (string-length str))))
(cond
((<= pad 0) str)
((< 0 width) (string-append (make-string pad char) str))
(else (string-append str (make-string pad char))))))
((number? object)
(and (not (eq? radix 'decimal)) precision
(error "cat: non-decimal cannot have a decimal point"))
(and precision (< precision 0) (eq? exactness 'exact)
(error "cat: exact number cannot have a decimal point without exact sign"))
(let* ((exact-sign (and precision
(<= 0 precision)
(or (eq? exactness 'exact)
(and (exact? object)
(not (eq? exactness
'inexact))))
"#e"))
(inexact-sign (and (not (eq? radix 'decimal))
(or (and (inexact? object)
(not (eq? exactness
'exact)))
(eq? exactness 'inexact))
"#i"))
(radix-sign (cdr (assq radix
'((decimal . #f)
(octal . "#o")
(binary . "#b")
(hexadecimal . "#x")))))
(plus-sign (and sign (< 0 (real-part object)) "+"))
(exactness-sign (or exact-sign inexact-sign))
(str
(if precision
(let ((precision (inexact->exact
(abs precision)))
(imag (imag-part object)))
(if (= 0 imag)
(e-mold object precision)
(string-append
(e-mold (real-part object) precision)
(if (< 0 imag) "+" "")
(e-mold imag precision)
"i")))
(number->string
(cond
(inexact-sign (inexact->exact object))
(exactness
(if (eq? exactness 'exact)
(inexact->exact object)
(exact->inexact object)))
(else object))
(cdr (assq radix '((decimal . 10)
(octal . 8)
(binary . 2)
(hexadecimal . 16)))))))
(str
(if (and separator
(not (or (and (eq? radix 'decimal)
(str-index str #\e))
(str-index str #\i)
(str-index str #\/))))
(let ((sep (string (car separator)))
(num (if (null? (cdr separator))
3 (cadr separator)))
(dot-index (str-index str #\.)))
(if dot-index
(string-append
(separate (substring str 0 dot-index)
sep num (if (< object 0)
'minus #t))
"."
(separate (substring
str (+ 1 dot-index)
(string-length str))
sep num #f))
(separate str sep num (if (< object 0)
'minus #t))))
str))
(pad (- (abs width)
(+ (string-length str)
(if exactness-sign 2 0)
(if radix-sign 2 0)
(if plus-sign 1 0))))
(pad (if (< 0 pad) pad 0)))
(if (< 0 width)
(if (char-numeric? char)
(if (< (real-part object) 0)
(string-append (or exactness-sign "")
(or radix-sign "")
"-"
(make-string pad char)
(substring str 1
(string-length
str)))
(string-append (or exactness-sign "")
(or radix-sign "")
(or plus-sign "")
(make-string pad char)
str))
(string-append (make-string pad char)
(or exactness-sign "")
(or radix-sign "")
(or plus-sign "")
str))
(string-append (or exactness-sign "")
(or radix-sign "")
(or plus-sign "")
str
(make-string pad char)))))
(else
(let* ((str (cond
(writer (get-output-string
(let ((str-port
(open-output-string)))
(writer object str-port)
str-port)))
((string? object) object)
((char? object) (string object))
((boolean? object) (if object "#t" "#f"))
((symbol? object) (symbol->string object))
(else (get-output-string
(let ((str-port (open-output-string)))
(write object str-port)
str-port)))))
(str (if pipe
(let loop ((str ((car pipe) str))
(fns (cdr pipe)))
(if (null? fns)
str
(loop ((car fns) str)
(cdr fns))))
str))
(str
(if take
(let ((left (car take))
(right (if (null? (cdr take))
0 (cadr take)))
(len (string-length str)))
(define (substr str beg end)
(let ((end (cond
((< end 0) 0)
((< len end) len)
(else end)))
(beg (cond
((< beg 0) 0)
((< len beg) len)
(else beg))))
(if (and (= beg 0) (= end len))
str
(substring str beg end))))
(string-append
(if (< left 0)
(substr str (abs left) len)
(substr str 0 left))
(if (< right 0)
(substr str 0 (+ len right))
(substr str (- len right) len))))
str))
(pad (- (abs width) (string-length str))))
(cond
((<= pad 0) str)
((< 0 width) (string-append (make-string pad char) str))
(else (string-append str (make-string pad char))))))))
(str (apply string-append str str-list)))
(and port (display str port))
str)))))
(define-syntax alet-cat* ; borrowed from SRFI-86
(syntax-rules ()
((alet-cat* z (a . e) bd ...)
(let ((y z))
(%alet-cat* y (a . e) bd ...)))))
(define-syntax %alet-cat* ; borrowed from SRFI-86
(syntax-rules ()
((%alet-cat* z ((n d t ...)) bd ...)
(let ((n (if (null? z)
d
(if (null? (cdr z))
(wow-cat-end z n t ...)
(error "cat: too many arguments" (cdr z))))))
bd ...))
((%alet-cat* z ((n d t ...) . e) bd ...)
(let ((n (if (null? z)
d
(wow-cat! z n d t ...))))
(%alet-cat* z e bd ...)))
((%alet-cat* z e bd ...)
(let ((e z)) bd ...))))
(define-syntax wow-cat! ; borrowed from SRFI-86
(syntax-rules ()
((wow-cat! z n d)
(let ((n (car z)))
(set! z (cdr z))
n))
((wow-cat! z n d t)
(let ((n (car z)))
(if t
(begin (set! z (cdr z)) n)
(let lp ((head (list n)) (tail (cdr z)))
(if (null? tail)
d
(let ((n (car tail)))
(if t
(begin (set! z (append (reverse head) (cdr tail))) n)
(lp (cons n head) (cdr tail)))))))))
((wow-cat! z n d t ts)
(let ((n (car z)))
(if t
(begin (set! z (cdr z)) ts)
(let lp ((head (list n)) (tail (cdr z)))
(if (null? tail)
d
(let ((n (car tail)))
(if t
(begin (set! z (append (reverse head) (cdr tail))) ts)
(lp (cons n head) (cdr tail)))))))))
((wow-cat! z n d t ts fs)
(let ((n (car z)))
(if t
(begin (set! z (cdr z)) ts)
(begin (set! z (cdr z)) fs))))))
(define-syntax wow-cat-end ; borrowed from SRFI-86
(syntax-rules ()
((wow-cat-end z n)
(car z))
((wow-cat-end z n t)
(let ((n (car z)))
(if t n (error "cat: too many argument" z))))
((wow-cat-end z n t ts)
(let ((n (car z)))
(if t ts (error "cat: too many argument" z))))
((wow-cat-end z n t ts fs)
(let ((n (car z)))
(if t ts fs)))))
(define (str-index str char)
(let ((len (string-length str)))
(let lp ((n 0))
(and (< n len)
(if (char=? char (string-ref str n))
n
(lp (+ n 1)))))))
(define (every? pred ls)
(let lp ((ls ls))
(or (null? ls)
(and (pred (car ls))
(lp (cdr ls))))))
(define (part pred ls)
(let lp ((ls ls) (true '()) (false '()))
(cond
((null? ls) (cons (reverse true) (reverse false)))
((pred (car ls)) (lp (cdr ls) (cons (car ls) true) false))
(else (lp (cdr ls) true (cons (car ls) false))))))
(define (e-mold num pre)
(let* ((str (number->string (exact->inexact num)))
(e-index (str-index str #\e)))
(if e-index
(string-append (mold (substring str 0 e-index) pre)
(substring str e-index (string-length str)))
(mold str pre))))
(define (mold str pre)
(let ((ind (str-index str #\.)))
(if ind
(let ((d-len (- (string-length str) (+ ind 1))))
(cond
((= d-len pre) str)
((< d-len pre) (string-append str (make-string (- pre d-len) #\0)))
;;((char<? #\4 (string-ref str (+ 1 ind pre)))
;;(let ((com (expt 10 pre)))
;; (number->string (/ (round (* (string->number str) com)) com))))
((or (char<? #\5 (string-ref str (+ 1 ind pre)))
(and (char=? #\5 (string-ref str (+ 1 ind pre)))
(or (< (+ 1 pre) d-len)
(memv (string-ref str (+ ind (if (= 0 pre) -1 pre)))
'(#\1 #\3 #\5 #\7 #\9)))))
(apply
string
(let* ((minus (char=? #\- (string-ref str 0)))
(str (substring str (if minus 1 0) (+ 1 ind pre)))
(char-list
(reverse
(let lp ((index (- (string-length str) 1))
(raise #t))
(if (= -1 index)
(if raise '(#\1) '())
(let ((chr (string-ref str index)))
(if (char=? #\. chr)
(cons chr (lp (- index 1) raise))
(if raise
(if (char=? #\9 chr)
(cons #\0 (lp (- index 1) raise))
(cons (integer->char
(+ 1 (char->integer chr)))
(lp (- index 1) #f)))
(cons chr (lp (- index 1) raise))))))))))
(if minus (cons #\- char-list) char-list))))
(else
(substring str 0 (+ 1 ind pre)))))
(string-append str "." (make-string pre #\0)))))
(define (separate str sep num opt)
(let* ((len (string-length str))
(pos (if opt
(let ((pos (remainder (if (eq? opt 'minus) (- len 1) len)
num)))
(if (= 0 pos) num pos))
num)))
(apply string-append
(let loop ((ini 0)
(pos (if (eq? opt 'minus) (+ pos 1) pos)))
(if (< pos len)
(cons (substring str ini pos)
(cons sep (loop pos (+ pos num))))
(list (substring str ini len)))))))