Skip to content

Commit

Permalink
Optimize Str random methods by just using strlen() and substr() (#15112)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlakoff authored and taylorotwell committed Aug 29, 2016
1 parent db580b5 commit ba7a37f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,12 @@ public static function random($length = 16)
{
$string = '';

while (($len = static::length($string)) < $length) {
while (($len = strlen($string)) < $length) {
$size = $length - $len;

$bytes = static::randomBytes($size);

$string .= static::substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
}

return $string;
Expand Down Expand Up @@ -255,7 +255,7 @@ public static function quickRandom($length = 16)
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

return static::substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
}

/**
Expand Down

0 comments on commit ba7a37f

Please sign in to comment.