Skip to content

Commit

Permalink
Fix #11464 map on a UTF16String should return a UTF16String
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jun 17, 2015
1 parent dbe94d1 commit 4ad0aa0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions base/utf16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,24 @@ function utf16(p::Union{Ptr{UInt16}, Ptr{Int16}})
while unsafe_load(p, len+1) != 0; len += 1; end
utf16(p, len)
end

function map(fun, str::UTF16String)
buf = UInt16[]
sizehint!(buf, length(str.data))
for ch in str
c2 = fun(ch)
!isa(c2, Char) &&
throw(UnicodeError(UTF_ERR_MAP_CHAR, 0, 0))
uc = reinterpret(UInt32, c2)
if uc < 0x10000
push!(buf, UInt16(uc))
elseif uc <= 0x10ffff
push!(buf, UInt16(0xd7c0 + (uc >> 10)))
push!(buf, UInt16(0xdc00 + (uc & 0x3ff)))
else
throw(UnicodeError(UTF_ERR_INVALID_CHAR, 0, uc))
end
end
push!(buf, 0)
UTF16String(buf)
end
10 changes: 10 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,13 @@ d = UTF32String(c)
c[1] = 'A'
@test d=="A"

# issue # 11464: uppercase/lowercase of UTF16String becomes a UTF8String

@test typeof(uppercase("abcdef")) == ASCIIString
@test typeof(uppercase(utf8("abcdef"))) == UTF8String
@test typeof(uppercase(utf16("abcdef"))) == UTF16String
@test typeof(uppercase(utf32("abcdef"))) == UTF32String
@test typeof(lowercase("ABCDEF")) == ASCIIString
@test typeof(lowercase(utf8("ABCDEF"))) == UTF8String
@test typeof(lowercase(utf16("ABCDEF"))) == UTF16String
@test typeof(lowercase(utf32("ABCDEF"))) == UTF32String

0 comments on commit 4ad0aa0

Please sign in to comment.