Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid double done call in join and add tests #9179

Merged
merged 1 commit into from
Nov 30, 2014
Merged
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
12 changes: 8 additions & 4 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1394,19 +1394,23 @@ function print_joined(io, strings, delim, last)
end
str, i = next(strings,i)
print(io, str)
while !done(strings,i)
is_done = done(strings,i)
while !is_done
str, i = next(strings,i)
print(io, done(strings,i) ? last : delim)
is_done = done(strings,i)
print(io, is_done ? last : delim)
print(io, str)
end
end

function print_joined(io, strings, delim)
i = start(strings)
while !done(strings,i)
is_done = done(strings,i)
while !is_done
str, i = next(strings,i)
is_done = done(strings,i)
print(io, str)
if !done(strings,i)
if !is_done
print(io, delim)
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,20 @@ end
# This caused JuliaLang/JSON.jl#82
@test first('\x00':'\x7f') === '\x00'
@test last('\x00':'\x7f') === '\x7f'

# Tests of join()
@test join([]) == ""
@test join(["a"],"?") == "a"
@test join("HELLO",'-') == "H-E-L-L-O"
@test join(1:5, ", ", " and ") == "1, 2, 3, 4 and 5"
@test join(["apples", "bananas", "pineapples"], ", ", " and ") == "apples, bananas and pineapples"

# issue #9178 `join` calls `done()` twice on the iterables
type i9178
nnext::Int64
ndone::Int64
end
Base.start(jt::i9178) = (jt.nnext=0 ; jt.ndone=0 ; 0)
Base.done(jt::i9178, n) = (jt.ndone += 1 ; n > 3)
Base.next(jt::i9178, n) = (jt.nnext += 1 ; ("$(jt.nnext),$(jt.ndone)", n+1))
@test join(i9178(0,0), ";") == "1,1;2,2;3,3;4,4"