Skip to content

Commit

Permalink
Add repeat for Char to fix performance of pow (JuliaLang#22797)
Browse files Browse the repository at this point in the history
* Add repeat for Char to fix performance of pow

* Address reviews

* Update docstring
  • Loading branch information
musm authored and jeffwong committed Jul 24, 2017
1 parent 11547b0 commit 67f93bd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
22 changes: 22 additions & 0 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,25 @@ function repeat(s::String, r::Integer)
end
return out
end

"""
repeat(c::Char, r::Integer) -> String
Repeat a character `r` times. This can equivalently be accomplished by calling [`c^r`](@ref ^).
# Examples
```jldoctest
julia> repeat('A', 3)
"AAA"
```
"""
function repeat(c::Char, r::Integer)
if isascii(c)
r < 0 && throw(ArgumentError("can't repeat a character $r times"))
out = _string_n(r)
ccall(:memset, Ptr{Void}, (Ptr{UInt8}, Cint, Csize_t), out, c, r)
return out
else
return repeat(string(c), r)
end
end
17 changes: 14 additions & 3 deletions base/strings/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ reverseind(s::RevString, i::Integer) = endof(s) - i + 1
reverseind(s::SubString{String}, i::Integer) =
reverseind(s.string, nextind(s.string, endof(s.string))-s.offset-s.endof+i-1) - s.offset

"""
repeat(s::AbstractString, r::Integer)
Repeat a string `r` times. This can equivalently be accomplished by calling [`s^r`](@ref ^).
# Examples
```jldoctest
julia> repeat("ha", 3)
"hahaha"
```
"""
function repeat(s::AbstractString, r::Integer)
r < 0 ? throw(ArgumentError("can't repeat a string $r times")) :
r == 0 ? "" :
Expand All @@ -160,9 +171,9 @@ function repeat(s::AbstractString, r::Integer)
end

"""
^(s::AbstractString, n::Integer)
^(s::Union{AbstractString,Char}, n::Integer)
Repeat `n` times the string `s`.
Repeat a string or character `n` times.
The [`repeat`](@ref) function is an alias to this operator.
# Examples
Expand All @@ -171,7 +182,7 @@ julia> "Test "^3
"Test Test Test "
```
"""
(^)(s::AbstractString, r::Integer) = repeat(s,r)
(^)(s::Union{AbstractString,Char}, r::Integer) = repeat(s,r)

pointer(x::SubString{String}) = pointer(x.string) + x.offset
pointer(x::SubString{String}, i::Integer) = pointer(x.string) + x.offset + (i-1)
1 change: 1 addition & 0 deletions doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Base.cumsum_kbn
Base.crc32c
Base.LinAlg.diff
Base.LinAlg.gradient
Base.repeat(::AbstractArray)
Base.rot180
Base.rotl90
Base.rotr90
Expand Down
1 change: 0 additions & 1 deletion doc/src/stdlib/linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Base.inv(::AbstractMatrix)
Base.LinAlg.pinv
Base.LinAlg.nullspace
Base.repmat
Base.repeat
Base.kron
Base.SparseArrays.blkdiag
Base.LinAlg.linreg
Expand Down
2 changes: 2 additions & 0 deletions doc/src/stdlib/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Base.sizeof(::AbstractString)
Base.:*(::AbstractString, ::Any...)
Base.:^(::AbstractString, ::Integer)
Base.string
Base.repeat(::AbstractString, ::Integer)
Base.repeat(::Char, ::Integer)
Base.repr
Core.String(::AbstractString)
Base.transcode
Expand Down
6 changes: 4 additions & 2 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,10 @@ Base.endof(x::CharStr) = endof(x.chars)
@test cmp(CharStr("\U1f596"), "\U1f596\U1f596") == -1

# repeat function
@test repeat("xx",3) == repeat("x",6) == "xxxxxx"
@test repeat("αα",3) == repeat("α",6) == "αααααα"
@test repeat("xx",3) == repeat("x",6) == repeat('x',6) == repeat(GenericString("x"), 6) == "xxxxxx"
@test repeat("αα",3) == repeat("α",6) == repeat('α',6) == repeat(GenericString("α"), 6) == "αααααα"
@test repeat("x",1) == repeat('x',1) == "x"^1 == 'x'^1 == GenericString("x")^1 == "x"
@test repeat("x",0) == repeat('x',0) == "x"^0 == 'x'^0 == GenericString("x")^0 == ""

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

0 comments on commit 67f93bd

Please sign in to comment.