8000 bug #47010 [String] Fix `width` method in `AbstractUnicodeString` (TB… · zimny9932/symfony@c02b704 · GitHub
[go: up one dir, main page]

Skip to content

Commit c02b704

Browse files
committed
bug symfony#47010 [String] Fix width method in AbstractUnicodeString (TBoileau)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [String] Fix `width` method in `AbstractUnicodeString` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#47004 | License | MIT | Doc PR | none - bugfix This PR ensures that the `width` method of `AbstractUnicodeString` returns the width and not the length of the text. Commits ------- 8150678 [String] Fix `width` method in `AbstractUnicodeString`
2 parents 296ff2b + 8150678 commit c02b704

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,11 @@ public function width(bool $ignoreAnsiDecoration = true): int
498498
)|[\p{Cc}\x7F]++)/xu', '', $s);
499499
}
500500

501-
// Non printable characters have been dropped, so wcswidth cannot logically return -1.
502-
$width += $this->wcswidth($s);
501+
$lineWidth = $this->wcswidth($s);
502+
503+
if ($lineWidth > $width) {
504+
$width = $lineWidth;
505+
}
503506
}
504507

505508
return $width;

src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515

1616
abstract class AbstractUnicodeTestCase extends AbstractAsciiTestCase
1717
{
18+
public static function provideWidth(): array
19+
{
20+
return array_merge(
21+
parent::provideWidth(),
22+
[
23+
[14, '<<<END
24+
This is a
25+
multiline text
26+
END'],
27+
]
28+
);
29+
}
30+
1831
public function testCreateFromStringWithInvalidUtf8Input()
1932
{
2033
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)
0