-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add Randomizer::getBytesFromString()
method
#9664
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
ext/random/tests/03_randomizer/methods/getBytesFromString.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--TEST-- | ||
Random: Randomizer: getBytesFromString(): Basic functionality | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine; | ||
use Random\Engine\Mt19937; | ||
use Random\Engine\PcgOneseq128XslRr64; | ||
use Random\Engine\Secure; | ||
use Random\Engine\Test\TestShaEngine; | ||
use Random\Engine\Xoshiro256StarStar; | ||
use Random\Randomizer; | ||
|
||
require __DIR__ . "/../../engines.inc"; | ||
|
||
$engines = []; | ||
$engines[] = new Mt19937(null, MT_RAND_MT19937); | ||
$engines[] = new Mt19937(null, MT_RAND_PHP); | ||
$engines[] = new PcgOneseq128XslRr64(); | ||
$engines[] = new Xoshiro256StarStar(); | ||
$engines[] = new Secure(); | ||
$engines[] = new TestShaEngine(); | ||
|
||
foreach ($engines as $engine) { | ||
echo $engine::class, PHP_EOL; | ||
|
||
$randomizer = new Randomizer($engine); | ||
var_dump($randomizer->getBytesFromString('a', 10)); | ||
var_dump($randomizer->getBytesFromString(str_repeat('a', 256), 5)); | ||
|
||
for ($i = 1; $i < 250; $i++) { | ||
$output = $randomizer->getBytesFromString(str_repeat('ab', $i), 500); | ||
|
||
// This check can theoretically fail with a chance of 0.5**500. | ||
if (!str_contains($output, 'a') || !str_contains($output, 'b')) { | ||
die("failure: didn't see both a and b at {$i}"); | ||
} | ||
} | ||
} | ||
|
||
die('success'); | ||
|
||
?> | ||
--EXPECT-- | ||
Random\Engine\Mt19937 | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
Random\Engine\Mt19937 | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
Random\Engine\PcgOneseq128XslRr64 | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
Random\Engine\Xoshiro256StarStar | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
Random\Engine\Secure | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
Random\Engine\Test\TestShaEngine | ||
string(10) "aaaaaaaaaa" | ||
string(5) "aaaaa" | ||
success |
28 changes: 28 additions & 0 deletions
28
ext/random/tests/03_randomizer/methods/getBytesFromString_error.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Random: Randomizer: getBytesFromString(): Parameters are correctly validated | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Randomizer; | ||
|
||
function randomizer(): Randomizer | ||
{ | ||
return new Randomizer(); | ||
} | ||
|
||
try { | ||
var_dump(randomizer()->getBytesFromString("", 2)); | ||
} catch (ValueError $e) { | ||
echo $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
try { | ||
var_dump(randomizer()->getBytesFromString("abc", 0)); | ||
} catch (ValueError $e) { | ||
echo $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Random\Randomizer::getBytesFromString(): Argument #1 ($string) cannot be empty | ||
Random\Randomizer::getBytesFromString(): Argument #2 ($length) must be greater than 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hmm, this isn't particularly performant. I wonder whether we should optimize that using
__builtin_clz
(and_BitScanReverse
on Windows). We already have a few usages in php-src (see e.g.zend_mm_small_size_to_bit()
), and it may make sense to unify these (to better encapsulate the usage of the intrinsics).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.
@cmb69 I noted the same at the end of my previous comment here: #9664 (comment).
While the current if-elseif-else-construction contains several hard-to-predict branches, the performance of it is likely to be dwarfed by the loop that actually selects the random bytes.
I'd likely optimize this in a separate PR when the need arises or when there is a good API for clz.
zend_ulong_nlz
is not great, because it takeszend_ulong
which size differs depending on the platform.