-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.scm
1634 lines (1510 loc) · 54.3 KB
/
format.scm
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; "format.scm" Common LISP text output formatter for SLIB
; Written 1992-1994 by Dirk Lutzebaeck ([email protected])
; 2004 Aubrey Jaffer: made reentrant; call slib:error for errors.
;
; This code is in the public domain.
; Authors of the original version (< 1.4) were Ken Dickey and Aubrey Jaffer.
; Please send error reports to the email address above.
; For documentation see slib.texi and format.doc.
; For testing load formatst.scm.
;
; Version 3.0
(require 'string-case)
(require 'string-port)
(require 'multiarg/and-)
(require 'rev4-optional-procedures)
(require-if 'compiling 'pretty-print)
;;; Configuration ------------------------------------------------------------
(define format:symbol-case-conv #f)
;; Symbols are converted by symbol->string so the case of the printed
;; symbols is implementation dependent. format:symbol-case-conv is a
;; one arg closure which is either #f (no conversion), string-upcase!,
;; string-downcase! or string-capitalize!.
(define format:iobj-case-conv #f)
;; As format:symbol-case-conv but applies for the representation of
;; implementation internal objects.
(define format:expch #\E)
;; The character prefixing the exponent value in ~e printing.
(define format:iteration-bounded #t)
;; If #t, "~{...~}" iterates no more than format:max-iterations times;
;; if #f, there is no bound.
(define format:max-iterations 100)
;; Compatible with previous versions.
(define format:floats (provided? 'inexact))
;; Detects if the scheme system implements flonums (see at eof).
(define format:complex-numbers (provided? 'complex))
;; Detects if the scheme system implements complex numbers.
(define format:radix-pref (char=? #\# (string-ref (number->string 8 8) 0)))
;; Detects if number->string adds a radix prefix.
(define format:ascii-non-printable-charnames
'#("nul" "soh" "stx" "etx" "eot" "enq" "ack" "bel"
"bs" "ht" "nl" "vt" "np" "cr" "so" "si"
"dle" "dc1" "dc2" "dc3" "dc4" "nak" "syn" "etb"
"can" "em" "sub" "esc" "fs" "gs" "rs" "us" "space"))
(define format:fn-max 200) ; max. number of number digits
(define format:en-max 10) ; max. number of exponent digits
;;; End of configuration ----------------------------------------------------
(define format:version "3.1")
(define format:space-ch (char->integer #\space))
(define format:zero-ch (char->integer #\0))
(define format:parameter-characters
'(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\- #\+ #\v #\# #\'))
;; cardinals & ordinals (from [email protected])
(define format:cardinal-thousand-block-list
'("" " thousand" " million" " billion" " trillion" " quadrillion"
" quintillion" " sextillion" " septillion" " octillion" " nonillion"
" decillion" " undecillion" " duodecillion" " tredecillion"
" quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
" octodecillion" " novemdecillion" " vigintillion"))
(define format:cardinal-ones-list
'(#f "one" "two" "three" "four" "five"
"six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen"
"fourteen" "fifteen" "sixteen" "seventeen" "eighteen"
"nineteen"))
(define format:cardinal-tens-list
'(#f #f "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty"
"ninety"))
(define format:ordinal-ones-list
'(#f "first" "second" "third" "fourth" "fifth"
"sixth" "seventh" "eighth" "ninth" "tenth" "eleventh" "twelfth"
"thirteenth" "fourteenth" "fifteenth" "sixteenth" "seventeenth"
"eighteenth" "nineteenth"))
(define format:ordinal-tens-list
'(#f #f "twentieth" "thirtieth" "fortieth" "fiftieth" "sixtieth"
"seventieth" "eightieth" "ninetieth"))
;; roman numerals (from [email protected]).
(define format:roman-alist
'((1000 #\M) (500 #\D) (100 #\C) (50 #\L)
(10 #\X) (5 #\V) (1 #\I)))
(define format:roman-boundary-values
'(100 100 10 10 1 1 #f))
;@
(define (format . args)
(define format:port #f) ; curr. format output port
(define format:output-col 0) ; curr. format output tty column
(define format:flush-output #f) ; flush output at end of formatting
(define format:case-conversion #f)
(define format:pos 0) ; curr. format string parsing position
;; format string and char output routines on format:port
(define (format:out-str str)
(if format:case-conversion
(display (format:case-conversion str) format:port)
(display str format:port))
(set! format:output-col
(+ format:output-col (string-length str))))
(define (format:out-char ch)
(if format:case-conversion
(display (format:case-conversion (string ch)) format:port)
(write-char ch format:port))
(set! format:output-col
(if (char=? ch #\newline)
0
(+ format:output-col 1))))
;;(define (format:out-substr str i n) ; this allocates a new string
;; (display (substring str i n) format:port)
;; (set! format:output-col (+ format:output-col n)))
(define (format:out-substr str i n)
(do ((k i (+ k 1)))
((= k n))
(write-char (string-ref str k) format:port))
(set! format:output-col (+ format:output-col n)))
;;(define (format:out-fill n ch) ; this allocates a new string
;; (format:out-str (make-string n ch)))
(define (format:out-fill n ch)
(do ((i 0 (+ i 1)))
((= i n))
(write-char ch format:port))
(set! format:output-col (+ format:output-col n)))
(define (format:out-obj-padded pad-left obj slashify pars format:read-proof)
(if (null? pars)
(format:out-str (format:obj->str obj slashify format:read-proof))
(let ((l (length pars)))
(let ((mincol (format:par pars l 0 0 "mincol"))
(colinc (format:par pars l 1 1 "colinc"))
(minpad (format:par pars l 2 0 "minpad"))
(padchar (integer->char
(format:par pars l 3 format:space-ch #f)))
(objstr (format:obj->str obj slashify format:read-proof)))
(if (not pad-left)
(format:out-str objstr))
(do ((objstr-len (string-length objstr))
(i minpad (+ i colinc)))
((>= (+ objstr-len i) mincol)
(format:out-fill i padchar)))
(if pad-left
(format:out-str objstr))))))
(define (format:out-num-padded modifier number pars radix)
(if (not (integer? number))
(slib:error 'format "argument not an integer" number))
(let ((numstr (number->string number radix)))
(if (and format:radix-pref (not (= radix 10)))
(set! numstr (substring numstr 2 (string-length numstr))))
(if (and (null? pars) (not modifier))
(format:out-str numstr)
(let ((l (length pars))
(numstr-len (string-length numstr)))
(let ((mincol (format:par pars l 0 #f "mincol"))
(padchar (integer->char
(format:par pars l 1 format:space-ch #f)))
(commachar (integer->char
(format:par pars l 2 (char->integer #\,) #f)))
(commawidth (format:par pars l 3 3 "commawidth")))
(if mincol
(let ((numlen numstr-len)) ; calc. the output len of number
(if (and (memq modifier '(at colon-at)) (> number 0))
(set! numlen (+ numlen 1)))
(if (memq modifier '(colon colon-at))
(set! numlen (+ (quotient (- numstr-len
(if (< number 0) 2 1))
commawidth)
numlen)))
(if (> mincol numlen)
(format:out-fill (- mincol numlen) padchar))))
(if (and (memq modifier '(at colon-at))
(> number 0))
(format:out-char #\+))
(if (memq modifier '(colon colon-at)) ; insert comma character
(let ((start (remainder numstr-len commawidth))
(ns (if (< number 0) 1 0)))
(format:out-substr numstr 0 start)
(do ((i start (+ i commawidth)))
((>= i numstr-len))
(if (> i ns)
(format:out-char commachar))
(format:out-substr numstr i (+ i commawidth))))
(format:out-str numstr)))))))
(define (format:tabulate modifier pars)
(let ((l (length pars)))
(let ((colnum (format:par pars l 0 1 "colnum"))
(colinc (format:par pars l 1 1 "colinc"))
(padch (integer->char (format:par pars l 2 format:space-ch #f))))
(case modifier
((colon colon-at)
(slib:error 'format "unsupported modifier for ~~t" modifier))
((at) ; relative tabulation
(format:out-fill
(if (= colinc 0)
colnum ; colnum = colrel
(do ((c 0 (+ c colinc))
(col (+ format:output-col colnum)))
((>= c col)
(- c format:output-col))))
padch))
(else ; absolute tabulation
(format:out-fill
(cond
((< format:output-col colnum)
(- colnum format:output-col))
((= colinc 0)
0)
(else
(do ((c colnum (+ c colinc)))
((>= c format:output-col)
(- c format:output-col)))))
padch))))))
(define format:num->old-roman
(lambda (n)
(if (and (integer? n) (>= n 1))
(let loop ((n n)
(romans format:roman-alist)
(s '()))
(if (null? romans) (list->string (reverse s))
(let ((roman-val (caar romans))
(roman-dgt (cadar romans)))
(do ((q (quotient n roman-val) (- q 1))
(s s (cons roman-dgt s)))
((= q 0)
(loop (remainder n roman-val)
(cdr romans) s))))))
(slib:error 'format "only positive integers can be romanized"))))
(define format:num->roman
(lambda (n)
(if (and (integer? n) (> n 0))
(let loop ((n n)
(romans format:roman-alist)
(boundaries format:roman-boundary-values)
(s '()))
(if (null? romans)
(list->string (reverse s))
(let ((roman-val (caar romans))
(roman-dgt (cadar romans))
(bdry (car boundaries)))
(let loop2 ((q (quotient n roman-val))
(r (remainder n roman-val))
(s s))
(if (= q 0)
(if (and bdry (>= r (- roman-val bdry)))
(loop (remainder r bdry) (cdr romans)
(cdr boundaries)
(cons roman-dgt
(append
(cdr (assv bdry romans))
s)))
(loop r (cdr romans) (cdr boundaries) s))
(loop2 (- q 1) r (cons roman-dgt s)))))))
(slib:error 'format "only positive integers can be romanized"))))
(define format:num->cardinal999
(lambda (n)
;;this procedure is inspired by the Bruno Haible's CLisp
;;function format-small-cardinal, which converts numbers
;;in the range 1 to 999, and is used for converting each
;;thousand-block in a larger number
(let* ((hundreds (quotient n 100))
(tens+ones (remainder n 100))
(tens (quotient tens+ones 10))
(ones (remainder tens+ones 10)))
(append
(if (> hundreds 0)
(append
(string->list
(list-ref format:cardinal-ones-list hundreds))
(string->list" hundred")
(if (> tens+ones 0) '(#\space) '()))
'())
(if (< tens+ones 20)
(if (> tens+ones 0)
(string->list
(list-ref format:cardinal-ones-list tens+ones))
'())
(append
(string->list
(list-ref format:cardinal-tens-list tens))
(if (> ones 0)
(cons #\-
(string->list
(list-ref format:cardinal-ones-list ones)))
'())))))))
(define format:num->cardinal
(lambda (n)
(cond ((not (integer? n))
(slib:error 'format
"only integers can be converted to English cardinals"))
((= n 0) "zero")
((< n 0) (string-append "minus " (format:num->cardinal (- n))))
(else
(let ((power3-word-limit
(length format:cardinal-thousand-block-list)))
(let loop ((n n)
(power3 0)
(s '()))
(if (= n 0)
(list->string s)
(let ((n-before-block (quotient n 1000))
(n-after-block (remainder n 1000)))
(loop n-before-block
(+ power3 1)
(if (> n-after-block 0)
(append
(if (> n-before-block 0)
(string->list ", ") '())
(format:num->cardinal999 n-after-block)
(if (< power3 power3-word-limit)
(string->list
(list-ref
format:cardinal-thousand-block-list
power3))
(append
(string->list " times ten to the ")
(string->list
(format:num->ordinal
(* power3 3)))
(string->list " power")))
s)
s))))))))))
(define format:num->ordinal
(lambda (n)
(cond ((not (integer? n))
(slib:error 'format
"only integers can be converted to English ordinals"))
((= n 0) "zeroth")
((< n 0) (string-append "minus " (format:num->ordinal (- n))))
(else
(let ((hundreds (quotient n 100))
(tens+ones (remainder n 100)))
(string-append
(if (> hundreds 0)
(string-append
(format:num->cardinal (* hundreds 100))
(if (= tens+ones 0) "th" " "))
"")
(if (= tens+ones 0) ""
(if (< tens+ones 20)
(list-ref format:ordinal-ones-list tens+ones)
(let ((tens (quotient tens+ones 10))
(ones (remainder tens+ones 10)))
(if (= ones 0)
(list-ref format:ordinal-tens-list tens)
(string-append
(list-ref format:cardinal-tens-list tens)
"-"
(list-ref format:ordinal-ones-list ones))))
))))))))
;; format fixed flonums (~F)
(define (format:out-fixed modifier number pars)
(if (not (or (number? number) (string? number)))
(slib:error 'format "argument is not a number or a number string"
number))
(let ((l (length pars)))
(let ((width (format:par pars l 0 #f "width"))
(digits (format:par pars l 1 #f "digits"))
(scale (format:par pars l 2 0 #f))
(overch (format:par pars l 3 #f #f))
(padch (format:par pars l 4 format:space-ch #f)))
(if digits
(begin ; fixed precision
(format:parse-float
(if (string? number) number (number->string number)) #t scale)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
(if width
(let ((numlen (+ format:fn-len 1)))
(if (or (not format:fn-pos?) (eq? modifier 'at))
(set! numlen (+ numlen 1)))
(if (and (= format:fn-dot 0) (> width (+ digits 1)))
(set! numlen (+ numlen 1)))
(if (< numlen width)
(format:out-fill (- width numlen) (integer->char padch)))
(if (and overch (> numlen width))
(format:out-fill width (integer->char overch))
(format:fn-out modifier (> width (+ digits 1)))))
(format:fn-out modifier #t)))
(begin ; free precision
(format:parse-float
(if (string? number) number (number->string number)) #t scale)
(format:fn-strip)
(if width
(let ((numlen (+ format:fn-len 1)))
(if (or (not format:fn-pos?) (eq? modifier 'at))
(set! numlen (+ numlen 1)))
(if (= format:fn-dot 0)
(set! numlen (+ numlen 1)))
(if (< numlen width)
(format:out-fill (- width numlen) (integer->char padch)))
(if (> numlen width) ; adjust precision if possible
(let ((dot-index (- numlen
(- format:fn-len format:fn-dot))))
(if (> dot-index width)
(if overch ; numstr too big for required width
(format:out-fill width (integer->char overch))
(format:fn-out modifier #t))
(begin
(format:fn-round (- width dot-index))
(format:fn-out modifier #t))))
(format:fn-out modifier #t)))
(format:fn-out modifier #t)))))))
;; format exponential flonums (~E)
(define (format:out-expon modifier number pars)
(if (not (or (number? number) (string? number)))
(slib:error 'format "argument is not a number" number))
(let ((l (length pars)))
(let ((width (format:par pars l 0 #f "width"))
(digits (format:par pars l 1 #f "digits"))
(edigits (format:par pars l 2 #f "exponent digits"))
(scale (format:par pars l 3 1 #f))
(overch (format:par pars l 4 #f #f))
(padch (format:par pars l 5 format:space-ch #f))
(expch (format:par pars l 6 #f #f)))
(if digits ; fixed precision
(let ((digits (if (> scale 0)
(if (< scale (+ digits 2))
(+ (- digits scale) 1)
0)
digits)))
(format:parse-float
(if (string? number) number (number->string number)) #f scale)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
(if width
(if (and edigits overch (> format:en-len edigits))
(format:out-fill width (integer->char overch))
(let ((numlen (+ format:fn-len 3))) ; .E+
(if (or (not format:fn-pos?) (eq? modifier 'at))
(set! numlen (+ numlen 1)))
(if (and (= format:fn-dot 0) (> width (+ digits 1)))
(set! numlen (+ numlen 1)))
(set! numlen
(+ numlen
(if (and edigits (>= edigits format:en-len))
edigits
format:en-len)))
(if (< numlen width)
(format:out-fill (- width numlen)
(integer->char padch)))
(if (and overch (> numlen width))
(format:out-fill width (integer->char overch))
(begin
(format:fn-out modifier (> width (- numlen 1)))
(format:en-out edigits expch)))))
(begin
(format:fn-out modifier #t)
(format:en-out edigits expch))))
(begin ; free precision
(format:parse-float
(if (string? number) number (number->string number)) #f scale)
(format:fn-strip)
(if width
(if (and edigits overch (> format:en-len edigits))
(format:out-fill width (integer->char overch))
(let ((numlen (+ format:fn-len 3))) ; .E+
(if (or (not format:fn-pos?) (eq? modifier 'at))
(set! numlen (+ numlen 1)))
(if (= format:fn-dot 0)
(set! numlen (+ numlen 1)))
(set! numlen
(+ numlen
(if (and edigits (>= edigits format:en-len))
edigits
format:en-len)))
(if (< numlen width)
(format:out-fill (- width numlen)
(integer->char padch)))
(if (> numlen width) ; adjust precision if possible
(let ((f (- format:fn-len format:fn-dot))) ; fract len
(if (> (- numlen f) width)
(if overch ; numstr too big for required width
(format:out-fill width
(integer->char overch))
(begin
(format:fn-out modifier #t)
(format:en-out edigits expch)))
(begin
(format:fn-round (+ (- f numlen) width))
(format:fn-out modifier #t)
(format:en-out edigits expch))))
(begin
(format:fn-out modifier #t)
(format:en-out edigits expch)))))
(begin
(format:fn-out modifier #t)
(format:en-out edigits expch))))))))
;; format general flonums (~G)
(define (format:out-general modifier number pars)
(if (not (or (number? number) (string? number)))
(slib:error 'format "argument is not a number or a number string"
number))
(let ((l (length pars)))
(let ((width (if (> l 0) (list-ref pars 0) #f))
(digits (if (> l 1) (list-ref pars 1) #f))
(edigits (if (> l 2) (list-ref pars 2) #f))
(overch (if (> l 4) (list-ref pars 4) #f))
(padch (if (> l 5) (list-ref pars 5) #f)))
(format:parse-float
(if (string? number) number (number->string number)) #t 0)
(format:fn-strip)
(let* ((ee (if edigits (+ edigits 2) 4)) ; for the following algorithm
(ww (if width (- width ee) #f)) ; see Steele's CL book p.395
(n (if (= format:fn-dot 0) ; number less than (abs 1.0) ?
(- (format:fn-zlead))
format:fn-dot))
(d (if digits
digits
(max format:fn-len (min n 7)))) ; q = format:fn-len
(dd (- d n)))
(if (<= 0 dd d)
(begin
(format:out-fixed modifier number (list ww dd #f overch padch))
(format:out-fill ee #\space)) ;~@T not implemented yet
(format:out-expon modifier number pars))))))
;; format dollar flonums (~$)
(define (format:out-dollar modifier number pars)
(if (not (or (number? number) (string? number)))
(slib:error 'format "argument is not a number or a number string"
number))
(let ((l (length pars)))
(let ((digits (format:par pars l 0 2 "digits"))
(mindig (format:par pars l 1 1 "mindig"))
(width (format:par pars l 2 0 "width"))
(padch (format:par pars l 3 format:space-ch #f)))
(format:parse-float
(if (string? number) number (number->string number)) #t 0)
(if (<= (- format:fn-len format:fn-dot) digits)
(format:fn-zfill #f (- digits (- format:fn-len format:fn-dot)))
(format:fn-round digits))
(let ((numlen (+ format:fn-len 1)))
(if (or (not format:fn-pos?) (memq modifier '(at colon-at)))
(set! numlen (+ numlen 1)))
(if (and mindig (> mindig format:fn-dot))
(set! numlen (+ numlen (- mindig format:fn-dot))))
(if (and (= format:fn-dot 0) (not mindig))
(set! numlen (+ numlen 1)))
(if (< numlen width)
(case modifier
((colon)
(if (not format:fn-pos?)
(format:out-char #\-))
(format:out-fill (- width numlen) (integer->char padch)))
((at)
(format:out-fill (- width numlen) (integer->char padch))
(format:out-char (if format:fn-pos? #\+ #\-)))
((colon-at)
(format:out-char (if format:fn-pos? #\+ #\-))
(format:out-fill (- width numlen) (integer->char padch)))
(else
(format:out-fill (- width numlen) (integer->char padch))
(if (not format:fn-pos?)
(format:out-char #\-))))
(if format:fn-pos?
(if (memq modifier '(at colon-at)) (format:out-char #\+))
(format:out-char #\-))))
(if (and mindig (> mindig format:fn-dot))
(format:out-fill (- mindig format:fn-dot) #\0))
(if (and (= format:fn-dot 0) (not mindig))
(format:out-char #\0))
(format:out-substr format:fn-str 0 format:fn-dot)
(format:out-char #\.)
(format:out-substr format:fn-str format:fn-dot format:fn-len))))
; the flonum buffers
(define format:fn-str (make-string format:fn-max)) ; number buffer
(define format:fn-len 0) ; digit length of number
(define format:fn-dot #f) ; dot position of number
(define format:fn-pos? #t) ; number positive?
(define format:en-str (make-string format:en-max)) ; exponent buffer
(define format:en-len 0) ; digit length of exponent
(define format:en-pos? #t) ; exponent positive?
(define (format:parse-float num-str fixed? scale)
(set! format:fn-pos? #t)
(set! format:fn-len 0)
(set! format:fn-dot #f)
(set! format:en-pos? #t)
(set! format:en-len 0)
(do ((i 0 (+ i 1))
(left-zeros 0)
(mantissa? #t)
(all-zeros? #t)
(num-len (string-length num-str))
(c #f)) ; current exam. character in num-str
((= i num-len)
(if (not format:fn-dot)
(set! format:fn-dot format:fn-len))
(if all-zeros?
(begin
(set! left-zeros 0)
(set! format:fn-dot 0)
(set! format:fn-len 1)))
;; now format the parsed values according to format's need
(if fixed?
(begin ; fixed format m.nnn or .nnn
(if (and (> left-zeros 0) (> format:fn-dot 0))
(if (> format:fn-dot left-zeros)
(begin ; norm 0{0}nn.mm to nn.mm
(format:fn-shiftleft left-zeros)
(set! left-zeros 0)
(set! format:fn-dot (- format:fn-dot left-zeros)))
(begin ; normalize 0{0}.nnn to .nnn
(format:fn-shiftleft format:fn-dot)
(set! left-zeros (- left-zeros format:fn-dot))
(set! format:fn-dot 0))))
(if (or (not (= scale 0)) (> format:en-len 0))
(let ((shift (+ scale (format:en-int))))
(cond
(all-zeros? #t)
((> (+ format:fn-dot shift) format:fn-len)
(format:fn-zfill
#f (- shift (- format:fn-len format:fn-dot)))
(set! format:fn-dot format:fn-len))
((< (+ format:fn-dot shift) 0)
(format:fn-zfill #t (- (- shift) format:fn-dot))
(set! format:fn-dot 0))
(else
(if (> left-zeros 0)
(if (<= left-zeros shift) ; shift always > 0 here
(format:fn-shiftleft shift) ; shift out 0s
(begin
(format:fn-shiftleft left-zeros)
(set! format:fn-dot (- shift left-zeros))))
(set! format:fn-dot (+ format:fn-dot shift))))))))
(let ((negexp ; expon format m.nnnEee
(if (> left-zeros 0)
(- left-zeros format:fn-dot -1)
(if (= format:fn-dot 0) 1 0))))
(if (> left-zeros 0)
(begin ; normalize 0{0}.nnn to n.nn
(format:fn-shiftleft left-zeros)
(set! format:fn-dot 1))
(if (= format:fn-dot 0)
(set! format:fn-dot 1)))
(format:en-set (- (+ (- format:fn-dot scale) (format:en-int))
negexp))
(cond
(all-zeros?
(format:en-set 0)
(set! format:fn-dot 1))
((< scale 0) ; leading zero
(format:fn-zfill #t (- scale))
(set! format:fn-dot 0))
((> scale format:fn-dot)
(format:fn-zfill #f (- scale format:fn-dot))
(set! format:fn-dot scale))
(else
(set! format:fn-dot scale)))))
#t)
;; do body
(set! c (string-ref num-str i)) ; parse the output of number->string
(cond ; which can be any valid number
((char-numeric? c) ; representation of R4RS except
(if mantissa? ; complex numbers
(begin
(if (char=? c #\0)
(if all-zeros?
(set! left-zeros (+ left-zeros 1)))
(begin
(set! all-zeros? #f)))
(string-set! format:fn-str format:fn-len c)
(set! format:fn-len (+ format:fn-len 1)))
(begin
(string-set! format:en-str format:en-len c)
(set! format:en-len (+ format:en-len 1)))))
((or (char=? c #\-) (char=? c #\+))
(if mantissa?
(set! format:fn-pos? (char=? c #\+))
(set! format:en-pos? (char=? c #\+))))
((char=? c #\.)
(set! format:fn-dot format:fn-len))
((char=? c #\e)
(set! mantissa? #f))
((char=? c #\E)
(set! mantissa? #f))
((char-whitespace? c) #t)
((char=? c #\d) #t) ; decimal radix prefix
((char=? c #\#) #t)
(else
(slib:error 'format "illegal character in number->string" c)))))
(define (format:en-int) ; convert exponent string to integer
(if (= format:en-len 0)
0
(do ((i 0 (+ i 1))
(n 0))
((= i format:en-len)
(if format:en-pos?
n
(- n)))
(set! n (+ (* n 10) (- (char->integer (string-ref format:en-str i))
format:zero-ch))))))
(define (format:en-set en) ; set exponent string number
(set! format:en-len 0)
(set! format:en-pos? (>= en 0))
(let ((en-str (number->string en)))
(do ((i 0 (+ i 1))
(en-len (string-length en-str))
(c #f))
((= i en-len))
(set! c (string-ref en-str i))
(if (char-numeric? c)
(begin
(string-set! format:en-str format:en-len c)
(set! format:en-len (+ format:en-len 1)))))))
(define (format:fn-zfill left? n) ; fill current number string with 0s
(if (> (+ n format:fn-len) format:fn-max) ; from the left or right
(slib:error 'format "number is too long to format (enlarge format:fn-max)"))
(set! format:fn-len (+ format:fn-len n))
(if left?
(do ((i format:fn-len (- i 1))) ; fill n 0s to left
((< i 0))
(string-set! format:fn-str i
(if (< i n)
#\0
(string-ref format:fn-str (- i n)))))
(do ((i (- format:fn-len n) (+ i 1))) ; fill n 0s to the right
((= i format:fn-len))
(string-set! format:fn-str i #\0))))
(define (format:fn-shiftleft n) ; shift left current number n positions
(if (> n format:fn-len)
(slib:error 'format "internal error in format:fn-shiftleft"
(list n format:fn-len)))
(do ((i n (+ i 1)))
((= i format:fn-len)
(set! format:fn-len (- format:fn-len n)))
(string-set! format:fn-str (- i n) (string-ref format:fn-str i))))
(define (format:fn-round digits) ; round format:fn-str
(set! digits (+ digits format:fn-dot))
(do ((i digits (- i 1)) ; "099",2 -> "10"
(c 5)) ; "023",2 -> "02"
((or (= c 0) (< i 0)) ; "999",2 -> "100"
(if (= c 1) ; "005",2 -> "01"
(begin ; carry overflow
(set! format:fn-len digits)
(format:fn-zfill #t 1) ; add a 1 before fn-str
(string-set! format:fn-str 0 #\1)
(set! format:fn-dot (+ format:fn-dot 1)))
(set! format:fn-len digits)))
(set! c (+ (- (char->integer (string-ref format:fn-str i))
format:zero-ch) c))
(string-set! format:fn-str i (integer->char
(if (< c 10)
(+ c format:zero-ch)
(+ (- c 10) format:zero-ch))))
(set! c (if (< c 10) 0 1))))
(define (format:fn-out modifier add-leading-zero?)
(if format:fn-pos?
(if (eq? modifier 'at)
(format:out-char #\+))
(format:out-char #\-))
(if (= format:fn-dot 0)
(if add-leading-zero?
(format:out-char #\0))
(format:out-substr format:fn-str 0 format:fn-dot))
(format:out-char #\.)
(format:out-substr format:fn-str format:fn-dot format:fn-len))
(define (format:en-out edigits expch)
(format:out-char (if expch (integer->char expch) format:expch))
(format:out-char (if format:en-pos? #\+ #\-))
(if edigits
(if (< format:en-len edigits)
(format:out-fill (- edigits format:en-len) #\0)))
(format:out-substr format:en-str 0 format:en-len))
(define (format:fn-strip) ; strip trailing zeros but one
(string-set! format:fn-str format:fn-len #\0)
(do ((i format:fn-len (- i 1)))
((or (not (char=? (string-ref format:fn-str i) #\0))
(<= i format:fn-dot))
(set! format:fn-len (+ i 1)))))
(define (format:fn-zlead) ; count leading zeros
(do ((i 0 (+ i 1)))
((or (= i format:fn-len)
(not (char=? (string-ref format:fn-str i) #\0)))
(if (= i format:fn-len) ; found a real zero
0
i))))
(define (format:format-work format-string arglist) ; does the formatting work
(letrec
((format-string-len (string-length format-string))
(arg-pos 0) ; argument position in arglist
(arg-len (length arglist)) ; number of arguments
(modifier #f) ; 'colon | 'at | 'colon-at | #f
(params '()) ; directive parameter list
(param-value-found #f) ; a directive parameter value found
(conditional-nest 0) ; conditional nesting level
(clause-pos 0) ; last cond. clause beginning char pos
(clause-default #f) ; conditional default clause string
(clauses '()) ; conditional clause string list
(conditional-type #f) ; reflects the contional modifiers
(conditional-arg #f) ; argument to apply the conditional
(iteration-nest 0) ; iteration nesting level
(iteration-pos 0) ; iteration string beginning char pos
(iteration-type #f) ; reflects the iteration modifiers
(max-iterations #f) ; maximum number of iterations
(recursive-pos-save format:pos)
(next-char ; gets the next char from format-string
(lambda ()
(let ((ch (peek-next-char)))
(set! format:pos (+ 1 format:pos))
ch)))
(peek-next-char
(lambda ()
(if (>= format:pos format-string-len)
(slib:error 'format "illegal format string")
(string-ref format-string format:pos))))
(one-positive-integer?
(lambda (params)
(cond
((null? params) #f)
((and (integer? (car params))
(>= (car params) 0)
(= (length params) 1)) #t)
(else (slib:error 'format "one positive integer parameter expected")))))
(next-arg
(lambda ()
(if (>= arg-pos arg-len)
(begin
(slib:error 'format "missing argument(s)")))
(add-arg-pos 1)
(list-ref arglist (- arg-pos 1))))
(prev-arg
(lambda ()
(add-arg-pos -1)
(if (negative? arg-pos)
(slib:error 'format "missing backward argument(s)"))
(list-ref arglist arg-pos)))
(rest-args
(lambda ()
(let loop ((l arglist) (k arg-pos)) ; list-tail definition
(if (= k 0) l (loop (cdr l) (- k 1))))))
(add-arg-pos
(lambda (n)
(set! arg-pos (+ n arg-pos))))
(anychar-dispatch ; dispatches the format-string
(lambda ()
(if (>= format:pos format-string-len)
arg-pos ; used for ~? continuance
(let ((char (next-char)))
(cond
((char=? char #\~)
(set! modifier #f)
(set! params '())
(set! param-value-found #f)
(tilde-dispatch))
(else
(if (and (zero? conditional-nest)
(zero? iteration-nest))
(format:out-char char))
(anychar-dispatch)))))))
(tilde-dispatch
(lambda ()
(cond
((>= format:pos format-string-len)
(format:out-str "~") ; tilde at end of string is just output
arg-pos) ; used for ~? continuance
((and (or (zero? conditional-nest)
(memv (peek-next-char) ; find conditional directives
(append '(#\[ #\] #\; #\: #\@ #\^)
format:parameter-characters)))
(or (zero? iteration-nest)
(memv (peek-next-char) ; find iteration directives
(append '(#\{ #\} #\: #\@ #\^)
format:parameter-characters))))
(case (char-upcase (next-char))
;; format directives
((#\A) ; Any -- for humans
(format:out-obj-padded (memq modifier '(at colon-at))
(next-arg) #f params
(memq modifier '(colon colon-at)))
(anychar-dispatch))
((#\S) ; Slashified -- for parsers
(format:out-obj-padded (memq modifier '(at colon-at))
(next-arg) #t params
(memq modifier '(colon colon-at)))
(anychar-dispatch))
((#\D) ; Decimal
(format:out-num-padded modifier (next-arg) params 10)
(anychar-dispatch))
((#\X) ; Hexadecimal
(format:out-num-padded modifier (next-arg) params 16)
(anychar-dispatch))
((#\O) ; Octal
(format:out-num-padded modifier (next-arg) params 8)
(anychar-dispatch))
((#\B) ; Binary
(format:out-num-padded modifier (next-arg) params 2)
(anychar-dispatch))
((#\R)
(if (null? params)
(format:out-obj-padded ; Roman, cardinal, ordinal numerals
#f
((case modifier
((at) format:num->roman)
((colon-at) format:num->old-roman)
((colon) format:num->ordinal)
(else format:num->cardinal))
(next-arg))
#f params #f) ;was format:read-proof
(format:out-num-padded ; any Radix
modifier (next-arg) (cdr params) (car params)))
(anychar-dispatch))
((#\F) ; Fixed-format floating-point
(if format:floats
(format:out-fixed modifier (next-arg) params)
(format:out-str (number->string (next-arg))))
(anychar-dispatch))
((#\E) ; Exponential floating-point
(if format:floats
(format:out-expon modifier (next-arg) params)
(format:out-str (number->string (next-arg))))
(anychar-dispatch))
((#\G) ; General floating-point
(if format:floats
(format:out-general modifier (next-arg) params)
(format:out-str (number->string (next-arg))))