Skip to content

Commit

Permalink
Fix JuliaLang#11389 Add UTF32String constructor for Vector{UInt32}
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed May 22, 2015
1 parent ed61f1e commit 8846118
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions base/utf32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions doc/stdlib/strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 8846118

Please sign in to comment.