-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
immutable UTF16String <: String | ||
data::Array{Uint16,1} | ||
end | ||
|
||
utf16_is_lead(c::Uint16) = (c & 0xfc00) == 0xd800 | ||
utf16_is_trail(c::Uint16) = (c & 0xfc00) == 0xdc00 | ||
utf16_is_surrogate(c::Uint16) = (c & 0xf800) == 0xd800 | ||
utf16_get_supplementary(lead::Uint16, trail::Uint16) = char((lead-0xd7f7)<<10 + trail) | ||
|
||
function endof(s::UTF16String) | ||
d = s.data | ||
i = length(d) | ||
i == 0 && return i | ||
utf16_is_surrogate(d[i]) ? i-1 : i | ||
end | ||
|
||
function next(s::UTF16String, i::Int) | ||
if !utf16_is_surrogate(s.data[i]) | ||
return char(s.data[i]), i+1 | ||
elseif length(s.data) > i && utf16_is_lead(s.data[i]) && utf16_is_trail(s.data[i+1]) | ||
return utf16_get_supplementary(s.data[i], s.data[i+1]), i+2 | ||
end | ||
error("invalid UTF-16 character index") | ||
end | ||
|
||
# TODO: optmize this | ||
function encode16(s::String) | ||
buf = Uint16[] | ||
for c in s | ||
if c < 0x10000 | ||
push!(buf, uint16(c)) | ||
else | ||
push!(buf, uint16(0xd7c0 + (c>>10) & 0x3ff)) | ||
push!(buf, uint16(0xdc00 + c & 0x3ff)) | ||
end | ||
end | ||
UTF16String(buf) | ||
end | ||
|
||
utf16(x) = convert(UTF16String, x) | ||
convert(::Type{UTF16String}, s::UTF16String) = s | ||
convert(::Type{UTF16String}, s::String) = encode16(s) | ||
convert(::Type{UTF8String}, s::UTF16String) = | ||
sprint(length(s.data), io->for c in s; write(io,c::Char); end) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters