@@ -19,15 +19,12 @@ function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,AbstractChar})
19
19
slen = length (s)
20
20
if wid <= slen
21
21
print (out, s)
22
+ elseif fs. align == ' <'
23
+ print (out, s)
24
+ _repprint (out, fs. fill, wid- slen)
22
25
else
23
- a = fs. align
24
- if a == ' <'
25
- print (out, s)
26
- _repprint (out, fs. fill, wid- slen)
27
- else
28
- _repprint (out, fs. fill, wid- slen)
29
- print (out, s)
30
- end
26
+ _repprint (out, fs. fill, wid- slen)
27
+ print (out, s)
31
28
end
32
29
end
33
30
@@ -44,6 +41,11 @@ _div(x::Integer, ::_Bin) = x >> 1
44
41
_div (x:: Integer , :: _Oct ) = x >> 3
45
42
_div (x:: Integer , :: Union{_Hex, _HEX} ) = x >> 4
46
43
44
+ _str (x:: Integer , :: _Dec ) = string (x, base= 10 )
45
+ _str (x:: Integer , :: _Bin ) = string (x, base= 2 )
46
+ _str (x:: Integer , :: _Oct ) = string (x, base= 8 )
47
+ _str (x:: Integer , :: Union{_Hex, _HEX} ) = string (x, base= 16 )
48
+
47
49
function _ndigits (x:: Integer , op) # suppose x is non-negative
48
50
m = 1
49
51
q = _div (x, op)
@@ -96,15 +98,53 @@ function _pfmt_intdigits(out::IO, ax::T, op::Op) where {Op, T<:Integer}
96
98
end
97
99
end
98
100
101
+ function _pfmt_intmin (out:: IO , ip:: ASCIIStr , zs:: Integer , s:: String )
102
+ # print sign
103
+ print (out, ' -' )
104
+ # print prefix
105
+ isempty (ip) || print (out, ip)
106
+ # print padding zeros
107
+ zs > 0 && _repprint (out, ' 0' , zs)
108
+ # print actual digits
109
+ print (out, SubString (s, 2 ))
110
+ nothing
111
+ end
112
+
113
+ # Special case were abs would give error
114
+ function _pfmt_imin (out:: IO , fs:: FormatSpec , x:: Integer , op:: Op ) where {Op}
115
+ s = _str (x, op)
116
+ xlen = length (s)
117
+ # prefix (e.g. 0x, 0b, 0o)
118
+ ip = " "
119
+ if fs. ipre
120
+ ip = _ipre (op)
121
+ xlen += length (ip)
122
+ end
123
+
124
+ # printing
125
+ wid = fs. width
126
+ if wid <= xlen
127
+ _pfmt_intmin (out, ip, 0 , s)
128
+ elseif fs. zpad
129
+ _pfmt_intmin (out, ip, wid- xlen, s)
130
+ elseif fs. align == ' <'
131
+ _pfmt_intmin (out, ip, 0 , s)
132
+ _repprint (out, fs. fill, wid- xlen)
133
+ else
134
+ _repprint (out, fs. fill, wid- xlen)
135
+ _pfmt_intmin (out, ip, 0 , s)
136
+ end
137
+ end
138
+
99
139
function _pfmt_i (out:: IO , fs:: FormatSpec , x:: Integer , op:: Op ) where {Op}
140
+ # Specially handle edge case of typemin
141
+ x === typemin (typeof (x)) && x isa Signed && return _pfmt_imin (out, fs, x, op)
100
142
# calculate actual length
101
143
ax = abs (x)
102
- xlen = _ndigits (abs (x) , op)
144
+ xlen = _ndigits (ax , op)
103
145
# sign char
104
146
sch = _signchar (x, fs. sign)
105
- if sch != ' \0 '
106
- xlen += 1
107
- end
147
+ xlen += (sch != ' \0 ' )
108
148
# prefix (e.g. 0x, 0b, 0o)
109
149
ip = " "
110
150
if fs. ipre
@@ -118,15 +158,12 @@ function _pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op}
118
158
_pfmt_int (out, sch, ip, 0 , ax, op)
119
159
elseif fs. zpad
120
160
_pfmt_int (out, sch, ip, wid- xlen, ax, op)
161
+ elseif fs. align == ' <'
162
+ _pfmt_int (out, sch, ip, 0 , ax, op)
163
+ _repprint (out, fs. fill, wid- xlen)
121
164
else
122
- a = fs. align
123
- if a == ' <'
124
- _pfmt_int (out, sch, ip, 0 , ax, op)
125
- _repprint (out, fs. fill, wid- xlen)
126
- else
127
- _repprint (out, fs. fill, wid- xlen)
128
- _pfmt_int (out, sch, ip, 0 , ax, op)
129
- end
165
+ _repprint (out, fs. fill, wid- xlen)
166
+ _pfmt_int (out, sch, ip, 0 , ax, op)
130
167
end
131
168
end
132
169
0 commit comments