Skip to content

Commit 5fb7c48

Browse files
authored
Merge pull request #48 from jmkuhn/0.7
0.7 compat
2 parents 0643f3b + dd275af commit 5fb7c48

File tree

10 files changed

+58
-55
lines changed

10 files changed

+58
-55
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
julia 0.6
2+
Compat 0.62.0

src/Formatting.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ __precompile__()
33
module Formatting
44

55
import Base.show
6+
using Compat
7+
using Compat.Printf
68

79
export
810
FormatSpec, FormatExpr,

src/cformat.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function generate_formatter( fmt::String )
1313

1414
haskey( formatters, fmt ) && return formatters[fmt]
1515

16-
if !contains( fmt, "'" )
16+
if !occursin("'", fmt)
1717
checkfmt(fmt)
1818
return (formatters[ fmt ] = @eval(x->@sprintf( $fmt, x )))
1919
end
@@ -22,7 +22,7 @@ function generate_formatter( fmt::String )
2222
conversion in "sduifF" ||
2323
error( string("thousand separator not defined for ", conversion, " conversion") )
2424

25-
fmtactual = replace( fmt, "'", "", 1 )
25+
fmtactual = replace( fmt, "'" => "", count=1 )
2626
checkfmt( fmtactual )
2727
conversion in "sfF" ||
2828
return (formatters[ fmt ] = @eval(x->checkcommas(@sprintf( $fmtactual, x ))))
@@ -38,8 +38,8 @@ function generate_formatter( fmt::String )
3838
end
3939

4040
function addcommasreal(s)
41-
dpos = findfirst( s, '.' )
42-
dpos != 0 && return string(addcommas( s[1:dpos-1] ), s[ dpos:end ])
41+
dpos = Compat.findfirst( isequal('.'), s )
42+
dpos !== nothing && return string(addcommas( s[1:dpos-1] ), s[ dpos:end ])
4343
# find the rightmost digit
4444
for i in length( s ):-1:1
4545
isdigit( s[i] ) && return string(addcommas( s[1:i] ), s[i+1:end])
@@ -49,7 +49,7 @@ end
4949

5050
function addcommasrat(s)
5151
# commas are added to only the numerator
52-
spos = findfirst( s, '/' )
52+
spos = Compat.findfirst( isequal('/'), s )
5353
string(addcommas( s[1:spos-1] ), s[spos:end])
5454
end
5555

@@ -123,7 +123,7 @@ function generate_format_string(;
123123
s * conversion
124124
end
125125

126-
function format{T<:Real}( x::T;
126+
function format( x::T;
127127
width::Int=-1,
128128
precision::Int= -1,
129129
leftjustified::Bool=false,
@@ -142,7 +142,7 @@ function format{T<:Real}( x::T;
142142
suffix::AbstractString="", # useful for units/%
143143
autoscale::Symbol=:none, # :metric, :binary or :finance
144144
conversion::String=""
145-
)
145+
) where {T<:Real}
146146
checkwidth = commas
147147
if conversion == ""
148148
if T <: AbstractFloat || T <: Rational && precision != -1
@@ -275,18 +275,18 @@ function format{T<:Real}( x::T;
275275
end
276276
checkwidth = true
277277
elseif !mixedfraction
278-
s = replace( s, "//", fractionsep )
278+
s = replace( s, "//" => fractionsep )
279279
checkwidth = true
280280
end
281281
elseif stripzeros && in( actualconv[1], "fFeEs" )
282-
dpos = findfirst( s, '.')
282+
dpos = Compat.findfirst( isequal('.'), s )
283283
if in( actualconv[1], "eEs" )
284284
if in( actualconv[1], "es" )
285-
epos = findfirst( s, 'e' )
285+
epos = Compat.findfirst( isequal('e'), s )
286286
else
287-
epos = findfirst( s, 'E' )
287+
epos = Compat.findfirst( isequal('E'), s )
288288
end
289-
if epos == 0
289+
if epos === nothing
290290
rpos = length( s )
291291
else
292292
rpos = epos-1
@@ -330,12 +330,12 @@ function format{T<:Real}( x::T;
330330

331331
if checkwidth && width != -1
332332
if length(s) > width
333-
s = replace( s, " ", "", length(s)-width )
333+
s = replace( s, " " => "", count=length(s)-width )
334334
if length(s) > width && endswith( s, " " )
335-
s = reverse( replace( reverse(s), " ", "", length(s)-width ) )
335+
s = reverse( replace( reverse(s), " " => "", count=length(s)-width ) )
336336
end
337337
if length(s) > width
338-
s = replace( s, ",", "", length(s)-width )
338+
s = replace( s, "," => "", count=length(s)-width )
339339
end
340340
elseif length(s) < width
341341
if leftjustified

src/fmtcore.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ _signchar(x::Real, s::Char) = signbit(x) ? '-' :
6767
s == '+' ? '+' :
6868
s == ' ' ? ' ' : '\0'
6969

70-
function _pfmt_int{Op}(out::IO, sch::Char, ip::String, zs::Integer, ax::Integer, op::Op)
70+
function _pfmt_int(out::IO, sch::Char, ip::String, zs::Integer, ax::Integer, op::Op) where {Op}
7171
# print sign
7272
if sch != '\0'
7373
write(out, sch)
@@ -88,8 +88,8 @@ function _pfmt_int{Op}(out::IO, sch::Char, ip::String, zs::Integer, ax::Integer,
8888
end
8989
end
9090

91-
function _pfmt_intdigits{Op,T<:Integer}(out::IO, ax::T, op::Op)
92-
b_lb = _div(ax, op)
91+
function _pfmt_intdigits(out::IO, ax::T, op::Op) where {Op, T<:Integer}
92+
b_lb = _div(ax, op)
9393
b = one(T)
9494
while b <= b_lb
9595
b = _mul(b, op)
@@ -102,7 +102,7 @@ function _pfmt_intdigits{Op,T<:Integer}(out::IO, ax::T, op::Op)
102102
end
103103
end
104104

105-
function _pfmt_i{Op}(out::IO, fs::FormatSpec, x::Integer, op::Op)
105+
function _pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op}
106106
# calculate actual length
107107
ax = abs(x)
108108
xlen = _ndigits(abs(x), op)
@@ -239,7 +239,7 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
239239
end
240240

241241
# print
242-
ec = isupper(fs.typ) ? 'E' : 'e'
242+
ec = isuppercase(fs.typ) ? 'E' : 'e'
243243
wid = fs.width
244244
if wid <= xlen
245245
_pfmt_floate(out, sch, 0, u, fs.prec, e, ec)

src/fmtspec.jl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ _tycls(c::Char) =
2626
(c == 's') ? 's' :
2727
error("Invalid type char $(c)")
2828

29-
immutable FormatSpec
29+
struct FormatSpec
3030
cls::Char # category: 'i' | 'f' | 'c' | 's'
3131
typ::Char
3232
fill::Char
@@ -158,19 +158,16 @@ end
158158

159159
## formatted printing using a format spec
160160

161-
type _Dec end
162-
type _Oct end
163-
type _Hex end
164-
type _HEX end
165-
type _Bin end
161+
mutable struct _Dec end
162+
mutable struct _Oct end
163+
mutable struct _Hex end
164+
mutable struct _HEX end
165+
mutable struct _Bin end
166166

167167
_srepr(x) = repr(x)
168168
_srepr(x::AbstractString) = x
169169
_srepr(x::Char) = string(x)
170-
171-
if isdefined(:Enum)
172-
_srepr(x::Enum) = string(x)
173-
end
170+
_srepr(x::Enum) = string(x)
174171

175172
function printfmt(io::IO, fs::FormatSpec, x)
176173
cls = fs.cls

src/formatexpr.jl

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Argument specification
44

5-
immutable ArgSpec
5+
struct ArgSpec
66
argidx::Int
77
hasfilter::Bool
88
filter::Function
@@ -26,10 +26,11 @@ function make_argspec(s::AbstractString, pos::Int)
2626
ff::Function = Base.identity
2727

2828
if !isempty(s)
29-
ifil = searchindex(s, "|>")
30-
if ifil == 0
29+
filrange = Compat.findfirst("|>", s)
30+
if filrange === nothing
3131
iarg = parse(Int,s)
3232
else
33+
ifil = first(filrange)
3334
iarg = ifil > 1 ? parse(Int,s[1:prevind(s, ifil)]) : -1
3435
hasfil = true
3536
ff = eval(Symbol(s[ifil+2:end]))
@@ -55,16 +56,16 @@ end
5556

5657
### Format entry
5758

58-
immutable FormatEntry
59+
struct FormatEntry
5960
argspec::ArgSpec
6061
spec::FormatSpec
6162
end
6263

6364
function make_formatentry(s::AbstractString, pos::Int)
6465
@assert s[1] == '{' && s[end] == '}'
65-
sc = s[2:prevind(s, endof(s))]
66-
icolon = search(sc, ':')
67-
if icolon == 0 # no colon
66+
sc = s[2:prevind(s, lastindex(s))]
67+
icolon = Compat.findfirst(isequal(':'), sc)
68+
if icolon === nothing # no colon
6869
(argspec, pos) = make_argspec(sc, pos)
6970
spec = FormatSpec('s')
7071
else
@@ -77,7 +78,7 @@ end
7778

7879
### Format expression
7980

80-
type FormatExpr
81+
mutable struct FormatExpr
8182
prefix::String
8283
suffix::String
8384
entries::Vector{FormatEntry}
@@ -87,25 +88,25 @@ end
8788
_raise_unmatched_lbrace() = error("Unmatched { in format expression.")
8889

8990
function find_next_entry_open(s::AbstractString, si::Int)
90-
slen = endof(s)
91-
p = search(s, '{', si)
92-
p < slen || _raise_unmatched_lbrace()
93-
while p > 0 && s[p+1] == '{' # escape `{{`
94-
p = search(s, '{', p+2)
95-
p < slen || _raise_unmatched_lbrace()
91+
slen = lastindex(s)
92+
p = Compat.findnext(isequal('{'), s, si)
93+
(p === nothing || p < slen) || _raise_unmatched_lbrace()
94+
while p !== nothing && s[p+1] == '{' # escape `{{`
95+
p = Compat.findnext(isequal('{'), s, p+2)
96+
(p === nothing || p < slen) || _raise_unmatched_lbrace()
9697
end
9798
# println("open at $p")
98-
pre = p > 0 ? s[si:prevind(s, p)] : s[si:end]
99+
pre = p !== nothing ? s[si:prevind(s, p)] : s[si:end]
99100
if !isempty(pre)
100-
pre = replace(pre, "{{", '{')
101-
pre = replace(pre, "}}", '}')
101+
pre = replace(pre, "{{" => '{')
102+
pre = replace(pre, "}}" => '}')
102103
end
103104
return (p, convert(String, pre))
104105
end
105106

106107
function find_next_entry_close(s::AbstractString, si::Int)
107-
p = search(s, '}', si)
108-
p > 0 || _raise_unmatched_lbrace()
108+
p = Compat.findnext(isequal('}'), s, si)
109+
p !== nothing || _raise_unmatched_lbrace()
109110
# println("close at $p")
110111
return p
111112
end
@@ -118,12 +119,12 @@ function FormatExpr(s::AbstractString)
118119
inter = String[]
119120
# scan
120121
(p, prefix) = find_next_entry_open(s, 1)
121-
if p > 0
122+
if p !== nothing
122123
q = find_next_entry_close(s, p+1)
123124
(e, pos) = make_formatentry(s[p:q], 0)
124125
push!(entries, e)
125126
(p, pre) = find_next_entry_open(s, q+1)
126-
while p > 0
127+
while p !== nothing
127128
push!(inter, pre)
128129
q = find_next_entry_close(s, p+1)
129130
(e, pos) = make_formatentry(s[p:q], pos)

test/cformat.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Formatting
2-
using Base.Test
2+
using Compat.Test
3+
using Compat.Printf
4+
using Compat.Random
35

46
_erfinv(z) = sqrt(π) * Base.Math.@horner(z, 0, 1, 0, π/12, 0, 7π^2/480, 0, 127π^3/40320, 0,
57
4369π^4/5806080, 0, 34807π^5/182476800) / 2

test/fmtspec.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# test format spec parsing
22

33
using Formatting
4-
using Base.Test
4+
using Compat.Test
55

66

77
# default spec

test/formatexpr.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Formatting
2-
using Base.Test
2+
using Compat.Test
33

44
# with positional arguments
55

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Formatting
2-
using Base.Test
2+
using Compat.Test
33

44
include( "cformat.jl" )
55
include( "fmtspec.jl" )

0 commit comments

Comments
 (0)