diff --git a/src/Compat.jl b/src/Compat.jl index 1a40bc175066b..928e2aec3be0d 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -400,4 +400,30 @@ if VERSION < v"0.4.0-dev+2861" export muladd end +if VERSION < v"0.4.0-dev+4734" + function is_valid_utf32(str::Union(Vector{Char}, Vector{UInt32})) + for i=1:length(str) + @inbounds if !is_valid_char(reinterpret(UInt32, str[i])) ; return false ; end + end + return true + end + is_valid_utf32(str::UTF32String) = is_valid_utf32(str.data) + export is_valid_utf32 +end + +if VERSION < v"0.4.0-dev+4939" + import Base.isvalid + isvalid(ch::Char) = is_valid_char(ch) + isvalid(str::ASCIIString) = is_valid_ascii(str) + isvalid(str::UTF8String) = is_valid_utf8(str) + isvalid(str::UTF16String) = is_valid_utf16(str) + isvalid(str::UTF32String) = is_valid_utf32(str) + isvalid(::Type{Char}, ch) = is_valid_char(ch) + isvalid(::Type{ASCIIString}, str) = is_valid_ascii(str) + isvalid(::Type{UTF8String}, str) = is_valid_utf8(str) + isvalid(::Type{UTF16String}, str) = is_valid_utf16(str) + isvalid(::Type{UTF32String}, str) = is_valid_utf32(str) + export isvalid +end + end # module diff --git a/test/runtests.jl b/test/runtests.jl index 203b7ef2a4104..ec9f1d01c87f0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -298,3 +298,25 @@ end # fma and muladd @test fma(3,4,5) == 3*4+5 == muladd(3,4,5) + +# is_valid_utf32 +s = utf32("abc") +@test is_valid_utf32(s) +s = utf32(UInt32[65,0x110000]) +@test !is_valid_utf32(s) + +# 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