Skip to content

Commit e5b1ede

Browse files
ScottPJonestkelman
authored andcommitted
Fix JuliaLang#11140 Added is_valid_utf32
1 parent 9a99ae0 commit e5b1ede

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ export
824824
is_valid_char,
825825
is_valid_utf8,
826826
is_valid_utf16,
827+
is_valid_utf32,
827828
isalnum,
828829
isalpha,
829830
isascii,

base/utf32.jl

+8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ function convert(T::Type{UTF32String}, bytes::AbstractArray{UInt8})
9292
UTF32String(d)
9393
end
9494

95+
function is_valid_utf32(s::Union(Vector{Char}, Vector{UInt32}))
96+
for i=1:length(s)
97+
@inbounds if !is_valid_char(reinterpret(UInt32, s[i])) ; return false ; end
98+
end
99+
return true
100+
end
101+
is_valid_utf32(s::UTF32String) = is_valid_utf32(s.data)
102+
95103
utf32(p::Ptr{Char}, len::Integer) = utf32(pointer_to_array(p, len))
96104
utf32(p::Union(Ptr{UInt32}, Ptr{Int32}), len::Integer) = utf32(convert(Ptr{Char}, p), len)
97105
function utf32(p::Union(Ptr{Char}, Ptr{UInt32}, Ptr{Int32}))

test/strings.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,11 @@ end
12901290
@test is_valid_utf16(utf16("\ud800")) == false
12911291
# TODO is_valid_utf8
12921292

1293+
# Issue #11140
1294+
@test is_valid_utf32(utf32("a")) == true
1295+
@test is_valid_utf32(utf32("\x00")) == true
1296+
@test is_valid_utf32(UInt32[0xd800,0]) == false
1297+
12931298
# This caused JuliaLang/JSON.jl#82
12941299
@test first('\x00':'\x7f') === '\x00'
12951300
@test last('\x00':'\x7f') === '\x7f'

0 commit comments

Comments
 (0)