diff --git a/base/utf32.jl b/base/utf32.jl index 306abcb24a95d..3d6390cb21bdd 100644 --- a/base/utf32.jl +++ b/base/utf32.jl @@ -3,15 +3,16 @@ ## UTF-32 in the native byte order, i.e. plain old character arrays ## immutable UTF32String <: DirectIndexString - data::Array{Char,1} # includes 32-bit NULL termination after string chars + data::Vector{Char} # includes 32-bit NULL termination after string chars - function UTF32String(a::Array{Char,1}) + function UTF32String(a::Vector{Char}) if length(a) < 1 || a[end] != Char(0) throw(ArgumentError("UTF32String data must be NULL-terminated")) end new(a) end end +UTF32String(data::Vector{UInt32}) = UTF32String(reinterpret(Char, data)) next(s::UTF32String, i::Int) = (s.data[i], i+1) endof(s::UTF32String) = length(s.data) - 1 diff --git a/doc/stdlib/strings.rst b/doc/stdlib/strings.rst index b05d50f3fb72f..da0bd958fb93d 100644 --- a/doc/stdlib/strings.rst +++ b/doc/stdlib/strings.rst @@ -383,7 +383,7 @@ .. function:: utf32(s) - Create a UTF-32 string from a byte array, array of ``UInt32``, or + Create a UTF-32 string from a byte array, array of ``Char`` or ``UInt32``, or any other string type. (Conversions of byte arrays check for a byte-order marker in the first four bytes, and do not include it in the resulting string.) @@ -393,7 +393,7 @@ string (so that it is mostly invisible in Julia); this allows the string to be passed directly to external functions requiring NUL-terminated data. This NUL is appended automatically by the - `utf32(s)` conversion function. If you have a ``UInt32`` array + `utf32(s)` conversion function. If you have a ``Char`` or ``UInt32`` array ``A`` that is already NUL-terminated UTF-32 data, then you can instead use `UTF32String(A)`` to construct the string without making a copy of the data and treating the NUL as a terminator diff --git a/test/strings.jl b/test/strings.jl index 1f520aa7d4eaf..38793b4b74186 100644 --- a/test/strings.jl +++ b/test/strings.jl @@ -1603,3 +1603,16 @@ let s = "ba\0d" @test_throws ArgumentError Base.unsafe_convert(Cstring, s) @test_throws ArgumentError Base.unsafe_convert(Cwstring, wstring(s)) end + +# issue # 11389: Vector{UInt32} was copied with UTF32String, unlike Vector{Char} +a = UInt32[48,0] +b = UTF32String(a) +@test b=="0" +a[1] = 65 +@test b=="A" +c = Char['0','\0'] +d = UTF32String(c) +@test d=="0" +c[1] = 'A' +@test d=="A" +