Skip to content

Commit e6f6887

Browse files
authored
Merge pull request #37 from tkelman/spj/fixworldage2
Make package 0.6-only, fix world age error
2 parents 452aa89 + 93b91e2 commit e6f6887

File tree

8 files changed

+97
-114
lines changed

8 files changed

+97
-114
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ os:
33
- linux
44
- osx
55
julia:
6-
- 0.4
7-
- 0.5
6+
- 0.6
87
- nightly
98
notifications:
109
email: false

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
julia 0.4
2-
Compat 0.8.0
1+
julia 0.6-pre

src/Formatting.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ module Formatting
99
printfmt, printfmtln, fmt, format,
1010
sprintf1, generate_formatter
1111

12-
using Compat
13-
1412
include("cformat.jl" )
1513
include("fmtspec.jl")
1614
include("fmtcore.jl")

src/cformat.jl

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,70 @@
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")
79
end
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
7948
end
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 == ""

src/fmtcore.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ end
1212

1313
### print string or char
1414

15-
@compat function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,Char})
15+
function _pfmt_s(out::IO, fs::FormatSpec, s::Union{AbstractString,Char})
1616
wid = fs.width
1717
slen = length(s)
1818
if wid <= slen
@@ -35,12 +35,12 @@ end
3535
_mul(x::Integer, ::_Dec) = x * 10
3636
_mul(x::Integer, ::_Bin) = x << 1
3737
_mul(x::Integer, ::_Oct) = x << 3
38-
@compat _mul(x::Integer, ::Union{_Hex, _HEX}) = x << 4
38+
_mul(x::Integer, ::Union{_Hex, _HEX}) = x << 4
3939

4040
_div(x::Integer, ::_Dec) = div(x, 10)
4141
_div(x::Integer, ::_Bin) = x >> 1
4242
_div(x::Integer, ::_Oct) = x >> 3
43-
@compat _div(x::Integer, ::Union{_Hex, _HEX}) = x >> 4
43+
_div(x::Integer, ::Union{_Hex, _HEX}) = x >> 4
4444

4545
function _ndigits(x::Integer, op) # suppose x is non-negative
4646
m = 1
@@ -53,21 +53,21 @@ function _ndigits(x::Integer, op) # suppose x is non-negative
5353
end
5454

5555
_ipre(op) = ""
56-
@compat _ipre(::Union{_Hex, _HEX}) = "0x"
56+
_ipre(::Union{_Hex, _HEX}) = "0x"
5757
_ipre(::_Oct) = "0o"
5858
_ipre(::_Bin) = "0b"
5959

60-
_digitchar(x::Integer, ::_Bin) = @compat Char(x == 0 ? '0' : '1')
61-
_digitchar(x::Integer, ::_Dec) = @compat Char('0' + x)
62-
_digitchar(x::Integer, ::_Oct) = @compat Char('0' + x)
63-
_digitchar(x::Integer, ::_Hex) = @compat Char(x < 10 ? '0' + x : 'a' + (x - 10))
64-
_digitchar(x::Integer, ::_HEX) = @compat Char(x < 10 ? '0' + x : 'A' + (x - 10))
60+
_digitchar(x::Integer, ::_Bin) = Char(x == 0 ? '0' : '1')
61+
_digitchar(x::Integer, ::_Dec) = Char('0' + x)
62+
_digitchar(x::Integer, ::_Oct) = Char('0' + x)
63+
_digitchar(x::Integer, ::_Hex) = Char(x < 10 ? '0' + x : 'a' + (x - 10))
64+
_digitchar(x::Integer, ::_HEX) = Char(x < 10 ? '0' + x : 'A' + (x - 10))
6565

6666
_signchar(x::Number, s::Char) = x < 0 ? '-' :
6767
s == '+' ? '+' :
6868
s == ' ' ? ' ' : '\0'
6969

70-
function _pfmt_int{Op}(out::IO, sch::Char, ip::Compat.ASCIIString, zs::Integer, ax::Integer, op::Op)
70+
function _pfmt_int{Op}(out::IO, sch::Char, ip::String, zs::Integer, ax::Integer, op::Op)
7171
# print sign
7272
if sch != '\0'
7373
write(out, sch)
@@ -222,8 +222,8 @@ function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::In
222222
e = -e
223223
end
224224
(e1, e2) = divrem(e, 10)
225-
write(out, @compat Char('0' + e1))
226-
write(out, @compat Char('0' + e2))
225+
write(out, Char('0' + e1))
226+
write(out, Char('0' + e2))
227227
end
228228

229229

src/fmtspec.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function printfmt(io::IO, fs::FormatSpec, x)
176176
cls = fs.cls
177177
ty = fs.typ
178178
if cls == 'i'
179-
ix = @compat Integer(x)
179+
ix = Integer(x)
180180
ty == 'd' || ty == 'n' ? _pfmt_i(io, fs, ix, _Dec()) :
181181
ty == 'x' ? _pfmt_i(io, fs, ix, _Hex()) :
182182
ty == 'X' ? _pfmt_i(io, fs, ix, _HEX()) :
@@ -194,7 +194,7 @@ function printfmt(io::IO, fs::FormatSpec, x)
194194
elseif cls == 's'
195195
_pfmt_s(io, fs, _srepr(x))
196196
else # cls == 'c'
197-
_pfmt_s(io, fs, @compat Char(x))
197+
_pfmt_s(io, fs, Char(x))
198198
end
199199
end
200200

src/formatexpr.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function make_argspec(s::AbstractString, pos::Int)
3232
else
3333
iarg = ifil > 1 ? parse(Int,s[1:ifil-1]) : -1
3434
hasfil = true
35-
ff = eval(@compat Symbol(s[ifil+2:end]))
35+
ff = eval(Symbol(s[ifil+2:end]))
3636
end
3737
end
3838

@@ -78,10 +78,10 @@ end
7878
### Format expression
7979

8080
type FormatExpr
81-
prefix::Compat.UTF8String
82-
suffix::Compat.UTF8String
81+
prefix::String
82+
suffix::String
8383
entries::Vector{FormatEntry}
84-
inter::Vector{Compat.UTF8String}
84+
inter::Vector{String}
8585
end
8686

8787
_raise_unmatched_lbrace() = error("Unmatched { in format expression.")
@@ -100,7 +100,7 @@ function find_next_entry_open(s::AbstractString, si::Int)
100100
pre = replace(pre, "{{", '{')
101101
pre = replace(pre, "}}", '}')
102102
end
103-
return (p, convert(Compat.UTF8String, pre))
103+
return (p, convert(String, pre))
104104
end
105105

106106
function find_next_entry_close(s::AbstractString, si::Int)
@@ -115,10 +115,10 @@ function FormatExpr(s::AbstractString)
115115
slen = length(s)
116116

117117
# init
118-
prefix = convert(Compat.UTF8String, "")
119-
suffix = convert(Compat.UTF8String, "")
118+
prefix = ""
119+
suffix = ""
120120
entries = FormatEntry[]
121-
inter = Compat.UTF8String[]
121+
inter = String[]
122122

123123
# scan
124124
(p, prefix) = find_next_entry_open(s, 1)
@@ -160,10 +160,10 @@ function printfmt(io::IO, fe::FormatExpr, args...)
160160
end
161161

162162
printfmt(io::IO, fe::AbstractString, args...) = printfmt(io, FormatExpr(fe), args...)
163-
@compat printfmt(fe::Union{AbstractString,FormatExpr}, args...) = printfmt(STDOUT, fe, args...)
163+
printfmt(fe::Union{AbstractString,FormatExpr}, args...) = printfmt(STDOUT, fe, args...)
164164

165-
@compat printfmtln(io::IO, fe::Union{AbstractString,FormatExpr}, args...) = (printfmt(io, fe, args...); println(io))
166-
@compat printfmtln(fe::Union{AbstractString,FormatExpr}, args...) = printfmtln(STDOUT, fe, args...)
165+
printfmtln(io::IO, fe::Union{AbstractString,FormatExpr}, args...) = (printfmt(io, fe, args...); println(io))
166+
printfmtln(fe::Union{AbstractString,FormatExpr}, args...) = printfmtln(STDOUT, fe, args...)
167167

168-
@compat format(fe::Union{AbstractString,FormatExpr}, args...) =
168+
format(fe::Union{AbstractString,FormatExpr}, args...) =
169169
sprint(printfmt, fe, args...)

test/cformat.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Formatting
2-
using Compat
32
using Base.Test
43

54
_erfinv(z) = sqrt(π) * Base.Math.@horner(z, 0, 1, 0, π/12, 0, 7π^2/480, 0, 127π^3/40320, 0,
@@ -8,11 +7,13 @@ _erfinv(z) = sqrt(π) * Base.Math.@horner(z, 0, 1, 0, π/12, 0, 7π^2/480, 0, 12
87
function test_equality()
98
println( "test cformat equality...")
109
srand(10)
11-
fmts = Compat.ASCIIString[ "%10.4f", "%f", "%e", "%10f", "%.3f", "%.3e" ]
12-
for fmt in fmts
13-
l = :( x-> x )
14-
l.args[2].args[2] = @compat Expr(:macrocall, Symbol("@sprintf"), fmt, :x)
15-
mfmtr = eval( l )
10+
fmts = [ (x->@sprintf("%10.4f",x), "%10.4f"),
11+
(x->@sprintf("%f", x), "%f"),
12+
(x->@sprintf("%e", x), "%e"),
13+
(x->@sprintf("%10f", x), "%10f"),
14+
(x->@sprintf("%.3f", x), "%.3f"),
15+
(x->@sprintf("%.3e", x), "%.3e")]
16+
for (mfmtr,fmt) in fmts
1617
for i in 1:10000
1718
n = _erfinv( rand() * 1.99 - 1.99/2.0 )
1819
expect = mfmtr( n )
@@ -21,11 +22,11 @@ function test_equality()
2122
end
2223
end
2324

24-
fmts = Compat.ASCIIString[ "%d", "%10d", "%010d", "%-10d" ]
25-
for fmt in fmts
26-
l = :( x-> x )
27-
l.args[2].args[2] = @compat Expr(:macrocall, Symbol("@sprintf"), fmt, :x)
28-
mfmtr = eval( l )
25+
fmts = [ (x->@sprintf("%d",x), "%d"),
26+
(x->@sprintf("%10d",x), "%10d"),
27+
(x->@sprintf("%010d",x), "%010d"),
28+
(x->@sprintf("%-10d",x), "%-10d")]
29+
for (mfmtr,fmt) in fmts
2930
for i in 1:10000
3031
j = round(Int, _erfinv( rand() * 1.99 - 1.99/2.0 ) * 100000 )
3132
expect = mfmtr( j )

0 commit comments

Comments
 (0)