Last active
March 5, 2020 21:10
-
-
Save nicolas-grekas/7d210d0f2559f085548f1591dd11c66a to your computer and use it in GitHub Desktop.
Revisions
-
nicolas-grekas revised this gist
Mar 5, 2020 . 1 changed file with 1 addition and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public function generate(bool $lowercase = false) self::$rand['low'] = 0; if (1<<40 === ++self::$rand['high']) { self::$rand['low'] = self::$rand['high'] = (1<<40) - 1; usleep(1000); return self::generate($lowercase); -
nicolas-grekas revised this gist
Mar 5, 2020 . 1 changed file with 1 addition and 0 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -20,6 +20,7 @@ public function generate(bool $lowercase = false) self::$rand['low'] = 0; if (1<<40 === ++self::$rand['high']) { self::$rand['low'] = --self::$rand['high']; usleep(1000); return self::generate($lowercase); -
nicolas-grekas revised this gist
Mar 5, 2020 . 1 changed file with 3 additions and 1 deletion.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 charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,9 @@ public function generate(bool $lowercase = false) self::$rand['low'] = 0; if (1<<40 === ++self::$rand['high']) { usleep(1000); return self::generate($lowercase); } } -
nicolas-grekas revised this gist
Mar 5, 2020 . 1 changed file with 33 additions and 33 deletions.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 charactersOriginal file line number Diff line number Diff line change @@ -1,33 +1,33 @@ <?php class Ulid { private static $time = -1; private static $rand = []; public function generate(bool $lowercase = false) { if (\PHP_INT_SIZE !== 8) { throw new \RuntimeException("Ulid::generate() requires a 64-bit PHP runtime." ); } $time = (int) (1000 * microtime(true)); if ($time !== self::$time) { self::$rand = unpack('Jhigh/Jlow', "\0\0\0".substr_replace(random_bytes(10), "\0\0\0", 5, 0)); self::$time = $time; } elseif (1<<40 === ++self::$rand['low']) { self::$rand['low'] = 0; if (1<<40 === ++self::$rand['high']) { self::$rand['high'] = 0; } } return strtr( str_pad(base_convert($time, 10, 32), 10, '0', STR_PAD_LEFT) .str_pad(base_convert(self::$rand['high'], 10, 32), 8, '0', STR_PAD_LEFT) .str_pad(base_convert(self::$rand['low'], 10, 32), 8, '0', STR_PAD_LEFT) , 'abcdefghijklmnopqrstuv', $lowercase ? 'abcdefghjkmnpqrstvwxyz' : 'ABCDEFGHJKMNPQRSTVWXYZ'); } } -
nicolas-grekas created this gist
Mar 5, 2020 .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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ <?php class Ulid { private static $time = -1; private static $rand = []; public function generate(bool $lowercase = false) { if (\PHP_INT_SIZE !== 8) { throw new \RuntimeException("Ulid::generate() requires a 64-bit PHP runtime." ); } $time = (int) (1000 * microtime(true)); if ($time !== self::$time) { self::$rand = unpack('Jhigh/Jlow', "\0\0\0".substr_replace(random_bytes(10), "\0\0\0", 5, 0)); self::$time = $time; } elseif (1<<40 === ++self::$rand['low']) { self::$rand['low'] = 0; if (1<<40 === ++self::$rand['high']) { self::$rand['high'] = 0; } } return strtr( str_pad(base_convert($time, 10, 32), 10, '0', STR_PAD_LEFT) .str_pad(base_convert(self::$rand['high'], 10, 32), 8, '0', STR_PAD_LEFT) .str_pad(base_convert(self::$rand['low'], 10, 32), 8, '0', STR_PAD_LEFT) , 'abcdefghijklmnopqrstuv', $lowercase ? 'abcdefghjkmnpqrstvwxyz' : 'ABCDEFGHJKMNPQRSTVWXYZ'); } }