1- formatters = Dict { Compat.ASCIIString , Function } ()
1+ formatters = Dict { String , Function } ()
22
3- function sprintf1 ( fmt:: Compat.ASCIIString , x )
4- global formatters
5- f = generate_formatter ( fmt )
6- f ( x )
3+ sprintf1 ( fmt:: String , x ) = eval (Expr (:call , generate_formatter ( fmt ), x))
4+
5+ function checkfmt (fmt)
6+ test = Base. Printf. parse ( fmt )
7+ (length ( test ) == 1 && typeof ( test[1 ] ) <: Tuple ) ||
8+ error ( " Only one AND undecorated format string is allowed" )
79end
810
9- function generate_formatter ( fmt:: Compat.ASCIIString )
11+ function generate_formatter ( fmt:: String )
1012 global formatters
11- if haskey ( formatters, fmt )
12- return formatters[fmt]
13- end
14- func = @compat Symbol (" sprintf_" , replace (base64encode (fmt), " =" , " !" ))
13+
14+ haskey ( formatters, fmt ) && return formatters[fmt]
1515
1616 if ! contains ( fmt, " '" )
17- test = Base. Printf. parse ( fmt )
18- if length ( test ) != 1 || ! ( typeof ( test[1 ] ) <: Tuple )
19- error ( " Only one AND undecorated format string is allowed" )
20- end
17+ checkfmt (fmt)
18+ return (formatters[ fmt ] = @eval (x-> @sprintf ( $ fmt, x )))
19+ end
2120
22- code = quote
23- function $func ( x )
24- @sprintf ( $ fmt, x )
25- end
26- end
27- else
28- conversion = fmt[end ]
29- if ! in ( conversion, " sduifF" )
30- error ( " thousand separator not defined for " * string ( conversion ) * " conversion" )
31- end
32- fmtactual = replace ( fmt, " '" , " " , 1 )
33- test = Base. Printf. parse ( fmtactual )
34- if length ( test ) != 1 || ! ( typeof ( test[1 ] ) <: Tuple )
35- error ( " Only one AND undecorated format string is allowed" )
36- end
37- if in ( conversion, " sfF" )
38- code = quote
39- function $func {T<:Real} ( x:: T )
40- s = @sprintf ( $ fmtactual, x )
41- # commas are added to only the numerator
42- if T <: Rational && endswith ( $ fmtactual, " s" )
43- spos = findfirst ( s, ' /' )
44- s = addcommas ( s[1 : spos- 1 ] ) * s[spos: end ]
45- else
46- dpos = findfirst ( s, ' .' )
47- if dpos != 0
48- s = addcommas ( s[1 : dpos- 1 ] ) * s[ dpos: end ]
49- else # find the rightmost digit
50- for i in length ( s ): - 1 : 1
51- if isdigit ( s[i] )
52- s = addcommas ( s[1 : i] ) * s[i+ 1 : end ]
53- break
54- end
55- end
56- end
57- end
58- s
59- end
60- end
21+ conversion = fmt[end ]
22+ conversion in " sduifF" ||
23+ error ( string (" thousand separator not defined for " , conversion, " conversion" ) )
24+
25+ fmtactual = replace ( fmt, " '" , " " , 1 )
26+ checkfmt ( fmtactual )
27+ conversion in " sfF" ||
28+ return (formatters[ fmt ] = @eval (x-> checkcommas (@sprintf ( $ fmtactual, x ))))
29+
30+ formatters[ fmt ] =
31+ if endswith ( fmtactual, ' s' )
32+ @eval ((x:: Real )-> ((eltype (x) <: Rational )
33+ ? addcommasrat (@sprintf ( $ fmtactual, x ))
34+ : addcommasreal (@sprintf ( $ fmtactual, x ))))
6135 else
62- code = quote
63- function $func ( x )
64- s = @sprintf ( $ fmtactual, x )
65- for i in length ( s ): - 1 : 1
66- if isdigit ( s[i] )
67- s = addcommas ( s[1 : i] ) * s[i+ 1 : end ]
68- break
69- end
70- end
71- s
72- end
73- end
36+ @eval ((x:: Real )-> addcommasreal (@sprintf ( $ fmtactual, x )))
7437 end
38+ end
39+
40+ function addcommasreal (s)
41+ dpos = findfirst ( s, ' .' )
42+ dpos != 0 && return string (addcommas ( s[1 : dpos- 1 ] ), s[ dpos: end ])
43+ # find the rightmost digit
44+ for i in length ( s ): - 1 : 1
45+ isdigit ( s[i] ) && return string (addcommas ( s[1 : i] ), s[i+ 1 : end ])
7546 end
76- f = eval ( code )
77- formatters[ fmt ] = f
78- f
47+ s
7948end
8049
81- function addcommas ( s:: Compat.ASCIIString )
50+ function addcommasrat (s)
51+ # commas are added to only the numerator
52+ spos = findfirst ( s, ' /' )
53+ string (addcommas ( s[1 : spos- 1 ] ), s[spos: end ])
54+ end
55+
56+ function checkcommas (s)
57+ for i in length ( s ): - 1 : 1
58+ if isdigit ( s[i] )
59+ s = string (addcommas ( s[1 : i] ), s[i+ 1 : end ])
60+ break
61+ end
62+ end
63+ s
64+ end
65+
66+
67+ function addcommas ( s:: String )
8268 len = length (s)
8369 t = " "
8470 for i in 1 : 3 : len
@@ -105,7 +91,7 @@ function generate_format_string(;
10591 signed:: Bool = false ,
10692 positivespace:: Bool = false ,
10793 alternative:: Bool = false ,
108- conversion:: Compat.ASCIIString = " f" # aAdecEfFiosxX
94+ conversion:: String = " f" # aAdecEfFiosxX
10995 )
11096 s = " %"
11197 if commas
@@ -155,7 +141,7 @@ function format{T<:Real}( x::T;
155141 tryden:: Int = 0 , # if 2 or higher, try to use this denominator, without losing precision
156142 suffix:: AbstractString = " " , # useful for units/%
157143 autoscale:: Symbol = :none , # :metric, :binary or :finance
158- conversion:: Compat.ASCIIString = " "
144+ conversion:: String = " "
159145 )
160146 checkwidth = commas
161147 if conversion == " "
0 commit comments