-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
automatically NULL-terminate UTF16 data for passing to Windows APIs #7016
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10d7ac0
automatically NULL-terminate UTF16 data for passing to Windows (and s…
stevengj a4a847c
NULL-terminate UTF32String data for consistency; deprecate UTF32Strin…
stevengj e96719a
NEWS updates for UTF16String and UTF32String
stevengj 11a0324
added utfXX(ptr[,len]) functions analogous to bytestring, added WStri…
stevengj e88192e
fix utf32/UTF32String mixup
stevengj e7f3fd6
add deprecation for UTF32String(s::String)
stevengj b24d29f
avoid deprecation warning in converting UTFxxString to equal-sized in…
stevengj 249d76d
type typo (and regression test) in utfXX(::Ptr{IntXX})
stevengj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,90 @@ | ||
## 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 | ||
|
||
function UTF32String(a::Array{Char,1}) | ||
if length(a) < 1 || a[end] != 0 | ||
throw(ArgumentError("UTF32String data must be NULL-terminated")) | ||
end | ||
new(a) | ||
end | ||
end | ||
|
||
next(s::UTF32String, i::Int) = (s.data[i], i+1) | ||
endof(s::UTF32String) = length(s.data) - 1 | ||
length(s::UTF32String) = length(s.data) - 1 | ||
|
||
function utf32(c::Integer...) | ||
a = Array(Char, length(c) + 1) | ||
for i = 1:length(c) | ||
a[i] = c[i] | ||
end | ||
a[end] = 0 | ||
UTF32String(a) | ||
end | ||
|
||
utf32(x) = convert(UTF32String, x) | ||
convert(::Type{UTF32String}, s::UTF32String) = s | ||
|
||
function convert(::Type{UTF32String}, s::String) | ||
a = Array(Char, length(s) + 1) | ||
i = 0 | ||
for c in s | ||
a[i += 1] = c | ||
end | ||
a[end] = 0 # NULL terminate | ||
UTF32String(a) | ||
end | ||
|
||
function convert(::Type{UTF32String}, data::Vector{Char}) | ||
len = length(data) | ||
d = Array(Char, len + 1) | ||
d[end] = 0 # NULL terminate | ||
UTF32String(copy!(d,1, data,1, len)) | ||
end | ||
|
||
convert{T<:Union(Int32,Uint32)}(::Type{UTF32String}, data::Vector{T}) = | ||
convert(UTF32String, reinterpret(Char, data)) | ||
|
||
convert{T<:String}(::Type{T}, v::Vector{Char}) = convert(T, UTF32String(v)) | ||
|
||
# specialize for performance reasons: | ||
function convert{T<:ByteString}(::Type{T}, data::Vector{Char}) | ||
s = IOBuffer(Array(Uint8,length(data)), true, true) | ||
truncate(s,0) | ||
for x in data | ||
print(s, x) | ||
end | ||
convert(T, takebuf_string(s)) | ||
end | ||
|
||
convert(::Type{Array{Char,1}}, s::UTF32String) = s.data | ||
convert(::Type{Array{Char}}, s::UTF32String) = s.data | ||
|
||
reverse(s::UTF32String) = UTF32String(reverse!(copy(s.data), 1, length(s))) | ||
|
||
sizeof(s::UTF32String) = sizeof(s.data) - sizeof(Char) | ||
convert{T<:Union(Int32,Uint32,Char)}(::Type{Ptr{T}}, s::UTF32String) = | ||
convert(Ptr{T}, s.data) | ||
|
||
function convert(T::Type{UTF32String}, bytes::Array{Uint8}) | ||
isempty(bytes) && return UTF32String(Char[0]) | ||
length(bytes) & 3 != 0 && throw(ArgumentError("need multiple of 4 bytes")) | ||
data = reinterpret(Char, bytes) | ||
# check for byte-order mark (BOM): | ||
if data[1] == 0x0000feff # native byte order | ||
d = Array(Char, length(data)) | ||
copy!(d,1, data,2, length(data)-1) | ||
elseif data[1] == 0xfffe0000 # byte-swapped | ||
d = Array(Char, length(data)) | ||
for i = 2:length(data) | ||
d[i-1] = bswap(data[i]) | ||
end | ||
else | ||
d = Array(Char, length(data) + 1) | ||
copy!(d,1, data,1, length(data)) # assume native byte order | ||
end | ||
d[end] = 0 # NULL terminate | ||
UTF32String(d) | ||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean a
Uint16
array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, will fix.