forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.rkt
More file actions
195 lines (176 loc) · 8.84 KB
/
format.rkt
File metadata and controls
195 lines (176 loc) · 8.84 KB
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
#lang racket/base
;; Functions to format numbers, and data structures containing numbers.
(require racket/string racket/list racket/pretty racket/contract racket/match
unstable/latent-contract/defthing
"contract.rkt"
"math.rkt")
(provide (all-defined-out))
(define (string-map f str)
(list->string (map f (string->list str))))
(defproc (integer->superscript [x exact-integer?]) string?
(string-map (λ (c) (case c
[(#\0) #\u2070]
[(#\1) #\u00b9]
[(#\2) #\u00b2]
[(#\3) #\u00b3]
[(#\4) #\u2074]
[(#\5) #\u2075]
[(#\6) #\u2076]
[(#\7) #\u2077]
[(#\8) #\u2078]
[(#\9) #\u2079]
[(#\+) #\u207a]
[(#\-) #\u207b]
[else c]))
(number->string x)))
(defproc (real->decimal-string* [x real?]
[min-digits exact-nonnegative-integer?]
[max-digits exact-nonnegative-integer? min-digits]) string?
(when (min-digits . > . max-digits)
(error 'real->decimal-string* "expected min-digits <= max-digits; given ~e and ~e"
min-digits max-digits))
(define str (real->decimal-string x max-digits))
(let loop ([i (string-length str)] [j (- max-digits min-digits)])
(cond [(zero? j) (substring str 0 i)]
[(zero? i) "0"] ; shouldn't happen, as real->decimal-string guarantees a "0." prefix
[(char=? #\0 (string-ref str (- i 1))) (loop (- i 1) (- j 1))]
[else (substring str 0 i)])))
(define (remove-trailing-zeros str)
(let loop ([i (string-length str)])
(cond [(zero? i) "0"]
[(char=? #\0 (string-ref str (sub1 i))) (loop (sub1 i))]
[(char=? #\. (string-ref str (sub1 i))) (substring str 0 (sub1 i))]
[else (substring str 0 i)])))
;; Returns the number of fractional digits needed to distinguish numbers [x-min..x-max]
(defproc (digits-for-range [x-min real?] [x-max real?]
[base (and/c exact-integer? (>=/c 2)) 10]
[extra-digits exact-integer? 3]) exact-integer?
(define range (abs (- x-max x-min)))
(+ extra-digits (if (zero? range) 0 (- (floor-log/base base range)))))
(define (int-str->e-str str)
(define n (string-length str))
(cond [(or (= 0 n) (string=? str "0")) "0"]
[else
(define fst (substring str 0 1))
(define rst (substring str 1 n))
(format "~a×10~a"
(remove-trailing-zeros (format "~a.~a" fst rst))
(integer->superscript (sub1 n)))]))
(define (frac-str->e-str str)
(define n (string-length str))
(let loop ([i 0])
(cond [(= i n) "0"]
[(char=? #\0 (string-ref str i)) (loop (add1 i))]
[else (define fst (substring str i (add1 i)))
(define rst (substring str (add1 i) n))
(cond [(= 0 (string-length rst))
(format "~a×10~a" fst (integer->superscript (- (add1 i))))]
[else
(format "~a.~a×10~a" fst rst (integer->superscript (- (add1 i))))])])))
(define (zero-string n)
(list->string (build-list n (λ _ #\0))))
(defproc (real->plot-label [x real?] [digits exact-integer?] [scientific? boolean? #t]) any
(cond
[(zero? x) "0"]
[(eqv? x +nan.0) "+nan.0"]
[(eqv? x +inf.0) "+inf.0"]
[(eqv? x -inf.0) "-inf.0"]
[else
(define front-sign (if (x . < . 0) "-" ""))
(define mid-sign (if (x . < . 0) "-" "+"))
(let* ([x (abs (inexact->exact x))])
;; Round away any extra digits
(define round-fac (expt 10 digits))
(define y (/ (round (* x round-fac)) round-fac))
;; Parse the output of real->decimal-string
(define-values (int-str frac-str)
(match-let ([(list _ int-str frac-str)
(regexp-match #rx"(.*)\\.(.*)" (real->decimal-string y (max 0 digits)))])
(values int-str (remove-trailing-zeros frac-str))))
(define int-zero? (string=? int-str "0"))
(define frac-zero? (string=? frac-str "0"))
(cond
[scientific?
;; Get scientific notation for the integer and fractional parts
(define int-e-str (int-str->e-str int-str))
(define frac-e-str (frac-str->e-str frac-str))
;(printf "int-str = ~v, frac-str = ~v~n" int-str frac-str)
;(printf "int-e-str = ~v, frac-e-str = ~v~n" int-e-str frac-e-str)
(define int-e-zero? (string=? int-e-str "0"))
(define frac-e-zero? (string=? frac-e-str "0"))
;; Build a list of possible output strings
(define strs
(list (cond [(and int-zero? frac-zero?) "0"]
[int-zero? (format "~a.~a" front-sign frac-str)]
[frac-zero? (format "~a~a" front-sign int-str)]
[else (format "~a~a.~a" front-sign int-str frac-str)])
(cond [(and int-e-zero? frac-zero?) "0"]
[int-e-zero? (format "~a.~a" front-sign frac-str)]
[frac-zero? (format "~a~a" front-sign int-e-str)]
[else (format "~a(~a)~a.~a" front-sign int-e-str mid-sign frac-str)])
(cond [(and int-zero? frac-e-zero?) "0"]
[int-zero? (format "~a~a" front-sign frac-e-str)]
[frac-e-zero? (format "~a~a" front-sign int-str)]
[else (format "~a~a~a(~a)" front-sign int-str mid-sign frac-e-str)])
(cond [(and int-e-zero? frac-e-zero?) "0"]
[int-e-zero? (format "~a~a" front-sign frac-e-str)]
[frac-e-zero? (format "~a~a" front-sign int-e-str)]
[else
(format "~a(~a)~a(~a)" front-sign int-e-str mid-sign frac-e-str)])))
;; Return the shortest possible output string
(argmin string-length strs)]
[else
(cond [(and int-zero? frac-zero?) "0"]
[int-zero? (format "~a.~a" front-sign frac-str)]
[frac-zero? (format "~a~a" front-sign int-str)]
[else (format "~a~a.~a" front-sign int-str frac-str)])]))]))
(define (format-special x)
(case x
[(#f) "#f"]
[(+nan.0) "+nan.0"]
[(+inf.0) "+inf.0"]
[(-inf.0) "-inf.0"]
[else "<unknown>"]))
(defproc (ivl->plot-label [i ivl?] [extra-digits exact-integer? 3]) string?
(match-define (ivl a b) i)
(cond [(and (not (rational? a)) (not (rational? b)))
(format "[~a,~a]" (format-special a) (format-special b))]
[(not (rational? a)) (format "[~a,~a]" (format-special a) (real->plot-label b 15))]
[(not (rational? b)) (format "[~a,~a]" (real->plot-label a 15) (format-special b))]
[else
(define digits (digits-for-range a b 10 extra-digits))
(format "[~a,~a]"
(real->plot-label a digits)
(real->plot-label b digits))]))
(defproc (->plot-label [a any/c] [digits exact-integer? 7]) string?
(let loop ([a a])
(cond [(string? a) a]
[(symbol? a) (symbol->string a)]
[(real? a) (real->plot-label a digits)]
[(ivl? a) (ivl->plot-label a)]
[(list? a) (string-append "(" (string-join (map loop a)) ")")]
[(cons? a) (string-append "(" (loop (car a)) " . " (loop (cdr a)) ")")]
[(boolean? a) (if a "true" "false")]
[(char? a) (list->string (list a))]
[else (pretty-format a)])))
;; Like real->decimal-string, but removes trailing zeros
(defproc (real->string/trunc [x real?] [e exact-integer?]) string?
(remove-trailing-zeros (real->decimal-string x (max 0 e))))
;; ===================================================================================================
;; Format strings
(defproc (parse-format-string [str string?]) (listof (or/c string? symbol?))
(define n (string-length str))
(let loop ([i 0] [fmt-list empty])
(cond [(i . >= . n) (reverse fmt-list)]
[(i . = . (- n 1)) (reverse (cons (substring str i (+ i 1)) fmt-list))]
[(char=? #\~ (string-ref str i))
(loop (+ i 2) (cons (string->symbol (substring str i (+ i 2))) fmt-list))]
[else (loop (+ i 1) (cons (substring str i (+ i 1)) fmt-list))])))
(defproc (apply-formatter [formatter (symbol? any/c . -> . (or/c string? #f))]
[fmt-list (listof (or/c string? symbol?))]
[d any/c]) (listof string?)
(for/list ([fmt (in-list fmt-list)])
(cond [(eq? fmt '~~) "~"]
[(symbol? fmt) (let ([val (formatter fmt d)])
(if val val (symbol->string fmt)))]
[(string? fmt) fmt])))