This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
cformat.jl
360 lines (336 loc) · 10.4 KB
/
cformat.jl
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
formatters = Dict{ String, Function }()
sprintf1( fmt::String, x ) = eval(Expr(:call, generate_formatter( fmt ), x))
function checkfmt(fmt)
@static if VERSION > v"1.6.0-DEV.854"
test = Printf.Format(fmt)
length(test.formats) == 1 ||
error( "Only one AND undecorated format string is allowed")
else
test = @static VERSION >= v"1.4.0-DEV.180" ? Printf.parse(fmt) : Base.Printf.parse( fmt )
(length( test ) == 1 && typeof( test[1] ) <: Tuple) ||
error( "Only one AND undecorated format string is allowed")
end
end
function generate_formatter( fmt::String )
global formatters
haskey( formatters, fmt ) && return formatters[fmt]
if !occursin("'", fmt)
checkfmt(fmt)
formatter = @eval(x->@sprintf( $fmt, x ))
return (formatters[ fmt ] = x->Base.invokelatest(formatter, x))
end
conversion = fmt[end]
conversion in "sduifF" ||
error( string("thousand separator not defined for ", conversion, " conversion") )
fmtactual = replace( fmt, "'" => "", count=1 )
checkfmt( fmtactual )
if !occursin(conversion, "sfF")
formatter = @eval(x->checkcommas(@sprintf( $fmtactual, x )))
return (formatters[ fmt ] = x->Base.invokelatest(formatter, x))
end
formatter =
if endswith( fmtactual, 's')
@eval((x::Real)->((eltype(x) <: Rational)
? addcommasrat(@sprintf( $fmtactual, x ))
: addcommasreal(@sprintf( $fmtactual, x ))))
else
@eval((x::Real)->addcommasreal(@sprintf( $fmtactual, x )))
end
return (formatters[ fmt ] = x->Base.invokelatest(formatter, x))
end
function addcommasreal(s)
dpos = findfirst( isequal('.'), s )
dpos !== nothing && return string(addcommas( s[1:dpos-1] ), s[ dpos:end ])
# find the rightmost digit
for i in length( s ):-1:1
isdigit( s[i] ) && return string(addcommas( s[1:i] ), s[i+1:end])
end
s
end
function addcommasrat(s)
# commas are added to only the numerator
spos = findfirst( isequal('/'), s )
string(addcommas( s[1:spos-1] ), s[spos:end])
end
function checkcommas(s)
for i in length( s ):-1:1
if isdigit( s[i] )
s = string(addcommas( s[1:i] ), s[i+1:end])
break
end
end
s
end
function addcommas( s::String )
len = length(s)
t = ""
for i in 1:3:len
subs = s[max(1,len-i-1):len-i+1]
if i == 1
t = subs
else
if match( r"[0-9]", subs ) != nothing
t = subs * "," * t
else
t = subs * t
end
end
end
return t
end
function generate_format_string(;
width::Int=-1,
precision::Int= -1,
leftjustified::Bool=false,
zeropadding::Bool=false,
commas::Bool=false,
signed::Bool=false,
positivespace::Bool=false,
alternative::Bool=false,
conversion::String="f" #aAdecEfFiosxX
)
s = "%"
if commas
s *= "'"
end
if alternative && in( conversion[1], "aAeEfFoxX" )
s *= "#"
end
if zeropadding && !leftjustified && width != -1
s *= "0"
end
if signed
s *= "+"
elseif positivespace
s *= " "
end
if width != -1
if leftjustified
s *= "-" * string( width )
else
s *= string( width )
end
end
if precision != -1
s *= "." * string( precision )
end
s * conversion
end
function format( x::T;
width::Int=-1,
precision::Int= -1,
leftjustified::Bool=false,
zeropadding::Bool=false, # when right-justified, use 0 instead of space to fill
commas::Bool=false,
signed::Bool=false, # +/- prefix
positivespace::Bool=false,
stripzeros::Bool=(precision== -1),
parens::Bool=false, # use (1.00) instead of -1.00. Used in finance
alternative::Bool=false, # usually for hex
mixedfraction::Bool=false,
mixedfractionsep::AbstractString="_",
fractionsep::AbstractString="/", # num / den
fractionwidth::Int = 0,
tryden::Int = 0, # if 2 or higher, try to use this denominator, without losing precision
suffix::AbstractString="", # useful for units/%
autoscale::Symbol=:none, # :metric, :binary or :finance
conversion::String=""
) where {T<:Real}
checkwidth = commas
if conversion == ""
if T <: AbstractFloat || T <: Rational && precision != -1
actualconv = "f"
elseif T <: Unsigned
actualconv = "x"
elseif T <: Integer
actualconv = "d"
else
conversion = "s"
actualconv = "s"
end
else
actualconv = conversion
end
if signed && commas
error( "You cannot use signed (+/-) AND commas at the same time")
end
if T <: Rational && conversion == "s"
stripzeros = false
end
if ( T <: AbstractFloat && actualconv == "f" || T <: Integer ) && autoscale != :none
actualconv = "f"
if autoscale == :metric
scales = [
(1e24, "Y" ),
(1e21, "Z" ),
(1e18, "E" ),
(1e15, "P" ),
(1e12, "T" ),
(1e9, "G"),
(1e6, "M"),
(1e3, "k") ]
if abs(x) > 1
for (mag, sym) in scales
if abs(x) >= mag
x /= mag
suffix = sym * suffix
break
end
end
elseif T <: AbstractFloat
smallscales = [
( 1e-12, "p" ),
( 1e-9, "n" ),
( 1e-6, "μ" ),
( 1e-3, "m" ) ]
for (mag,sym) in smallscales
if abs(x) < mag*10
x /= mag
suffix = sym * suffix
break
end
end
end
else
if autoscale == :binary
scales = [
(1024.0 ^8, "Yi" ),
(1024.0 ^7, "Zi" ),
(1024.0 ^6, "Ei" ),
(1024.0 ^5, "Pi" ),
(1024.0 ^4, "Ti" ),
(1024.0 ^3, "Gi"),
(1024.0 ^2, "Mi"),
(1024.0, "Ki")
]
else # :finance
scales = [
(1e12, "t" ),
(1e9, "b"),
(1e6, "m"),
(1e3, "k") ]
end
for (mag, sym) in scales
if abs(x) >= mag
x /= mag
suffix = sym * suffix
break
end
end
end
end
nonneg = x >= 0
fractional = 0
if T <: Rational && mixedfraction
actualconv = "d"
actualx = trunc( Int, x )
fractional = abs(x) - abs(actualx)
else
if parens && !in( actualconv[1], "xX" )
actualx = abs(x)
else
actualx = x
end
end
s = sprintf1( generate_format_string( width=width,
precision=precision,
leftjustified=leftjustified,
zeropadding=zeropadding,
commas=commas,
signed=signed,
positivespace=positivespace,
alternative=alternative,
conversion=actualconv
),actualx)
if T <:Rational && conversion == "s"
if mixedfraction && fractional != 0
num = fractional.num
den = fractional.den
if tryden >= 2 && mod( tryden, den ) == 0
num *= div(tryden,den)
den = tryden
end
fs = string( num ) * fractionsep * string(den)
if length(fs) < fractionwidth
fs = repeat( "0", fractionwidth - length(fs) ) * fs
end
s = rstrip(s)
if actualx != 0
s = rstrip(s) * mixedfractionsep * fs
else
if !nonneg
s = "-" * fs
else
s = fs
end
end
checkwidth = true
elseif !mixedfraction
s = replace( s, "//" => fractionsep )
checkwidth = true
end
elseif stripzeros && in( actualconv[1], "fFeEs" )
dpos = findfirst( isequal('.'), s )
if in( actualconv[1], "eEs" )
if in( actualconv[1], "es" )
epos = findfirst( isequal('e'), s )
else
epos = findfirst( isequal('E'), s )
end
if epos === nothing
rpos = length( s )
else
rpos = epos-1
end
else
rpos = length(s)
end
# rpos at this point is the rightmost possible char to start
# stripping
stripfrom = rpos+1
for i = rpos:-1:dpos+1
if s[i] == '0'
stripfrom = i
elseif s[i] ==' '
continue
else
break
end
end
if stripfrom <= rpos
if stripfrom == dpos+1 # everything after decimal is 0, so strip the decimal too
stripfrom = dpos
end
s = s[1:stripfrom-1] * s[rpos+1:end]
checkwidth = true
end
end
s *= suffix
if parens && !in( actualconv[1], "xX" )
# if zero or positive, we still need 1 white space on the right
if nonneg
s = " " * strip(s) * " "
else
s = "(" * strip(s) * ")"
end
checkwidth = true
end
if checkwidth && width != -1
if length(s) > width
s = replace( s, " " => "", count=length(s)-width )
if length(s) > width && endswith( s, " " )
s = reverse( replace( reverse(s), " " => "", count=length(s)-width ) )
end
if length(s) > width
s = replace( s, "," => "", count=length(s)-width )
end
elseif length(s) < width
if leftjustified
s = s * repeat( " ", width - length(s) )
else
s = repeat( " ", width - length(s) ) * s
end
end
end
s
end