Skip to content

[String] Method toByteString conversion using iconv is unreachable #52491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,11 @@ public function toByteString(string $toEncoding = null): ByteString
try {
try {
$b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
} catch (InvalidArgumentException $e) {
} catch (InvalidArgumentException|\ValueError $e) {
if (!\function_exists('iconv')) {
if ($e instanceof \ValueError) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
throw $e;
}

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1583,4 +1583,22 @@ public static function provideWidth(): array
[17, "\u{007f}\u{007f}f\u{001b}[0moo\u{0001}bar\u{007f}cccïf\u{008e}cy\u{0005}1", false], // f[0moobarcccïfcy1
];
}

/**
* @dataProvider provideToByteString
*/
public function testToByteString(string $origin, string $encoding)
{
$instance = static::createFromString($origin)->toByteString($encoding);
$this->assertInstanceOf(ByteString::class, $instance);
}

public static function provideToByteString(): array
{
return [
['žsžsý', 'UTF-8'],
['žsžsý', 'windows-1250'],
['žsžsý', 'Windows-1252'],
];
}
}