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

Add 2-argument isvalid(::Type{T},str::T) #11482

Merged
merged 2 commits into from
May 29, 2015
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
1 change: 1 addition & 0 deletions base/utf32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function isvalid(::Type{UTF32String}, str::Union(Vector{Char}, Vector{UInt32}))
end
isvalid(str::Vector{Char}) = isvalid(UTF32String, str)
isvalid{T<:Union(ASCIIString,UTF8String,UTF16String,UTF32String)}(str::T) = isvalid(T, str.data)
isvalid{T<:Union(ASCIIString,UTF8String,UTF16String,UTF32String)}(::Type{T}, str::T) = isvalid(T, str.data)

utf32(p::Ptr{Char}, len::Integer) = utf32(pointer_to_array(p, len))
utf32(p::Union(Ptr{UInt32}, Ptr{Int32}), len::Integer) = utf32(convert(Ptr{Char}, p), len)
Expand Down
18 changes: 18 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,24 @@ end
# Check seven-byte sequences, should be invalid
@test isvalid(UTF8String, UInt8[0xfe, 0x80, 0x80, 0x80, 0x80, 0x80]) == false

# 11482

# isvalid
let s = "abcdef", u8 = "abcdef\uff", u16 = utf16(u8), u32 = utf32(u8),
bad32 = utf32(UInt32[65,0x110000]), badch = Char[0x110000][1]

@test !isvalid(bad32)
@test !isvalid(badch)
@test isvalid(s)
@test isvalid(u8)
@test isvalid(u16)
@test isvalid(u32)
@test isvalid(ASCIIString, s)
@test isvalid(UTF8String, u8)
@test isvalid(UTF16String, u16)
@test isvalid(UTF32String, u32)
end

# This caused JuliaLang/JSON.jl#82
@test first('\x00':'\x7f') === '\x00'
@test last('\x00':'\x7f') === '\x7f'
Expand Down