Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ export
showcompact,
showerror,
split,
splititr,
sprint,
string,
strip,
Expand Down
25 changes: 25 additions & 0 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,31 @@ rsplit(s::String, spl, keep::Bool) = rsplit(s, spl, 0, keep)
rsplit(s::String, spl) = rsplit(s, spl, 0, true)
#rsplit(str::String) = rsplit(str, _default_delims, 0, false)

immutable SplitItr
str::String
splitter
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this immutable would perform better if it was made parametric

immutable SplitItr{S<:String, T}
    str::S
    splitter::T
end


splititr(s, splitter) = SplitItr(s, splitter)
splititr(s) = SplitItr(s, _default_delims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the constructors lowercase method limit the types, so that the NoMethodError arises one step closer to the user?


start(::SplitItr) = 1
done(itr::SplitItr, pos) = pos > endof(itr.str)
eltype(itr::SplitItr) = String

function next(itr::SplitItr, pos)
match = search(itr.str, itr.splitter, pos)
if first(match) > 0
delstart = first(match)
delend = last(match)
else
delstart = endof(itr.str) + 1
delend = delstart
end
chunk = SubString(itr.str, pos, prevind(itr.str, delstart))
return (chunk, nextind(itr.str, delend))
end

function replace(str::ByteString, pattern, repl::Function, limit::Integer)
n = 1
e = endof(str)
Expand Down
5 changes: 5 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ end
@test isequal(split("abcd", r"d+"), ["abc",""])
@test isequal(split("abcd", r"[ad]?"), ["","b","c",""])

# splititr
@test isequal(collect(splititr("foo bar", ' ')), ["foo", "bar"])
@test isequal(collect(splititr("foo bar", " ")), ["foo", "bar"])
@test isequal(collect(splititr("foo bar", r" ")), ["foo", "bar"])

# replace
@test replace("foobar", 'o', '0') == "f00bar"
@test replace("foobar", 'o', '0', 1) == "f0obar"
Expand Down