Skip to content

Commit

Permalink
optimize repeat(string, n) for repeating single ASCII chars (#22462)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj authored and KristofferC committed Jun 24, 2017
1 parent bef5811 commit b1c2d73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,12 @@ function repeat(s::String, r::Integer)
r < 0 && throw(ArgumentError("can't repeat a string $r times"))
n = s.len
out = _string_n(n*r)
for i=1:r
unsafe_copy!(pointer(out, 1+(i-1)*n), pointer(s), n)
if n == 1 # common case: repeating a single ASCII char
ccall(:memset, Ptr{Void}, (Ptr{UInt8}, Cint, Csize_t), out, unsafe_load(pointer(s)), r)
else
for i=1:r
unsafe_copy!(pointer(out, 1+(i-1)*n), pointer(s), n)
end
end
return out
end
4 changes: 4 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ Base.endof(x::CharStr) = endof(x.chars)
@test cmp("\U1f596\U1f596", CharStr("\U1f596")) == 1 # Gives BoundsError with bug
@test cmp(CharStr("\U1f596"), "\U1f596\U1f596") == -1

# repeat function
@test repeat("xx",3) == repeat("x",6) == "xxxxxx"
@test repeat("αα",3) == repeat("α",6) == "αααααα"

# issue #12495: check that logical indexing attempt raises ArgumentError
@test_throws ArgumentError "abc"[[true, false, true]]
@test_throws ArgumentError "abc"[BitArray([true, false, true])]

0 comments on commit b1c2d73

Please sign in to comment.