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

Make next behave like nextind for String #24423

Closed
wants to merge 1 commit into from
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
20 changes: 15 additions & 5 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,22 @@ end
end
trailing = utf8_trailing[b + 1]
if l < i + trailing
return '\ufffd', i+1
end
c::UInt32 = 0
@inbounds for j = 1:(trailing + 1)
for j in (i+1):l
@inbounds if !is_valid_continuation(codeunit(s, j))
return '\ufffd' j
Copy link

@Liso77 Liso77 Oct 31, 2017

Choose a reason for hiding this comment

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

comma is missing here? return '\ufffd', j

Copy link
Member Author

Choose a reason for hiding this comment

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

thank you for the catch! I will fix it is decided that this should be merged (for now I feel that the proposal of @StefanKarpinski in #24420 will make it redundant).

end
end
return '\ufffd', l+1
end
@inbounds c::UInt32 = codeunit(s, i)
i += 1
@inbounds for j = 1:trailing
b = codeunit(s, i)
if !is_valid_continuation(b)
return '\ufffd', i
end
c <<= 6
c += codeunit(s, i)
c += b
i += 1
end
c -= utf8_offset[trailing + 1]
Expand Down
14 changes: 14 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,24 @@ end
@test lcfirst("")==""
@test lcfirst("*")=="*"
end

# test next for invalid UTF-8 AbstractStrings
@testset "next for invalid UTF-8" begin
srand(1)
let s = "1"*String(rand(0x00:0xff, 2^16)), i = 1, j = 1
while !done(s, i)
c, i = next(s, i)
j = nextind(s, j)
@test i == j
end
end
end

# test AbstractString functions at beginning of string.jl
struct tstStringType <: AbstractString
data::Array{UInt8,1}
end

@testset "AbstractString functions" begin
tstr = tstStringType(Vector{UInt8}("12"))
@test_throws ErrorException endof(tstr)
Expand Down