Skip to content

Commit 14250f7

Browse files
KristofferCJeffBezanson
authored andcommitted
use the optimized string map even for AbstractString (#34094)
1 parent 074974a commit 14250f7

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

base/strings/basic.jl

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -565,28 +565,18 @@ isascii(c::Char) = bswap(reinterpret(UInt32, c)) < 0x80
565565
isascii(s::AbstractString) = all(isascii, s)
566566
isascii(c::AbstractChar) = UInt32(c) < 0x80
567567

568-
## string map, filter, has ##
569-
function map(f, s::AbstractString)
570-
out = IOBuffer(sizehint=sizeof(s))
571-
for c in s
572-
c′ = f(c)
573-
isa(c′, AbstractChar) || throw(ArgumentError(
574-
"map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead"))
575-
write(out, c′::AbstractChar)
576-
end
577-
String(take!(out))
578-
end
568+
## string map, filter ##
579569

580-
function map(f, s::String)
570+
function map(f, s::AbstractString)
581571
out = StringVector(max(4, sizeof(s)÷sizeof(codeunit(s))))
582572
index = UInt(1)
583573
for c in s
584574
c′ = f(c)
585-
isa(c′, Char) || throw(ArgumentError(
586-
"map(f, s::String) requires f to return Char; " *
575+
isa(c′, AbstractChar) || throw(ArgumentError(
576+
"map(f, s::AbstractString) requires f to return AbstractChar; " *
587577
"try map(f, collect(s)) or a comprehension instead"))
588578
index + 3 > length(out) && resize!(out, unsigned(2 * length(out)))
589-
index += __unsafe_string!(out, c′, index)
579+
index += __unsafe_string!(out, convert(Char, c′), index)
590580
end
591581
resize!(out, index-1)
592582
sizehint!(out, index-1)

0 commit comments

Comments
 (0)