forked from JuliaString/Format.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmtcore.jl
302 lines (264 loc) · 7.43 KB
/
fmtcore.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
# core formatting functions
export fmt_Number
### auxiliary functions
### print char n times
function _repprint(out::IO, c::AbstractChar, n::Int)
while n > 0
print(out, c)
n -= 1
end
end
### print string or char
function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,AbstractChar})
wid = fs.width
slen = length(s)
if wid <= slen
print(out, s)
else
a = fs.align
if a == '<'
print(out, s)
_repprint(out, fs.fill, wid-slen)
else
_repprint(out, fs.fill, wid-slen)
print(out, s)
end
end
end
### print integers
_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
function _ndigits(x::Integer, op) # suppose x is non-negative
m = 1
q = _div(x, op)
while q > 0
m += 1
q = _div(q, op)
end
return m
end
_ipre(op) = ""
_ipre(::Union{_Hex, _HEX}) = "0x"
_ipre(::_Oct) = "0o"
_ipre(::_Bin) = "0b"
_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'
function _pfmt_int(out::IO, sch::AbstractChar, ip::ASCIIStr, zs::Integer, ax::Integer,
op::Op) where {Op}
# print sign
sch != '\0' && print(out, sch)
# print prefix
!isempty(ip) && print(out, ip)
# print padding zeros
zs > 0 && _repprint(out, '0', zs)
# print actual digits
ax == 0 ? print(out, '0') : _pfmt_intdigits(out, ax, op)
nothing
end
function _pfmt_intdigits(out::IO, ax::T, op::Op) 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 _pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op}
# calculate actual length
ax = abs(x)
xlen = _ndigits(abs(x), op)
# sign char
sch = _signchar(x, fs.sign)
if sch != '\0'
xlen += 1
end
# prefix (e.g. 0x, 0b, 0o)
ip = ""
if fs.ipre
ip = _ipre(op)
xlen += length(ip)
end
# printing
wid = fs.width
if wid <= xlen
_pfmt_int(out, sch, ip, 0, ax, op)
elseif fs.zpad
_pfmt_int(out, sch, ip, wid-xlen, ax, op)
else
a = fs.align
if a == '<'
_pfmt_int(out, sch, ip, 0, ax, op)
_repprint(out, fs.fill, wid-xlen)
else
_repprint(out, fs.fill, wid-xlen)
_pfmt_int(out, sch, ip, 0, ax, op)
end
end
end
### print floating point numbers
function _pfmt_float(out::IO, sch::AbstractChar, zs::Integer, intv::Real, decv::Real, prec::Int)
# print sign
sch != '\0' && print(out, sch)
# print padding zeros
zs > 0 && _repprint(out, '0', zs)
idecv = round(Integer, decv * exp10(prec))
# print integer part
if intv == 0
print(out, '0')
else
_pfmt_intdigits(out, intv, _Dec())
end
# print decimal point
print(out, '.')
# print decimal part
if prec > 0
nd = _ndigits(idecv, _Dec())
nd < prec && _repprint(out, '0', prec - nd)
_pfmt_intdigits(out, idecv, _Dec())
end
end
function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
# separate sign, integer, and decimal part
rax = round(abs(x); digits = fs.prec)
sch = _signchar(x, fs.sign)
intv = trunc(Integer, rax)
decv = rax - intv
# calculate length
xlen = _ndigits(intv, _Dec()) + 1 + fs.prec
sch != '\0' && (xlen += 1)
# print
wid = fs.width
if wid <= xlen
_pfmt_float(out, sch, 0, intv, decv, fs.prec)
elseif fs.zpad
_pfmt_float(out, sch, wid-xlen, intv, decv, fs.prec)
else
a = fs.align
if a == '<'
_pfmt_float(out, sch, 0, intv, decv, fs.prec)
_repprint(out, fs.fill, wid-xlen)
else
_repprint(out, fs.fill, wid-xlen)
_pfmt_float(out, sch, 0, intv, decv, fs.prec)
end
end
end
function _pfmt_floate(out::IO, sch::AbstractChar, zs::Integer, u::Real, prec::Int, e::Integer,
ec::AbstractChar)
intv = trunc(Integer, u)
decv = u - intv
if intv == 0 && decv != 0
intv = 1
decv -= 1
end
_pfmt_float(out, sch, zs, intv, decv, prec)
print(out, ec)
if e >= 0
print(out, '+')
else
print(out, '-')
e = -e
end
e < 10 && print(out, '0')
_pfmt_intdigits(out, e, _Dec())
end
function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
# extract sign, significand, and exponent
ax = abs(x)
sch = _signchar(x, fs.sign)
if ax == 0.0
e = 0
u = zero(x)
else
rax = round(ax; sigdigits = fs.prec + 1)
e = floor(Integer, log10(rax)) # exponent
u = rax * exp10(-e) # significand
end
# calculate length
xlen = 6 + fs.prec
abs(e) > 99 && (xlen += _ndigits(abs(e), _Dec()) - 2)
sch != '\0' && (xlen += 1)
# print
ec = isuppercase(fs.typ) ? 'E' : 'e'
wid = fs.width
if wid <= xlen
_pfmt_floate(out, sch, 0, u, fs.prec, e, ec)
elseif fs.zpad
_pfmt_floate(out, sch, wid-xlen, u, fs.prec, e, ec)
else
a = fs.align
if a == '<'
_pfmt_floate(out, sch, 0, u, fs.prec, e, ec)
_repprint(out, fs.fill, wid-xlen)
else
_repprint(out, fs.fill, wid-xlen)
_pfmt_floate(out, sch, 0, u, fs.prec, e, ec)
end
end
end
function _pfmt_g(out::IO, fs::FormatSpec, x::AbstractFloat)
# number decomposition
ax = abs(x)
if 1.0e-4 <= ax < 1.0e6
_pfmt_f(out, fs, x)
else
_pfmt_e(out, fs, 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
function _pfmt_Number_f(out::IO, fs::FormatSpec, x::Number, _pf::Function)
fsi = FormatSpec(fs, width = -1)
f = x::AbstractFloat->begin
io = IOBuffer()
_pf(io, fsi, x)
String(take!(io))
end
s = fmt_Number(x, f)
_pfmt_s(out, fs, s)
end
function _pfmt_Number_i(out::IO, fs::FormatSpec, x::Number, op::Op, _pf::Function) where {Op}
fsi = FormatSpec(fs, width = -1)
f = x::Integer->begin
io = IOBuffer()
_pf(io, fsi, x, op)
String(take!(io))
end
s = fmt_Number(x, f)
_pfmt_s(out, fs, s)
end
function _pfmt_i(out::IO, fs::FormatSpec, x::Number, op::Op) where {Op}
_pfmt_Number_i(out, fs, x, op, _pfmt_i)
end
function _pfmt_f(out::IO, fs::FormatSpec, x::Number)
_pfmt_Number_f(out, fs, x, _pfmt_f)
end
function _pfmt_e(out::IO, fs::FormatSpec, x::Number)
_pfmt_Number_f(out, fs, x, _pfmt_e)
end
function fmt_Number(x::Complex, f::Function)
s = f(real(x)) * (imag(x) >= 0 ? " + " : " - ") * f(abs(imag(x))) * "im"
end