Skip to content

Commit

Permalink
map() UTF32String to UTF32String, return UTF8String for all non-ASCII…
Browse files Browse the repository at this point in the history
…String

fixes #7422
  • Loading branch information
JeffBezanson committed Jun 30, 2014
1 parent ad64c83 commit 566f8b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ end

## string map, filter, has ##

map_result(s::String, a::Vector{Uint8}) = UTF8String(a)
map_result(s::Union(ASCIIString,SubString{ASCIIString}), a::Vector{Uint8}) = bytestring(a)

This comment has been minimized.

Copy link
@stevengj

stevengj Jul 1, 2014

Member

This doesn't seem type-stable, since f may or may not map ASCII to non-ASCII characters. Isn't the only thing that you can do is to just return UTF8String always?


function map(f::Function, s::String)
out = IOBuffer(Array(Uint8,endof(s)),true,true)
truncate(out,0)
Expand All @@ -800,7 +803,7 @@ function map(f::Function, s::String)
end
write(out, c2::Char)
end
takebuf_string(out)
map_result(s, takebuf_array(out))
end

function filter(f::Function, s::String)
Expand Down
15 changes: 15 additions & 0 deletions base/utf32.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@ function utf32(p::Union(Ptr{Char}, Ptr{Uint32}, Ptr{Int32}))
while unsafe_load(p, len+1) != 0; len += 1; end
utf32(p, len)
end

function map(f::Function, s::UTF32String)
d = s.data
out = similar(d)
out[end] = 0

for i = 1:(length(d)-1)
c2 = f(d[i])
if !isa(c2,Char)
error("map(f,s::String) requires f to return Char; try map(f,collect(s)) or a comprehension instead")
end
out[i] = (c2::Char)
end
UTF32String(out)
end

0 comments on commit 566f8b4

Please sign in to comment.