Skip to content

Commit ac70459

Browse files
duboissnicolas-grekas
authored andcommitted
[String] Fix ellipsis of truncate when not using cut option
1 parent 90c2a51 commit ac70459

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

AbstractString.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,11 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true):
643643
}
644644

645645
if (!$cut) {
646-
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
646+
if (null === $length = $this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1)) {
647+
return clone $this;
648+
}
649+
650+
$length += $ellipsisLength;
647651
}
648652

649653
$str = $this->slice(0, $length - $ellipsisLength);

Tests/AbstractAsciiTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ public static function provideTruncate()
14501450
['foobar...', 'foobar foo', 6, '...', false],
14511451
['foobar...', 'foobar foo', 7, '...', false],
14521452
['foobar foo...', 'foobar foo a', 10, '...', false],
1453+
['foobar foo aar', 'foobar foo aar', 12, '...', false],
14531454
];
14541455
}
14551456

0 commit comments

Comments
 (0)