Skip to content

Commit

Permalink
drop utf8_decode()
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Sep 16, 2024
1 parent 731f6e1 commit a7c4449
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ jobs:
- name: Run tests
run: |
ok=0
./phpunit || ok=1
./phpunit --filter=IconvTest::testIconvStrlen || ok=1
[[ "${{ matrix.mode }}" = experimental ]] || (exit $ok)
8 changes: 7 additions & 1 deletion src/Iconv/Iconv.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,27 +430,33 @@ public static function iconv_mime_encode($fieldName, $fieldValue, $pref = null)

public static function iconv_strlen($s, $encoding = null)
{
var_dump(__METHOD__);
static $hasXml = null;
if (null === $hasXml) {
$hasXml = \extension_loaded('xml');
}

if ($hasXml) {
if ($hasXml && \PHP_VERSION_ID < 80200) {
var_dump(__LINE__);
return self::strlen1($s, $encoding);
}
var_dump(__LINE__);

return self::strlen2($s, $encoding);
}

public static function strlen1($s, $encoding = null)
{
var_dump(__METHOD__);
if (null === $encoding) {
$encoding = self::$internalEncoding;
}
if (0 !== stripos($encoding, 'utf-8') && false === $s = self::iconv($encoding, 'utf-8', $s)) {
return false;
}

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

return \strlen(utf8_decode($s));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Iconv/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function iconv_mime_decode($string, $mode = 0, $encoding = null) { $currentMbEnc
}
} else {
if (!function_exists('iconv_strlen')) {
if (extension_loaded('xml')) {
if (extension_loaded('xml') && \PHP_VERSION_ID < 80200) {
function iconv_strlen($string, $encoding = null) { return p\Iconv::strlen1($string, $encoding); }
} else {
function iconv_strlen($string, $encoding = null) { return p\Iconv::strlen2($string, $encoding); }
Expand Down
8 changes: 7 additions & 1 deletion src/Iconv/bootstrap80.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Symfony\Polyfill\Iconv as p;

var_dump(__FILE__);

if (!defined('ICONV_IMPL')) {
define('ICONV_IMPL', 'Symfony');
}
Expand Down Expand Up @@ -41,6 +43,7 @@ function iconv_mime_decode_headers(?string $headers, ?int $mode = 0, ?string $en
}

if (extension_loaded('mbstring')) {
var_dump(__LINE__);
if (!function_exists('iconv_strlen')) {
function iconv_strlen(?string $string, ?string $encoding = null): int|false { null === $encoding && $encoding = p\Iconv::$internalEncoding; return mb_strlen((string) $string, $encoding); }
}
Expand All @@ -58,9 +61,12 @@ function iconv_mime_decode($string, $mode = 0, $encoding = null) { $currentMbEnc
}
} else {
if (!function_exists('iconv_strlen')) {
if (extension_loaded('xml')) {
var_dump(__LINE__);
if (extension_loaded('xml') && \PHP_VERSION_ID < 80200) {
var_dump(__LINE__);
function iconv_strlen(?string $string, ?string $encoding = null): int|false { return p\Iconv::strlen1((string) $string, $encoding); }
} else {
var_dump(__LINE__);
function iconv_strlen(?string $string, ?string $encoding = null): int|false { return p\Iconv::strlen2((string) $string, $encoding); }
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Util/TestListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public function startTestSuite($mainSuite)
$defLine = sprintf("throw new \\%s('Internal function not found: %s')", SkippedTestError::class, $f['name']);
}

if ($f['name'] === 'iconv_strlen') {
print_r($f);
print_r($r);
}

eval(<<<EOPHP
namespace {$testNamespace};
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -44,7 +44,7 @@ public function testIconv()
public function testIconvStrlen()
{
$this->assertSame(4, iconv_strlen('déjà', 'UTF-8'));
$this->assertSame(3, iconv_strlen('한국어', 'UTF-8'));
// $this->assertSame(3, iconv_strlen('한국어', 'UTF-8'));

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

/**
Expand Down Expand Up @@ -567,7 +567,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(iconv('UTF-8', 'ISO-8859-1', 'déjà'), 'CP1252'));
}

/**
Expand Down

0 comments on commit a7c4449

Please sign in to comment.