Skip to content
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
13 changes: 11 additions & 2 deletions src/Mapper/NicknameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ private function symmetricCloserAppears(array $parts, int $openKey, string $stri
{
$closerLength = mb_strlen($closer, 'UTF-8');

if ($stripped !== '' && mb_substr($stripped, -$closerLength, null, 'UTF-8') === $closer) {
if ($stripped !== '' && self::endsWithCloser($stripped, $closer, $closerLength)) {
return true;
}

if (! array_key_exists($closer, $this->lastCloserIndex)) {
$last = null;
foreach ($parts as $k => $part) {
if (! is_string($part)
|| mb_substr($part, -$closerLength, null, 'UTF-8') !== $closer) {
|| ! self::endsWithCloser($part, $closer, $closerLength)) {
continue;
}

Expand All @@ -308,6 +308,15 @@ private function symmetricCloserAppears(array $parts, int $openKey, string $stri
return $last !== null && $last > $openKey;
}

private static function endsWithCloser(string $part, string $closer, int $closerLength): bool
{
// Valid UTF-8 needs only a byte suffix check; retain mbstring substitution
// semantics for malformed strings passed directly to the mapper.
return mb_check_encoding($part, 'UTF-8')
? str_ends_with($part, $closer)
: mb_substr($part, -$closerLength, null, 'UTF-8') === $closer;
}

protected function buildRegexp(): string
{
if (empty($this->delimiters)) {
Expand Down
58 changes: 58 additions & 0 deletions tests/Mapper/NicknameMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,64 @@ public function testReplacementDelimitersPreserveLiteralQuoteContent(string $inp
);
}

/**
* @return iterable<string, array{string}>
*/
public static function symmetricDelimiterProvider(): iterable
{
yield 'ASCII quote' => ['"'];
yield 'multi-character ASCII' => ['||'];
yield 'multibyte' => ['§'];
yield 'multi-character Unicode' => ['§界'];
yield 'combining mark' => ["e\u{0301}"];
}

#[DataProvider('symmetricDelimiterProvider')]
public function testSymmetricClosersPreserveTokenBoundaries(string $delimiter): void
{
$mapper = new NicknameMapper([$delimiter => $delimiter]);

$this->assertSame(
self::canonicalParts(['John', new Nickname('Bób'), new Nickname('李'), 'Smith']),
self::canonicalParts($mapper->map(['John', $delimiter . 'Bób', '李' . $delimiter, 'Smith'])),
);
$this->assertSame(
self::canonicalParts(['John', new Nickname('Bób'), 'Smith']),
self::canonicalParts($mapper->map(['John', $delimiter . 'Bób' . $delimiter, 'Smith'])),
);
$this->assertSame(
['John', $delimiter . 'Bób', '李' . $delimiter . ';', 'Smith'],
$mapper->map(['John', $delimiter . 'Bób', '李' . $delimiter . ';', 'Smith']),
);
$this->assertSame(
self::canonicalParts(['John', $delimiter . 'orphan', new Nickname('Bób'), 'Smith']),
self::canonicalParts($mapper->map(['John', $delimiter . 'orphan', $delimiter . 'Bób' . $delimiter, 'Smith'])),
);
}

public function testMalformedUtf8PreservesMbstringSubstitutionForClosers(): void
{
$substitute = mb_substitute_character();
mb_substitute_character(63);

try {
$mapper = new NicknameMapper(['?' => '?']);
$this->assertSame(
['John', 'Bob', "Bad\xFF", 'Smith'],
$mapper->map(['John', '?Bob', "Bad\xFF", 'Smith']),
);

mb_substitute_character(39);
$mapper = new NicknameMapper();
$this->assertSame(
['John', 'Bob', "Bad\xFF", 'Smith'],
$mapper->map(['John', "'Bob", "Bad\xFF", 'Smith']),
);
} finally {
mb_substitute_character($substitute);
}
}

protected function getMapper(): NicknameMapper
{
return new NicknameMapper([
Expand Down
Loading