Skip to content

[String] Add trimSuffix() and trimPrefix() methods #43481

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
Oct 15, 2021
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
68 changes: 68 additions & 0 deletions src/Symfony/Component/String/AbstractString.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,79 @@ abstract public function trim(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}")
*/
abstract public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;

/**
* @param string|string[] $prefix
*
* @return static
*/
public function trimPrefix($prefix): self
{
if (\is_array($prefix) || $prefix instanceof \Traversable) {
foreach ($prefix as $s) {
$t = $this->trimPrefix($s);

if ($t->string !== $this->string) {
return $t;
}
}

return clone $this;
}

$str = clone $this;

if ($prefix instanceof self) {
$prefix = $prefix->string;
} else {
$prefix = (string) $prefix;
}

if ('' !== $prefix && \strlen($this->string) >= \strlen($prefix) && 0 === substr_compare($this->string, $prefix, 0, \strlen($prefix), $this->ignoreCase)) {
$str->string = substr($this->string, \strlen($prefix));
}

return $str;
}

/**
* @return static
*/
abstract public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): self;

/**
* @param string|string[] $suffix
*
* @return static
*/
public function trimSuffix($suffix): self
{
if (\is_array($suffix) || $suffix instanceof \Traversable) {
foreach ($suffix as $s) {
$t = $this->trimSuffix($s);

if ($t->string !== $this->string) {
return $t;
}
}

return clone $this;
}

$str = clone $this;

if ($suffix instanceof self) {
$suffix = $suffix->string;
} else {
$suffix = (string) $suffix;
}

if ('' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase)) {
$str->string = substr($this->string, 0, -\strlen($suffix));
}

return $str;
}

/**
* @return static
*/
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,26 @@ public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): pare
return $str;
}

public function trimPrefix($prefix): parent
{
if (!$this->ignoreCase) {
return parent::trimPrefix($prefix);
}

$str = clone $this;

if ($prefix instanceof \Traversable) {
$prefix = iterator_to_array($prefix, false);
} elseif ($prefix instanceof parent) {
$prefix = $prefix->string;
}

$prefix = implode('|', array_map('preg_quote', (array) $prefix));
$str->string = preg_replace("{^(?:$prefix)}iuD", '', $this->string);

return $str;
}

public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): parent
{
if (" \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}" !== $chars && !preg_match('//u', $chars)) {
Expand All @@ -422,6 +442,26 @@ public function trimStart(string $chars = " \t\n\r\0\x0B\x0C\u{A0}\u{FEFF}"): pa
return $str;
}

public function trimSuffix($suffix): parent
{
if (!$this->ignoreCase) {
return parent::trimSuffix($suffix);
}

$str = clone $this;

if ($suffix instanceof \Traversable) {
$suffix = iterator_to_array($suffix, false);
} elseif ($suffix instanceof parent) {
$suffix = $suffix->string;
}

$suffix = implode('|', array_map('preg_quote', (array) $suffix));
$str->string = preg_replace("{(?:$suffix)$}iuD", '', $this->string);

return $str;
}

public function upper(): parent
{
$str = clone $this;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Add `trimSuffix()` and `trimPrefix()` methods

5.3
---

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,15 @@ public static function provideTrim()
];
}

public function testTrimPrefix()
{
$str = static::createFromString('abc.def');

$this->assertEquals(static::createFromString('def'), $str->trimPrefix('abc.'));
$this->assertEquals(static::createFromString('def'), $str->trimPrefix(['abc.', 'def']));
$this->assertEquals(static::createFromString('def'), $str->ignoreCase()->trimPrefix('ABC.'));
}

/**
* @dataProvider provideTrimStart
*/
Expand All @@ -744,6 +753,15 @@ public function testTrimStart(string $expected, string $origin, ?string $chars)
$this->assertEquals(static::createFromString($expected), $result);
}

public function testTrimSuffix()
{
$str = static::createFromString('abc.def');

$this->assertEquals(static::createFromString('abc'), $str->trimSuffix('.def'));
$this->assertEquals(static::createFromString('abc'), $str->trimSuffix(['.def', 'abc']));
$this->assertEquals(static::createFromString('abc'), $str->ignoreCase()->trimSuffix('.DEF'));
}

public static function provideTrimStart()
{
return [
Expand Down