-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Conversation
base/strings/string.jl
Outdated
""" | ||
repeat(s::String, r::Integer) | ||
|
||
Repeat a string `r` times. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
little odd that repeat is currently in the linalg doc index, maybe should be moved to more general collections section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have it in both the linalg and string indices, with AbstractArray
and AbstractString
signatures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I'm not sure why anyone would ever call repeat
for a string rather than using ^
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PR please check if this now looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the multiple docstrings would need to be distinguished by signature otherwise they will all be repeated in both sections
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tkelman thanks will update. This means I should include:
Base.repeat(::AbstractString, ::Integer)
Base.repeat(::Char, ::Integer)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well those two docstrings would probably be fine to list together since they're closely related - it's the array one that should be in a different section
@@ -438,3 +449,25 @@ function repeat(s::String, r::Integer) | |||
end | |||
return out | |||
end | |||
|
|||
""" | |||
repeat(c::Char, r::Integer) -> String |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
base/strings/string.jl
Outdated
``` | ||
""" | ||
function repeat(c::Char, r::Integer) | ||
r < 0 && throw(ArgumentError("can't repeat a character $r times")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the r < 0
check inside the isascii
check, since repeat(string(c), r)
also does this check; otherwise you are doing the same check twice for non-ascii chars.
test/strings/basic.jl
Outdated
@test repeat("xx",3) == repeat("x",6) == "xxxxxx" | ||
@test repeat("αα",3) == repeat("α",6) == "αααααα" | ||
@test repeat("xx",3) == repeat("x",6) == repeat('x',6) == "xxxxxx" | ||
@test repeat("αα",3) == repeat("α",6) == repeat('α',6) == "αααααα" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also add a regression test for ^
to make sure 'x'^0 == ""
and 'x'^1 == "x"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks done.
base/strings/types.jl
Outdated
|
||
Repeat `n` times the string `s`. | ||
Repeat `n` times the string or character `s`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost reads like "repeat n * string". Perhaps:
Repeat the string or character `s` `n` times.
Interestingly the performance characteristics of |
test/strings/basic.jl
Outdated
@test repeat("x",1) == repeat('x',1) == "x" | ||
@test repeat("x",0) == repeat('x',0) == "" | ||
@test "x"^1 == 'x'^1 == "x" | ||
@test "x"^0 == 'x'^0 == "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably merge these two tests with the previous lines, e.g. @test "x"^1 == 'x'^1 == repeat("x",1) == repeat('x',1) == "x"
Updated to also test |
base/strings/types.jl
Outdated
""" | ||
repeat(s::AbstractString, r::Integer) | ||
|
||
Repeat a string `r` times. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the docstring should also cross-reference ^
; similarly for Char
.
base/strings/types.jl
Outdated
@@ -155,7 +155,7 @@ reverseind(s::SubString{String}, i::Integer) = | |||
""" | |||
repeat(s::AbstractString, r::Integer) | |||
|
|||
Repeat a string `r` times. | |||
Repeat a string `r` times. This can equivalently be accomplished by calling [`^`](@ref). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be clearer to write:
by calling [`s^r`](@ref ^).
Ideally, we should cross-reference ^(::AbstractString, ::Integer)
... @MichaelHatherly, does @ref ^(::AbstractString, ::Integer)
work in Documenter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.
(Travis failure is #17626. Not sure what the |
I think there is a bug in the implementation here
|
@musm That's because |
@TotalVerb oh right, thanks! I added a new commit which removed the |
Good to go then? |
yes (please squash) |
* Add repeat for Char to fix performance of pow * Address reviews * Update docstring
close #22785
But more importantly fix the bug for
::Char ^ 0
and::Char^1
As a future optimization someone may want to improve the nonascii case to remove the allocation for the string conversion.