11# This file is a part of Julia. License is MIT: http://julialang.org/license
22
3- immutable UTF16String <: AbstractString
4- data:: Array{UInt16,1} # includes 16-bit NULL termination after string chars
5- function UTF16String (data:: Vector{UInt16} )
6- if length (data) < 1 || data[end ] != 0
7- throw (ArgumentError (" UTF16String data must be NULL-terminated" ))
8- end
9- new (data)
10- end
11- end
12-
133utf16_is_lead (c:: UInt16 ) = (c & 0xfc00 ) == 0xd800
144utf16_is_trail (c:: UInt16 ) = (c & 0xfc00 ) == 0xdc00
155utf16_is_surrogate (c:: UInt16 ) = (c & 0xf800 ) == 0xd800
@@ -39,7 +29,7 @@ function next(s::UTF16String, i::Int)
3929 elseif length (s. data)- 1 > i && utf16_is_lead (s. data[i]) && utf16_is_trail (s. data[i+ 1 ])
4030 return utf16_get_supplementary (s. data[i], s. data[i+ 1 ]), i+ 2
4131 end
42- throw (ArgumentError ( " invalid UTF-16 character index " ))
32+ throw (UnicodeError (UTF_ERR_INVALID_INDEX, 0 , 0 ))
4333end
4434
4535function reverseind (s:: UTF16String , i:: Integer )
@@ -74,7 +64,7 @@ function encode16(s::AbstractString)
7464 push! (buf, UInt16 (0xd7c0 + (c>> 10 )))
7565 push! (buf, UInt16 (0xdc00 + (c & 0x3ff )))
7666 else
77- throw (ArgumentError ( " invalid Unicode character (0x $( hex (c)) > 0x10ffff) " ))
67+ throw (UnicodeError (UTF_ERR_INVALID_CHAR, 0 , ch ))
7868 end
7969 end
8070 push! (buf, 0 ) # NULL termination
@@ -111,7 +101,7 @@ function isvalid(::Type{UTF16String}, data::AbstractArray{UInt16})
111101end
112102
113103function convert (:: Type{UTF16String} , data:: AbstractVector{UInt16} )
114- ! isvalid (UTF16String, data) && throw (ArgumentError ( " invalid UTF16 data " ))
104+ ! isvalid (UTF16String, data) && throw (UnicodeError (UTF_ERR_INVALID_16, 0 , 0 ))
115105 len = length (data)
116106 d = Array (UInt16, len + 1 )
117107 d[end ] = 0 # NULL terminate
@@ -126,7 +116,7 @@ convert(T::Type{UTF16String}, data::AbstractArray{Int16}) =
126116
127117function convert (T:: Type{UTF16String} , bytes:: AbstractArray{UInt8} )
128118 isempty (bytes) && return UTF16String (UInt16[0 ])
129- isodd (length (bytes)) && throw (ArgumentError ( " odd number of bytes" ))
119+ isodd (length (bytes)) && throw (UnicodeError (UTF_ERR_ODD_BYTES_16, length ( bytes), 0 ))
130120 data = reinterpret (UInt16, bytes)
131121 # check for byte-order mark (BOM):
132122 if data[1 ] == 0xfeff # native byte order
@@ -142,7 +132,7 @@ function convert(T::Type{UTF16String}, bytes::AbstractArray{UInt8})
142132 copy! (d,1 , data,1 , length (data)) # assume native byte order
143133 end
144134 d[end ] = 0 # NULL terminate
145- ! isvalid (UTF16String, d) && throw (ArgumentError ( " invalid UTF16 data " ))
135+ ! isvalid (UTF16String, d) && throw (UnicodeError (UTF_ERR_INVALID_16, 0 , 0 ))
146136 UTF16String (d)
147137end
148138
0 commit comments