From 566f8b4e6f980dd280a995b55a2026fff157d3aa Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 30 Jun 2014 15:49:55 -0400 Subject: [PATCH] map() UTF32String to UTF32String, return UTF8String for all non-ASCIIString fixes #7422 --- base/string.jl | 5 ++++- base/utf32.jl | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/base/string.jl b/base/string.jl index eb7e8a520735f..9d4ba6ec0f353 100644 --- a/base/string.jl +++ b/base/string.jl @@ -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) + function map(f::Function, s::String) out = IOBuffer(Array(Uint8,endof(s)),true,true) truncate(out,0) @@ -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) diff --git a/base/utf32.jl b/base/utf32.jl index 1a5aac3ff513c..50bc1d8b0af3f 100644 --- a/base/utf32.jl +++ b/base/utf32.jl @@ -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