Skip to content

Commit

Permalink
Various fixes and removed deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Sep 11, 2024
1 parent 731f6e1 commit d0869f7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
32 changes: 31 additions & 1 deletion src/Iconv/Iconv.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public static function strlen1($s, $encoding = null)
return false;
}

return \strlen(utf8_decode($s));
return \strlen(self::utf8Decode($s));
}

public static function strlen2($s, $encoding = null)
Expand Down Expand Up @@ -741,4 +741,34 @@ private static function getData($file)

return false;
}

private static function utf8Decode($s)
{
$s = (string) $s;
$len = \strlen($s);

for ($i = 0, $j = 0; $i < $len; ++$i, ++$j) {
switch ($s[$i] & "\xF0") {
case "\xC0":
case "\xD0":
$c = (\ord($s[$i] & "\x1F") << 6) | \ord($s[++$i] & "\x3F");
$s[$j] = $c < 256 ? \chr($c) : '?';
break;

case "\xF0":
++$i;
// no break

case "\xE0":
$s[$j] = '?';
$i += 2;
break;

default:
$s[$j] = $s[$i];
}
}

return substr($s, 0, $j);
}
}
4 changes: 2 additions & 2 deletions src/Intl/Idn/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
}

if (!function_exists('idn_to_ascii')) {
function idn_to_ascii(?string $domain, ?int $flags = IDNA_DEFAULT, ?int $variant = INTL_IDNA_VARIANT_UTS46, &$idna_info = null): string|false { return p\Idn::idn_to_ascii((string) $domain, (int) $flags, (int) $variant, $idna_info); }
function idn_to_ascii(string $domain, int $flags = IDNA_DEFAULT, int $variant = INTL_IDNA_VARIANT_UTS46, &$idna_info = null): string|false { return p\Idn::idn_to_ascii($domain, $flags, $variant, $idna_info); }
}
if (!function_exists('idn_to_utf8')) {
function idn_to_utf8(?string $domain, ?int $flags = IDNA_DEFAULT, ?int $variant = INTL_IDNA_VARIANT_UTS46, &$idna_info = null): string|false { return p\Idn::idn_to_utf8((string) $domain, (int) $flags, (int) $variant, $idna_info); }
function idn_to_utf8(string $domain, int $flags = IDNA_DEFAULT, int $variant = INTL_IDNA_VARIANT_UTS46, &$idna_info = null): string|false { return p\Idn::idn_to_utf8($domain, $flags, $variant, $idna_info); }
}
1 change: 0 additions & 1 deletion src/Util/TestListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ function {$f['name']}{$f['signature']}

$map = [
'?' => '',
'IDNA_DEFAULT' => \PHP_VERSION_ID >= 80100 ? 'IDNA_DEFAULT' : '0',
'array|string|null $string' => 'array|string $string',
'array|string|null $from_encoding = null' => 'array|string|null $from_encoding = null',
'array|string|null $from_encoding' => 'array|string $from_encoding',
Expand Down
2 changes: 1 addition & 1 deletion tests/Iconv/IconvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testIconv()
$this->assertFalse(@iconv('UTF-8', 'ISO-8859-1', 'nœud'));
$this->assertSame('nud', iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud'));

$this->assertSame(utf8_decode('déjà'), iconv('CP1252', 'ISO-8859-1', utf8_decode('déjà')));
$this->assertSame(mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8'), iconv('CP1252', 'ISO-8859-1', mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8')));
$this->assertSame('deja noeud', p::iconv('UTF-8//ignore//IGNORE', 'US-ASCII//TRANSLIT//IGNORE//translit', 'déjà nœud'));

$this->assertSame('4', iconv('UTF-8', 'UTF-8', 4));
Expand Down
16 changes: 12 additions & 4 deletions tests/Mbstring/MbstringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ public function testInternalEncodingWithInvalidEncoding()
*/
public function testConvertEncoding()
{
$this->assertSame(utf8_decode('déjà'), mb_convert_encoding('déjà', 'Windows-1252'));
$this->assertSame(mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8'), mb_convert_encoding('déjà', 'Windows-1252'));
$this->assertSame('déjà', mb_convert_encoding(mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8'), 'Utf-8', 'ASCII,ISO-2022-JP,UTF-8,ISO-8859-1'));
$this->assertSame('déjà', mb_convert_encoding(mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8'), 'Utf-8', ['ASCII', 'ISO-2022-JP', 'UTF-8', 'ISO-8859-1']));
}

/**
* @group legacy
*/
public function testConvertLegacyEncoding()
{
// handling base64 and html entities with mb_convert_encoding is deprecated in PHP 8.2
$this->assertSame(base64_encode('déjà'), mb_convert_encoding('déjà', 'Base64'));
$this->assertSame('&#23455;<&>d&eacute;j&agrave;', mb_convert_encoding('実<&>déjà', 'Html-entities'));
$this->assertSame('déjà', mb_convert_encoding(base64_encode('déjà'), 'Utf-8', 'Base64'));
$this->assertSame('déjà', mb_convert_encoding('d&eacute;j&#224;', 'Utf-8', 'Html-entities'));
$this->assertSame('déjà', mb_convert_encoding(utf8_decode('déjà'), 'Utf-8', 'ASCII,ISO-2022-JP,UTF-8,ISO-8859-1'));
$this->assertSame('déjà', mb_convert_encoding(utf8_decode('déjà'), 'Utf-8', ['ASCII', 'ISO-2022-JP', 'UTF-8', 'ISO-8859-1']));
}

/**
Expand Down Expand Up @@ -567,7 +575,7 @@ public function testStrwidth()
{
$this->assertSame(3, mb_strwidth("\000", 'UTF-8'));
$this->assertSame(4, mb_strwidth('déjà', 'UTF-8'));
$this->assertSame(4, mb_strwidth(utf8_decode('déjà'), 'CP1252'));
$this->assertSame(4, mb_strwidth(mb_convert_encoding('déjà', 'ISO-8859-1', 'UTF-8'), 'CP1252'));
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Php81/Php81Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testArrayIsList()

/**
* @requires extension mysqli
* @requires PHP < 8.4
*/
public function testMysqliRefreshReplicaDefined()
{
Expand Down

0 comments on commit d0869f7

Please sign in to comment.