Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add repeat for Char to fix performance of pow #22797

Merged
merged 3 commits into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use Julia syntax repeat(c::Char, r::Integer)::String for this sort of thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, that change should however be made or brought up in another PR/issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find an open issue on this, so I filed one here #22804


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