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
6 changes: 5 additions & 1 deletion src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FE
/**
* @return static
*/
public function truncate(int $length, string $ellipsis = ''): self
public function truncate(int $length, string $ellipsis = '', bool $cut = true): self
{
$stringLength = $this->length();

Expand All @@ -634,6 +634,10 @@ public function truncate(int $length, string $ellipsis = ''): self
$ellipsisLength = 0;
}

if (!$cut) {
$length = $ellipsisLength + ($this->indexOf([' ', "\r", "\n", "\t"], ($length ?: 1) - 1) ?? $stringLength);
}

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

return $ellipsisLength ? $str->trimEnd()->append($ellipsis) : $str;
Expand Down
7 changes: 4 additions & 3 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ CHANGELOG
* added the `AbstractString::reverse()` method
* made `AbstractString::width()` follow POSIX.1-2001
* added `LazyString` which provides memoizing stringable objects
* The component is not marked as `@experimental` anymore.
* Added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy.
* The component is not marked as `@experimental` anymore
* added the `s()` helper method to get either an `UnicodeString` or `ByteString` instance,
depending of the input string UTF-8 compliancy
* added `$cut` parameter to `Symfony\Component\String\AbstractString::truncate()`

5.0.0
-----
Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1405,9 +1405,9 @@ public static function providePadStart()
/**
* @dataProvider provideTruncate
*/
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis)
public function testTruncate(string $expected, string $origin, int $length, string $ellipsis, bool $cut = true)
{
$instance = static::createFromString($origin)->truncate($length, $ellipsis);
$instance = static::createFromString($origin)->truncate($length, $ellipsis, $cut);

$this->assertEquals(static::createFromString($expected), $instance);
}
Expand All @@ -1417,12 +1417,17 @@ public static function provideTruncate()
return [
['', '', 3, ''],
['', 'foo', 0, '...'],
['foo', 'foo', 0, '...', false],
['fo', 'foobar', 2, ''],
['foobar', 'foobar', 10, ''],
['foobar', 'foobar', 10, '...', false],
['foo', 'foo', 3, '...'],
['fo', 'foobar', 2, '...'],
['...', 'foobar', 3, '...'],
['fo...', 'foobar', 5, '...'],
['foobar...', 'foobar foo', 6, '...', false],
['foobar...', 'foobar foo', 7, '...', false],
['foobar foo...', 'foobar foo a', 10, '...', false],
];
}

Expand Down