-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfmtcore.jl
More file actions
354 lines (310 loc) · 8.77 KB
/
fmtcore.jl
File metadata and controls
354 lines (310 loc) · 8.77 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
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
# core formatting functions
### auxiliary functions
### print char n times
function _repprint(out::IO, c::AbstractChar, n::Int)
wid = textwidth(c)
n < wid && return
while n > 0
print(out, c)
n -= wid
end
end
### print string or char
function _truncstr(s::AbstractString, slen, prec)
prec == 0 && return ("", 0)
i, n = 0, 1
siz = ncodeunits(s)
while n <= siz
(prec -= textwidth(s[n])) < 0 && break
i = n
n = nextind(s, i)
end
str = SubString(s, 1, i)
return (str, textwidth(str))
end
_truncstr(s::AbstractChar, slen, prec) = ("", 0)
function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,AbstractChar})
slen = textwidth(s)
str, slen = 0 <= fs.prec < slen ? _truncstr(s, slen, fs.prec) : (s, slen)
prepad = postpad = 0
pad = fs.width - slen
if pad > 0
if fs.align == '<'
postpad = pad
elseif fs.align == '^'
prepad, postpad = pad>>1, (pad+1)>>1
else
prepad = pad
end
end
# left padding
prepad == 0 || _repprint(out, fs.fill, prepad)
# print string
print(out, str)
# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end
_unsigned_abs(x::Signed) = unsigned(abs(x))
_unsigned_abs(x::Bool) = UInt(x)
_unsigned_abs(x::Unsigned) = x
# Special case because unsigned fails for BigInt
_unsigned_abs(x::BigInt) = abs(x)
_ndigits(x, ::_Dec) = ndigits(x)
_ndigits(x, ::_Bin) = ndigits(x, base=2)
_ndigits(x, ::_Oct) = ndigits(x, base=8)
_ndigits(x, ::Union{_Hex, _HEX}) = ndigits(x, base=16)
_sepcnt(::_Dec) = 3
_sepcnt(::Any) = 4
_mul(x::Integer, ::_Dec) = x * 10
_mul(x::Integer, ::_Bin) = x << 1
_mul(x::Integer, ::_Oct) = x << 3
_mul(x::Integer, ::Union{_Hex, _HEX}) = x << 4
_div(x::Integer, ::_Dec) = div(x, 10)
_div(x::Integer, ::_Bin) = x >> 1
_div(x::Integer, ::_Oct) = x >> 3
_div(x::Integer, ::Union{_Hex, _HEX}) = x >> 4
_ipre(op) = ""
_ipre(::_Oct) = "0o"
_ipre(::_Bin) = "0b"
_ipre(::_Hex) = "0x"
_ipre(::_HEX) = "0X"
_digitchar(x::Integer, ::_Bin) = x == 0 ? '0' : '1'
_digitchar(x::Integer, ::_Dec) = Char(Int('0') + x)
_digitchar(x::Integer, ::_Oct) = Char(Int('0') + x)
_digitchar(x::Integer, ::_Hex) = Char(x < 10 ? '0' + x : 'a' + (x - 10))
_digitchar(x::Integer, ::_HEX) = Char(x < 10 ? '0' + x : 'A' + (x - 10))
_signchar(x::Real, s::AbstractChar) = signbit(x) ? '-' : s == '+' ? '+' : s == ' ' ? ' ' : '\0'
### output integers (with or without separators)
function _outint(out::IO, ax::T, op::Op=_Dec()) where {Op, T<:Integer}
b_lb = _div(ax, op)
b = one(T)
while b <= b_lb
b = _mul(b, op)
end
r = ax
while b > 0
(q, r) = divrem(r, b)
print(out, _digitchar(q, op))
b = _div(b, op)
end
end
function _outint(out::IO, ax::T, op::Op, numini, sep) where {Op, T<:Integer}
b_lb = _div(ax, op)
b = one(T)
while b <= b_lb
b = _mul(b, op)
end
r = ax
while true
(q, r) = divrem(r, b)
print(out, _digitchar(q, op))
b = _div(b, op)
b == 0 && break
if numini == 0
numini = _sepcnt(op)
print(out, sep)
end
numini -= 1
end
end
# Print integer
function _pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op}
# calculate actual length
ax = _unsigned_abs(x)
xlen = _ndigits(ax, op)
numsep, numini = fs.tsep ? divrem(xlen-1, _sepcnt(op)) : (0, 0)
xlen += numsep
# sign char
sch = _signchar(x, fs.sign)
xlen += (sch != '\0')
# prefix (e.g. 0x, 0b, 0o)
ip = ""
if fs.ipre
ip = _ipre(op)
xlen += length(ip)
end
# printing
pad = fs.width - xlen
prepad = postpad = zpad = 0
if pad > 0
if fs.zpad
zpad = pad
elseif fs.align == '<'
postpad = pad
elseif fs.align == '^'
prepad, postpad = pad>>1, (pad+1)>>1
else
prepad = pad
end
end
# left padding
prepad == 0 || _repprint(out, fs.fill, prepad)
# print sign
sch != '\0' && print(out, sch)
# print prefix
!isempty(ip) && print(out, ip)
# print padding zeros
zpad > 0 && _repprint(out, '0', zpad)
# print actual digits
ax == 0 ? print(out, '0') :
numsep == 0 ? _outint(out, ax, op) : _outint(out, ax, op, numini, fs.sep)
# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end
function _truncval(v)
try
return trunc(Integer, v)
catch e
e isa InexactError || rethrow(e)
end
try
return trunc(Int128, v)
catch e
e isa InexactError || rethrow(e)
end
trunc(BigInt, v)
end
### print floating point numbers
function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
# Handle %
percentflag = (fs.typ == '%')
percentflag && (x *= 100)
# separate sign, integer, and decimal part
prec = fs.prec
rax = round(abs(x); digits = prec)
sch = _signchar(x, fs.sign)
intv = _truncval(rax)
decv = rax - intv
# calculate length
xlen = ndigits(intv)
numsep, numini = fs.tsep ? divrem(xlen - 1, 3) : (0, 0)
xlen += ifelse(prec > 0, prec + 1, 0) + (sch != '\0') + numsep + percentflag
# calculate padding needed
pad = fs.width - xlen
prepad = postpad = zpad = 0
if pad > 0
if fs.zpad
zpad = pad
elseif fs.align == '<'
postpad = pad
elseif fs.align == '^'
prepad, postpad = pad>>1, (pad+1)>>1
else
prepad = pad
end
end
# left padding
prepad == 0 || _repprint(out, fs.fill, prepad)
# print sign
sch != '\0' && print(out, sch)
# print padding zeros
zpad > 0 && _repprint(out, '0', zpad)
# print integer part
intv == 0 ? print(out, '0') :
numsep == 0 ? _outint(out, intv) : _outint(out, intv, _Dec(), numini, fs.sep)
# print decimal part
if prec > 0
print(out, '.')
idecv = round(Integer, decv * exp10(prec))
nd = ndigits(idecv)
nd < prec && _repprint(out, '0', prec - nd)
_outint(out, idecv)
end
# print trailing percent sign
percentflag && print(out, '%')
# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end
function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
# extract sign, significand, and exponent
prec = fs.prec
ax = abs(x)
sch = _signchar(x, fs.sign)
if ax == 0.0
e = 0
u = zero(x)
else
rax = round(ax; sigdigits = prec + 1)
e = floor(Integer, log10(rax)) # exponent
u = round(rax * exp10(-e); sigdigits = prec + 1) # significand
i = 0
v10 = 1
while isinf(u)
i += 1
i > 18 && (u = 0.0; e = 0; break)
v10 *= 10
u = round(v10 * rax * exp10(-e - i); sigdigits = prec + 1)
end
end
# calculate length
xlen = 6 + prec + (sch != '\0') + (abs(e) > 99 ? ndigits(abs(e)) - 2 : 0)
# calculate padding
pad = fs.width - xlen
prepad = postpad = zpad = 0
if pad > 0
if fs.zpad
zpad = pad
elseif fs.align == '<'
postpad = pad
elseif fs.align == '^'
prepad, postpad = pad>>1, (pad+1)>>1
else
prepad = pad
end
end
# left padding
prepad == 0 || _repprint(out, fs.fill, prepad)
# print sign
sch != '\0' && print(out, sch)
# print padding zeros
zpad > 0 && _repprint(out, '0', zpad)
# print actual digits
intv = trunc(Integer, u)
decv = u - intv
if intv == 0 && decv != 0
intv = 1
decv -= 1
end
# print integer part (should always be 0-9)
print(out, Char(Int('0') + intv))
# print decimal part
if prec > 0
print(out, '.')
idecv = round(Integer, decv * exp10(prec))
nd = ndigits(idecv)
nd < prec && _repprint(out, '0', prec - nd)
_outint(out, idecv)
end
# print exponent
print(out, isuppercase(fs.typ) ? 'E' : 'e')
if e >= 0
print(out, '+')
else
print(out, '-')
e = -e
end
e < 10 && print(out, '0')
_outint(out, e)
# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end
function _pfmt_g(out::IO, fs::FormatSpec, x::AbstractFloat)
# Branch according to the exponent
ax = round(abs(x) ;sigdigits=fs.prec)
expnt = ax == 0 ? 0 : floor(Int, log10(ax) )
if -4 <= expnt < fs.prec
newprec = fs.prec - expnt - 1
_pfmt_f(out, FormatSpec(fs ;prec=newprec), x)
else
newprec = fs.prec - 1
_pfmt_e(out, FormatSpec(fs ;prec=newprec), x)
end
end
function _pfmt_specialf(out::IO, fs::FormatSpec, x::AbstractFloat)
if isinf(x)
_pfmt_s(out, fs, x > 0 ? "Inf" : "-Inf")
else
@assert isnan(x)
_pfmt_s(out, fs, "NaN")
end
end