Skip to content
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

Fix bugs with using pointer on a SubString, add tests of pointer #12639

Merged
merged 2 commits into from Aug 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions base/unicode/utf32.jl
Expand Up @@ -11,7 +11,6 @@ sizeof(s::UTF32String) = sizeof(s.data) - sizeof(Char)

const empty_utf32 = UTF32String(UInt32[0])

utf32(x) = convert(UTF32String, x)
convert(::Type{UTF32String}, c::Char) = UTF32String(Char[c, Char(0)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case someone else is reviewing this and gets confused like I did, this is a duplicate definition

convert(::Type{UTF32String}, s::UTF32String) = s

Expand Down Expand Up @@ -186,8 +185,6 @@ end
convert(::Type{Vector{Char}}, str::UTF32String) = str.data
convert(::Type{Array{Char}}, str::UTF32String) = str.data

reverse(s::UTF32String) = UTF32String(reverse!(copy(s.data), 1, length(s)))

unsafe_convert{T<:Union{Int32,UInt32,Char}}(::Type{Ptr{T}}, s::UTF32String) =
convert(Ptr{T}, pointer(s))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate method removal too


Expand Down Expand Up @@ -272,9 +269,11 @@ end

# pointer conversions of ASCII/UTF8/UTF16/UTF32 strings:
pointer(x::Union{ByteString,UTF16String,UTF32String}) = pointer(x.data)
pointer{T<:ByteString}(x::SubString{T}) = pointer(x.string.data) + x.offset
pointer(x::ByteString, i::Integer) = pointer(x.data)+(i-1)
pointer{T<:ByteString}(x::SubString{T}, i::Integer) = pointer(x.string.data) + x.offset + (i-1)
pointer(x::Union{UTF16String,UTF32String}, i::Integer) = pointer(x)+(i-1)*sizeof(eltype(x.data))
pointer{T<:Union{UTF16String,UTF32String}}(x::SubString{T}) = pointer(x.string.data) + x.offset*sizeof(eltype(x.data))
pointer{T<:Union{UTF16String,UTF32String}}(x::SubString{T}, i::Integer) = pointer(x.string.data) + (x.offset + (i-1))*sizeof(eltype(x.data))

# pointer conversions of SubString of ASCII/UTF8/UTF16/UTF32:
pointer{T<:ByteString}(x::SubString{T}) = pointer(x.string.data) + x.offset
pointer{T<:ByteString}(x::SubString{T}, i::Integer) = pointer(x.string.data) + x.offset + (i-1)
pointer{T<:Union{UTF16String,UTF32String}}(x::SubString{T}) = pointer(x.string.data) + x.offset*sizeof(eltype(x.string.data))
pointer{T<:Union{UTF16String,UTF32String}}(x::SubString{T}, i::Integer) = pointer(x.string.data) + (x.offset + (i-1))*sizeof(eltype(x.string.data))
59 changes: 59 additions & 0 deletions test/unicode/utf32.jl
Expand Up @@ -208,3 +208,62 @@ end

@test isvalid(UTF32String, Char['d','\uff','\u7ff','\u7fff','\U7ffff'])
@test reverse(utf32("abcd \uff\u7ff\u7fff\U7ffff")) == utf32("\U7ffff\u7fff\u7ff\uff dcba")

# Test pointer() functions
let str = "this "
u8 = utf8(str)
u16 = utf16(str)
u32 = utf32(str)
pa = pointer(str)
p8 = pointer(u8)
p16 = pointer(u16)
p32 = pointer(u32)
@test typeof(pa) == Ptr{UInt8}
@test unsafe_load(pa,1) == 0x74
@test typeof(p8) == Ptr{UInt8}
@test unsafe_load(p8,1) == 0x74
@test typeof(p16) == Ptr{UInt16}
@test unsafe_load(p16,1) == 0x0074
@test typeof(p32) == Ptr{Char}
@test unsafe_load(p32,1) == 't'
pa = pointer(str, 2)
p8 = pointer(u8, 2)
p16 = pointer(u16, 2)
p32 = pointer(u32, 2)
@test typeof(pa) == Ptr{UInt8}
@test unsafe_load(pa,1) == 0x68
@test typeof(p8) == Ptr{UInt8}
@test unsafe_load(p8,1) == 0x68
@test typeof(p16) == Ptr{UInt16}
@test unsafe_load(p16,1) == 0x0068
@test typeof(p32) == Ptr{Char}
@test unsafe_load(p32,1) == 'h'
sa = SubString{ASCIIString}(str, 3, 5)
s8 = SubString{UTF8String}(u8, 3, 5)
s16 = SubString{UTF16String}(u16, 3, 5)
s32 = SubString{UTF32String}(u32, 3, 5)
pa = pointer(sa)
p8 = pointer(s8)
p16 = pointer(s16)
p32 = pointer(s32)
@test typeof(pa) == Ptr{UInt8}
@test unsafe_load(pa,1) == 0x69
@test typeof(p8) == Ptr{UInt8}
@test unsafe_load(p8,1) == 0x69
@test typeof(p16) == Ptr{UInt16}
@test unsafe_load(p16,1) == 0x0069
@test typeof(p32) == Ptr{Char}
@test unsafe_load(p32,1) == 'i'
pa = pointer(sa, 2)
p8 = pointer(s8, 2)
p16 = pointer(s16, 2)
p32 = pointer(s32, 2)
@test typeof(pa) == Ptr{UInt8}
@test unsafe_load(pa,1) == 0x73
@test typeof(p8) == Ptr{UInt8}
@test unsafe_load(p8,1) == 0x73
@test typeof(p16) == Ptr{UInt16}
@test unsafe_load(p16,1) == 0x0073
@test typeof(p32) == Ptr{Char}
@test unsafe_load(p32,1) == 's'
end