Skip to content

Commit

Permalink
Merge pull request #17 from tanmaykm/tanmaykm
Browse files Browse the repository at this point in the history
add compat for rsplit
  • Loading branch information
tanmaykm committed Dec 3, 2014
2 parents 7743203 + 057650b commit 45140ea
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ Currently, the `@compat` macro supports the following syntaxes:

* `@compat split(str, splitter; keywords...)` - the Julia 0.4-style keyword-based `split` function

* `@compat rsplit(str, splitter; keywords...)` - the Julia 0.4-style keyword-based `rsplit` function

## Type Aliases

* `typealias AbstractString String` - `String` has been renamed to `AbstractString` [#8872](https://github.com/JuliaLang/julia/pull/8872)

* For all unsigned integer types to their equivalents with uppercase `I`. [#8907](https://github.com/JuliaLang/julia/pull/8907)

## Renamed functions

* `itrunc`, `iround`, `iceil`, `ifloor` are now accessed via `trunc(T, x)`, etc. [#9133](https://github.com/JuliaLang/julia/pull/9133)
Expand Down
18 changes: 9 additions & 9 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function rewrite_dict(ex)
newex
end

# rewrite Julia 0.4-style split(str, splitter; kws...) into 0.2/0.3-style
# positional arguments
function rewrite_split(ex)
# rewrite Julia 0.4-style split or rsplit (str, splitter; kws...)
# into 0.2/0.3-style positional arguments
function rewrite_split(ex, f)
limit = nothing
keep = nothing
for i in 4:length(ex.args)
Expand All @@ -84,15 +84,15 @@ function rewrite_split(ex)
end
if limit == nothing
if keep == nothing
return Expr(:call, :split, ex.args[2], ex.args[3])
return Expr(:call, f, ex.args[2], ex.args[3])
else
return Expr(:call, :split, ex.args[2], ex.args[3], keep)
return Expr(:call, f, ex.args[2], ex.args[3], keep)
end
else
if keep == nothing
return Expr(:call, :split, ex.args[2], ex.args[3], limit)
return Expr(:call, f, ex.args[2], ex.args[3], limit)
else
return Expr(:call, :split, ex.args[2], ex.args[3], limit, keep)
return Expr(:call, f, ex.args[2], ex.args[3], limit, keep)
end
end
end
Expand All @@ -102,8 +102,8 @@ function _compat(ex::Expr)
f = ex.args[1]
if VERSION < v"0.4.0-dev+980" && (f == :Dict || (isexpr(f, :curly) && length(f.args) == 3 && f.args[1] == :Dict))
ex = rewrite_dict(ex)
elseif VERSION < v"0.4.0-dev+129" && f == :split && length(ex.args) >= 4 && isexpr(ex.args[4], :kw)
ex = rewrite_split(ex)
elseif VERSION < v"0.4.0-dev+129" && (f == :split || f == :rsplit) && length(ex.args) >= 4 && isexpr(ex.args[4], :kw)
ex = rewrite_split(ex, f)
end
end
return Expr(ex.head, map(_compat, ex.args)...)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ end
@test @compat split("a,b,,c", ',', keep=false) == ["a", "b", "c"]
@test @compat split("a,b,,c", ',', keep=true) == ["a", "b", "", "c"]

@test @compat rsplit("a,b,,c", ',', limit=2) == ["a,b,", "c"]
@test @compat rsplit("a,b,,c", ',', limit=2,keep=true) == ["a,b,", "c"]
@test @compat rsplit("a,b,,c", ',', keep=false) == ["a", "b", "c"]
@test @compat rsplit("a,b,,c", ',', keep=true) == ["a", "b", "", "c"]

if VERSION < v"0.4.0-dev+1387"
@test isdefined(Main, :AbstractString)
end

0 comments on commit 45140ea

Please sign in to comment.