-
-
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
randstring: allow specifying chars to pick from #22222
Conversation
If |
I don't think so, but this is the point of my "RFC". I would tend to think that the |
base/random.jl
Outdated
""" | ||
randstring([rng=GLOBAL_RNG], [chars::AbstractArray{<:Union{UInt8,Char}}], [len=8]) | ||
|
||
Create a random ASCII string of length `len`, consisting of characters |
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.
is it ASCII if chars
has non-ascii in it?
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.
Ah good catch, indeed not, I will update the doc.
I decided to remove the proposed |
base/random.jl
Outdated
randstring(r::AbstractRNG) = randstring(r,8) | ||
randstring(n::Int) = randstring(GLOBAL_RNG, n) | ||
randstring() = randstring(GLOBAL_RNG) | ||
randstring(r::AbstractRNG, chars::AbstractArray{<:Union{UInt8,Char}}=b, n::Int=8) = |
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.
allowing UInt8 arrays here is a little odd
(and unrelated to this PR, but anyone else think the default length of 8 is kind of arbitrary? was added without much justification in 399c541)
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 choice of Char
and UInt8
is because the String
constructor accepts arrays of those types, and I thougth that String(v::Vector{UInt8})
was taking ownership of v
(according to the docs) so that it would be efficient, but I just did few tests and it seems that this constructor actually copies the data from the vector. Anyway, randstring
is still about twice as fast when chars
is UInt8
data rather than Char
.
(And I also find the default length surprisingly arbitrary, not sure what to do about it though).
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 docs might need updating in that respect with the StringVector representation change, not sure
e85434e
to
1ff58f5
Compare
I updated by not constraining the type of |
base/random.jl
Outdated
`UInt8` (more efficient), provided [`rand`](@ref) can randomly | ||
pick characters from it. | ||
""" | ||
|
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.
no empty line here
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.
Oh right, I add an empty line when justifying with emacs, and often forget to remove it :-/
test/random.jl
Outdated
@test length(randstring(rng..., 20)) == 20 | ||
@test issubset(randstring(rng...), b) | ||
for c = ['a':'z', "qwèrtï", Set(Vector{UInt8}("gcat"))], | ||
len = [8, 20] |
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'd indent this one level further or include it on the same line as the rest of the for, it looks like an assignment rather than a loop variable if you miss the final comma on the previous line
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.
Makes sense.
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.
no, I meant indent this line one level further, not the contents of the block
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.
OK got it, I didn't know this style
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 think this is how the matlab auto indenter would handle line continuations of block openers - one of the few useful things from matlab I miss
cf4a2e1
to
684d897
Compare
Rebased. I will merge in a few days if no objection. |
This reverts commit cdb89ac.
CI failure seems unrelated: it concerns libgit2, and a similar problem appears in other unrelated PRs. |
This allows the API
randstring([rng], [chars], len=8)
, e.g.randstring('a':'z')
will produce a random string of length 8 of lower case letters.I also added an alias as a method ofEDIT: this will be for another time (possibly with a different API).rand
:rand([rng], ::Type{String}, [chars], len=8)
. I'm not sure both ways should exist, hence requesting for comments.