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 random.sampleBuffer #16957

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bfaf7b6
ReSync with Devel
juancarlospaco Sep 2, 2020
914b642
ReSync devel
juancarlospaco Oct 25, 2020
13064ce
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Oct 26, 2020
76d1123
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Oct 26, 2020
04caa74
ReSync
juancarlospaco Oct 26, 2020
e4d1dce
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Oct 29, 2020
a899cc1
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Nov 3, 2020
d083701
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Nov 20, 2020
f66517f
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Dec 2, 2020
2ec956e
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 15, 2021
8684049
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 16, 2021
2ef8ada
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 18, 2021
c8c07e3
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 21, 2021
1216a03
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 22, 2021
3d391b7
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 22, 2021
3d6a30b
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 24, 2021
f6374a7
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 25, 2021
9977e37
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 29, 2021
35337e5
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 30, 2021
c4d67bb
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 30, 2021
50a0292
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 31, 2021
6161bcc
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Jan 31, 2021
b00a1c9
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Feb 3, 2021
840e77a
Merge branch 'devel' of https://github.com/nim-lang/Nim into devel
juancarlospaco Feb 6, 2021
9654dd4
Add random.randToken
juancarlospaco Feb 6, 2021
838e055
Add random.randToken
juancarlospaco Feb 6, 2021
cce02df
IUpdate changelog.md
juancarlospaco Feb 7, 2021
8531b98
Update lib/pure/random.nim
juancarlospaco Feb 7, 2021
e606667
Update lib/pure/random.nim
juancarlospaco Feb 7, 2021
35eb74a
Merge branch 'devel' of https://github.com/nim-lang/Nim into rando
juancarlospaco Feb 7, 2021
ee54a06
Add random.randToken
juancarlospaco Feb 7, 2021
304fc6b
Add back at least 1 example that do NOT use sugars
juancarlospaco Feb 7, 2021
e7557d7
Add back at least 1 example that do NOT use sugars
juancarlospaco Feb 7, 2021
1e30082
https://github.com/nim-lang/Nim/pull/16957#issuecomment-774728750
juancarlospaco Feb 7, 2021
d0ad5ad
https://github.com/nim-lang/Nim/pull/16957#issuecomment-774728750
juancarlospaco Feb 7, 2021
ceaef79
https://github.com/nim-lang/Nim/pull/16957#discussion_r571748103
juancarlospaco Feb 8, 2021
959c3c0
https://github.com/nim-lang/Nim/pull/16957#discussion_r571748103
juancarlospaco Feb 8, 2021
5e78d8c
https://github.com/nim-lang/Nim/pull/16957#discussion_r571748103
juancarlospaco Feb 8, 2021
09c8e46
More examples
juancarlospaco Feb 8, 2021
56b2947
More examples
juancarlospaco Feb 8, 2021
287d220
Update changelog.md
juancarlospaco Feb 8, 2021
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
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior.
(instead of skipping them sometimes as it was before).
- Added optional `followSymlinks` argument to `setFilePermissions`.

- Added `random.sampleBuffer` to in-place populate a `buffer` with random samples from `alphabet` arguments.
Can be used to generate random ASCII URL-Safe strings of specified length and chars.


## Language changes

- `nimscript` now handles `except Exception as e`.
Expand Down
15 changes: 15 additions & 0 deletions lib/pure/random.nim
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,19 @@ when not defined(nimscript) and not defined(standalone):
let now = times.getTime()
randomize(convert(Seconds, Nanoseconds, now.toUnix) + now.nanosecond)

proc sampleBuffer*[T](r: var Rand, buffer: var openArray[T]; alphabet: set[T]) {.inline, since: (1, 5).} =
## Populates `buffer` with random samples from `alphabet`.
## Can be used to generate random ASCII URL-Safe strings of specified length and chars.
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
# Inspired but not copied from Python 3.10 "secrets.token_urlsafe()".
timotheecour marked this conversation as resolved.
Show resolved Hide resolved
runnableExamples:
import std/[sugar, strutils] ## sugar.dup, strutils.UrlSafeChars
var r = initRand(666)
var token = "12345678" ## Length is 8.
sampleBuffer(r, token, UrlSafeChars) ## Use sugar.dup for out-place.
doAssert token != "12345678" and token.len == 8 ## Random ASCII URL-Safe string.
doAssert newString(8).dup(sampleBuffer(r, _, {'0'..'9'})).len == 8
doAssert newString(8).dup(sampleBuffer(r, _, UrlSafeChars)).len == 8
for c in buffer.mitems: c = sample(r, alphabet)


{.pop.}
3 changes: 3 additions & 0 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ const
## doAssert "01234".find(invalid) == -1
## doAssert "01A34".find(invalid) == 2

UrlSafeChars* = {'A'..'Z', 'a'..'z', '0'..'9', '-', '_'}
## ASCII URL-Safe `char` alphabet representation, compatible with Base64 URL-Safe.

func isAlphaAscii*(c: char): bool {.rtl, extern: "nsuIsAlphaAsciiChar".} =
## Checks whether or not character `c` is alphabetical.
##
Expand Down