mirror of
https://github.com/processwire/processwire.git
synced 2025-08-12 01:34:31 +02:00
Update Punycode class to not require PHP's mb_string
This commit is contained in:
@@ -71,6 +71,14 @@ class Punycode {
|
|||||||
*/
|
*/
|
||||||
protected $encoding;
|
protected $encoding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PHP mb string functions supported?
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected $mb = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
@@ -78,7 +86,12 @@ class Punycode {
|
|||||||
*/
|
*/
|
||||||
public function __construct($encoding = 'UTF-8') {
|
public function __construct($encoding = 'UTF-8') {
|
||||||
$this->encoding = $encoding;
|
$this->encoding = $encoding;
|
||||||
|
$this->mb = function_exists("mb_internal_encoding");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function strtolower($str) { return $this->mb ? mb_strtolower($str, $this->encoding) : strtolower($str); }
|
||||||
|
public function strlen($str) { return $this->mb ? mb_strlen($str, $this->encoding) : strlen($str); }
|
||||||
|
public function substr($str, $a, $b) { return $this->mb ? mb_substr($str, $a, $b, $this->encoding) : substr($str, $a, $b); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode a domain to its Punycode version
|
* Encode a domain to its Punycode version
|
||||||
@@ -88,7 +101,7 @@ class Punycode {
|
|||||||
* @return string Punycode representation in ASCII
|
* @return string Punycode representation in ASCII
|
||||||
*/
|
*/
|
||||||
public function encode($input) {
|
public function encode($input) {
|
||||||
$input = mb_strtolower($input, $this->encoding);
|
$input = $this->strtolower($input);
|
||||||
$parts = explode('.', $input);
|
$parts = explode('.', $input);
|
||||||
foreach($parts as &$part) {
|
foreach($parts as &$part) {
|
||||||
$part = $this->encodePart($part);
|
$part = $this->encodePart($part);
|
||||||
@@ -122,7 +135,7 @@ class Punycode {
|
|||||||
$codePoints['nonBasic'] = array_unique($codePoints['nonBasic']);
|
$codePoints['nonBasic'] = array_unique($codePoints['nonBasic']);
|
||||||
sort($codePoints['nonBasic']);
|
sort($codePoints['nonBasic']);
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$length = mb_strlen($input, $this->encoding);
|
$length = $this->strlen($input);
|
||||||
while($h < $length) {
|
while($h < $length) {
|
||||||
$m = $codePoints['nonBasic'][$i++];
|
$m = $codePoints['nonBasic'][$i++];
|
||||||
$delta = $delta + ($m - $n) * ($h + 1);
|
$delta = $delta + ($m - $n) * ($h + 1);
|
||||||
@@ -209,9 +222,9 @@ class Punycode {
|
|||||||
$bias = $this->adapt($i - $oldi, ++$outputLength, ($oldi === 0));
|
$bias = $this->adapt($i - $oldi, ++$outputLength, ($oldi === 0));
|
||||||
$n = $n + (int) ($i / $outputLength);
|
$n = $n + (int) ($i / $outputLength);
|
||||||
$i = $i % ($outputLength);
|
$i = $i % ($outputLength);
|
||||||
$output = mb_substr($output, 0, $i, $this->encoding) .
|
$output = $this->substr($output, 0, $i) .
|
||||||
$this->codePointToChar($n) .
|
$this->codePointToChar($n) .
|
||||||
mb_substr($output, $i, $outputLength - 1, $this->encoding);
|
$this->substr($output, $i, $outputLength - 1);
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
return $output;
|
return $output;
|
||||||
@@ -272,9 +285,9 @@ class Punycode {
|
|||||||
'basic' => array(),
|
'basic' => array(),
|
||||||
'nonBasic' => array(),
|
'nonBasic' => array(),
|
||||||
);
|
);
|
||||||
$length = mb_strlen($input, $this->encoding);
|
$length = $this->strlen($input);
|
||||||
for($i = 0; $i < $length; $i++) {
|
for($i = 0; $i < $length; $i++) {
|
||||||
$char = mb_substr($input, $i, 1, $this->encoding);
|
$char = $this->substr($input, $i, 1);
|
||||||
$code = $this->charToCodePoint($char);
|
$code = $this->charToCodePoint($char);
|
||||||
if($code < 128) {
|
if($code < 128) {
|
||||||
$codePoints['all'][] = $codePoints['basic'][] = $code;
|
$codePoints['all'][] = $codePoints['basic'][] = $code;
|
||||||
@@ -323,4 +336,4 @@ class Punycode {
|
|||||||
return chr(($code >> 18) + 240) . chr((($code >> 12) & 63) + 128) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128);
|
return chr(($code >> 18) + 240) . chr((($code >> 12) & 63) + 128) . chr((($code >> 6) & 63) + 128) . chr(($code & 63) + 128);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user