Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[String] Fix ellipsis of truncate when not using cut option #37054

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
Jun 11, 2020
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 @@ -643,7 +643,11 @@ public function truncate(int $length, string $ellipsis = '', bool $cut = true):
}

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

$length += $ellipsisLength;
}

$str = $this->slice(0, $length - $ellipsisLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,7 @@ public static function provideTruncate()
['foobar...', 'foobar foo', 6, '...', false],
Copy link
Contributor

@apfelbox apfelbox Jun 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort-of unrelated, but what happens with ['???', 'foobar', 5, '...'],
ie. if ($length + $ellipsisLength) > $stringLength?
It should return the full string then, possibly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean, please provide a test case if you want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure whether the length provided to the method is including or excluding the ellipsis. But apparently it is, so the case I talked about can't happen here.

So nvm then 👍

['foobar...', 'foobar foo', 7, '...', false],
['foobar foo...', 'foobar foo a', 10, '...', false],
['foobar foo aar', 'foobar foo aar', 12, '...', false],
];
}

Expand Down