Skip to content

Instantly share code, notes, and snippets.

@nicolas-grekas
Last active March 5, 2020 21:10
Show Gist options
  • Select an option

  • Save nicolas-grekas/7d210d0f2559f085548f1591dd11c66a to your computer and use it in GitHub Desktop.

Select an option

Save nicolas-grekas/7d210d0f2559f085548f1591dd11c66a to your computer and use it in GitHub Desktop.

Revisions

  1. nicolas-grekas revised this gist Mar 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Ulid.php
    Original 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'];
    self::$rand['low'] = self::$rand['high'] = (1<<40) - 1;
    usleep(1000);

    return self::generate($lowercase);
  2. nicolas-grekas revised this gist Mar 5, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Ulid.php
    Original 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);
  3. nicolas-grekas revised this gist Mar 5, 2020. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Ulid.php
    Original 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']) {
    self::$rand['high'] = 0;
    usleep(1000);

    return self::generate($lowercase);
    }
    }

  4. nicolas-grekas revised this gist Mar 5, 2020. 1 changed file with 33 additions and 33 deletions.
    66 changes: 33 additions & 33 deletions Ulid.php
    Original 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');
    }
    }
    <?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');
    }
    }
  5. nicolas-grekas created this gist Mar 5, 2020.
    33 changes: 33 additions & 0 deletions Ulid.php
    Original 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');
    }
    }