|
|
|
@ -27,11 +27,18 @@ class Base62
|
|
|
|
|
return $chars;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getSpecialChar($char, $default = null)
|
|
|
|
|
{
|
|
|
|
|
if ($default == null) {
|
|
|
|
|
$default = $char;
|
|
|
|
|
}
|
|
|
|
|
return $char == 'i' ? "ia" : ($char == '+' ? "ib" : ($char == '/' ? "ic" : $default));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function encode($string)
|
|
|
|
|
{
|
|
|
|
|
$encodes = self::getCharArray(self::ENCODES);
|
|
|
|
|
$bytes = self::stringToBytes($string);
|
|
|
|
|
|
|
|
|
|
$pos = 0;
|
|
|
|
|
$val = 0;
|
|
|
|
|
$result = [];
|
|
|
|
@ -40,21 +47,14 @@ class Base62
|
|
|
|
|
$pos += 8;
|
|
|
|
|
while ($pos > 5) {
|
|
|
|
|
$char = $encodes[$val >> ($pos -= 6)];
|
|
|
|
|
$result[] = (
|
|
|
|
|
$char == 'i' ? "ia" :
|
|
|
|
|
$char == '+' ? "ib" :
|
|
|
|
|
$char == '/' ? "ic" : $char
|
|
|
|
|
);
|
|
|
|
|
$encodeChar = self::getSpecialChar($char);
|
|
|
|
|
$result[] = $encodeChar;
|
|
|
|
|
$val &= ((1 << $pos) - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($pos > 0) {
|
|
|
|
|
$char = $encodes[$val << (6 - $pos)];
|
|
|
|
|
$result[] = (
|
|
|
|
|
$char == 'i' ? "ia" :
|
|
|
|
|
$char == '+' ? "ib" :
|
|
|
|
|
$char == '/' ? "ic" : $char
|
|
|
|
|
);
|
|
|
|
|
$result[] = self::getSpecialChar($char);
|
|
|
|
|
}
|
|
|
|
|
return implode('', $result);
|
|
|
|
|
}
|
|
|
|
@ -70,10 +70,10 @@ class Base62
|
|
|
|
|
$char = $chars[$i];
|
|
|
|
|
if ($char == 'i') {
|
|
|
|
|
$char = $chars[++$i];
|
|
|
|
|
$char =
|
|
|
|
|
$char =
|
|
|
|
|
$char == 'a' ? 'i' :
|
|
|
|
|
$char == 'b' ? '+' :
|
|
|
|
|
$char == 'c' ? '/' : $chars[--$i];
|
|
|
|
|
($char == 'b' ? '+' :
|
|
|
|
|
($char == 'c' ? '/' : $chars[--$i]));
|
|
|
|
|
}
|
|
|
|
|
$val = ($val << 6) | $decodes[$char];
|
|
|
|
|
$pos += 6;
|
|
|
|
|