Skip to content

Commit

Permalink
Fix JuliaLang#11140 Added is_valid_utf32
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones authored and tkelman committed Jun 6, 2015
1 parent 9a99ae0 commit e5b1ede
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ export
is_valid_char,
is_valid_utf8,
is_valid_utf16,
is_valid_utf32,
isalnum,
isalpha,
isascii,
Expand Down
8 changes: 8 additions & 0 deletions base/utf32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ function convert(T::Type{UTF32String}, bytes::AbstractArray{UInt8})
UTF32String(d)
end

function is_valid_utf32(s::Union(Vector{Char}, Vector{UInt32}))
for i=1:length(s)
@inbounds if !is_valid_char(reinterpret(UInt32, s[i])) ; return false ; end
end
return true
end
is_valid_utf32(s::UTF32String) = is_valid_utf32(s.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)
function utf32(p::Union(Ptr{Char}, Ptr{UInt32}, Ptr{Int32}))
Expand Down
5 changes: 5 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,11 @@ end
@test is_valid_utf16(utf16("\ud800")) == false
# TODO is_valid_utf8

# Issue #11140
@test is_valid_utf32(utf32("a")) == true
@test is_valid_utf32(utf32("\x00")) == true
@test is_valid_utf32(UInt32[0xd800,0]) == false

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

0 comments on commit e5b1ede

Please sign in to comment.