8000 bug #38678 [String] fix before/after[Last]() returning the empty stri… · symfony/symfony@310926f · GitHub
[go: up one dir, main page]

Skip to content

Commit 310926f

Browse files
committed
bug #38678 [String] fix before/after[Last]() returning the empty string instead of the original one on non-match (nicolas-grekas)
This PR was merged into the 5.1 branch. Discussion ---------- [String] fix before/after[Last]() returning the empty string instead of the original one on non-match | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The current behavior is the least useful one. Returning the original string when no match is found is the most sensible behavior. It saves code instead of requiring more. Typical example is when removing a potential suffix: `$bar = $foo->beforeLast(['.svg', '.png']);` should remove any of these extensions if found, and return the original string otherwise. Commits ------- 22a2740 [String] fix before/after[Last]() returning the empty string instead of the original one on non-match
2 parents 29684ac + 22a2740 commit 310926f

File tree

4 files changed

+35
-39
lines changed

4 files changed

+35
-39
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public static function wrap(array $values): array
9898
public function after($needle, bool $includeNeedle = false, int $offset = 0): self
9999
{
100100
$str = clone $this;
101-
$str->string = '';
102101
$i = \PHP_INT_MAX;
103102

104103
foreach ((array) $needle as $n) {
@@ -130,7 +129,6 @@ public function after($needle, bool $includeNeedle = false, int $offset = 0): se
130129
public function afterLast($needle, bool $includeNeedle = false, int $offset = 0): self
131130
{
132131
$str = clone $this;
133-
$str->string = '';
134132
$i = null;
135133

136134
foreach ((array) $needle as $n) {
@@ -167,7 +165,6 @@ abstract public function append(string ...$suffix): self;
167165
public function before($needle, bool $includeNeedle = false, int $offset = 0): self
168166
{
169167
$str = clone $this;
170-
$str->string = '';
171168
$i = \PHP_INT_MAX;
172169

173170
foreach ((array) $needle as $n) {
@@ -199,7 +196,6 @@ public function before($needle, bool $includeNeedle = false, int $offset = 0): s
199196
public function beforeLast($needle, bool $includeNeedle = false, int $offset = 0): self
200197
{
201198
$str = clone $this;
202-
$str->string = '';
203199
$i = null;
204200

205201
foreach ((array) $needle as $n) {

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,12 @@ public function testBeforeAfter(string $expected, string $needle, string $origin
797797
public static function provideBeforeAfter()
798798
{
799799
return [
800-
['', '', 'hello world', 0, true],
801-
['', '', 'hello world', 0, false],
802-
['', 'w', 'hello World', 0, true],
803-
['', 'w', 'hello World', 0, false],
804-
['', 'o', 'hello world', 10, true],
805-
['', 'o', 'hello world', 10, false],
800+
['hello world', '', 'hello world', 0, true],
801+
['hello world', '', 'hello world', 0, false],
802+
['hello World', 'w', 'hello World', 0, true],
803+
['hello World', 'w', 'hello World', 0, false],
804+
['hello world', 'o', 'hello world', 10, true],
805+
['hello world', 'o', 'hello world', 10, false],
806806
['hello ', 'w', 'hello world', 0, true],
807807
['world', 'w', 'hello world', 0, false],
808808
['hello W', 'O', 'hello WORLD', 0, true],
@@ -825,12 +825,12 @@ public function testBeforeAfterIgnoreCase(string $expected, string $needle, stri
825825
public static function provideBeforeAfterIgnoreCase()
826826
{
827827
return [
828-
['', '', 'hello world', 0, true],
829-
['', '', 'hello world', 0, false],
830-
['', 'foo', 'hello world', 0, true],
831-
['', 'foo', 'hello world', 0, false],
832-
['', 'o', 'hello world', 10, true],
833-
['', 'o', 'hello world', 10, false],
828+
['hello world', '', 'hello world', 0, true],
829+
['hello world', '', 'hello world', 0, false],
830+
['hello world', 'foo', 'hello world', 0, true],
831+
['hello world', 'foo', 'hello world', 0, false],
832+
['hello world', 'o', 'hello world', 10, true],
833+
['hello world', 'o', 'hello world', 10, false],
834834
['hello ', 'w', 'hello world', 0, true],
835835
['world', 'w', 'hello world', 0, false],
836836
['hello ', 'W', 'hello world', 0, true],
@@ -853,12 +853,12 @@ public function testBeforeAfterLast(string $expected, string $needle, string $or
853853
public static function provideBeforeAfterLast()
854854
{
855855
return [
856-
['', '', 'hello world', 0, true],
857-
['', '', 'hello world', 0, false],
858-
['', 'L', 'hello world', 0, true],
859-
['', 'L', 'hello world', 0, false],
860-
['', 'o', 'hello world', 10, true],
861-
['', 'o', 'hello world', 10, false],
856+
['hello world', '', 'hello world', 0, true],
857+
['hello world', '', 'hello world', 0, false],
858+
['hello world', 'L', 'hello world', 0, true],
859+
['hello world', 'L', 'hello world', 0, false],
860+
['hello world', 'o', 'hello world', 10, true],
861+
['hello world', 'o', 'hello world', 10, false],
862862
['hello wor', 'l', 'hello world', 0, true],
863863
['ld', 'l', 'hello world', 0, false],
864864
['hello w', 'o', 'hello world', 0, true],
@@ -882,12 +882,12 @@ public function testBeforeAfterLastIgnoreCase(string $expected, string $needle,
882882
public static function provideBeforeAfterLastIgnoreCase()
883883
{
884884
return [
885-
['', '', 'hello world', 0, true],
886-
['', '', 'hello world', 0, false],
887-
['', 'FOO', 'hello world', 0, true],
888-
['', 'FOO', 'hello world', 0, false],
889-
['', 'o', 'hello world', 10, true],
890-
['', 'o', 'hello world', 10, false],
885+
['hello world', '', 'hello world', 0, true],
886+
['hello world', '', 'hello world', 0, false],
887+
['hello world', 'FOO', 'hello world', 0, true],
888+
['hello world', 'FOO', 'hello world', 0, false],
889+
['hello world', 'o', 'hello world', 10, true],
890+
['hello world', 'o', 'hello world', 10, false],
891891
['hello wor', 'l', 'hello world', 0, true],
892892
['ld', 'l', 'hello world', 0, false],
893893
['hello wor', 'L', 'hello world', 0, true],

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ public static function provideBeforeAfterIgnoreCase(): array
396396
['', '', 'déjàdéjà', 0, true],
397397
['éjàdéjà', 'é', 'déjàdéjà', 0, false],
398398
['d', 'é', 'déjàdéjà', 0, true],
399-
['', 'Ç', 'déjàdéjà', 0, false],
400-
['', 'Ç', 'déjàdéjà', 0, true],
399+
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
400+
['déjàdéjà', 'Ç', 'déjàdéjà', 0, true],
401401
]
402402
);
403403
}
@@ -407,8 +407,8 @@ public static function provideBeforeAfterLast(): array
407407
return array_merge(
408408
parent::provideBeforeAfterLast(),
409409
[
410-
['', 'Ç', 'déjàdéjà', 0, false],
411-
['', 'Ç', 'déjàdéjà', 0, true],
410+
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
411+
['déjàdéjà', 'Ç', 'déjàdéjà', 0, true],
412412
['éjà', 'é', 'déjàdéjà', 0, false],
413413
['déjàd', 'é', 'déjàdéjà', 0, true],
414414
]
@@ -420,7 +420,7 @@ public static function provideBeforeAfterLastIgnoreCase(): array
420420
return array_merge(
421421
parent::provideBeforeAfterLastIgnoreCase(),
422422
[
423-
['', 'Ç', 'déjàdéjà', 0, false],
423+
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
424424
['éjà', 'é', 'déjàdéjà', 0, false],
425425
['éjà', 'É', 'déjàdéjà', 0, false],
426426
]

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public static function provideBeforeAfterIgnoreCase(): array
186186
return array_merge(
187187
parent::provideBeforeAfterIgnoreCase(),
188188
[
189-
['', 'छेछे', 'दछेच्नुअ', 0, false],
190-
['', 'छेछे', 'दछेच्नुअ', 0, true],
189+
['दछेच्नुअ', 'छेछे', 'दछेच्नुअ', 0, false],
190+
['दछेच्नुअ', 'छेछे', 'दछेच्नुअ', 0, true],
191191
['छेच्नुअ', 'छे', 'दछेच्नुअ', 0, false],
192192
['', 'छे', 'दछेच्नुअ', 0, true],
193193
]
@@ -199,8 +199,8 @@ public static function provideBeforeAfterLast(): array
199199
return array_merge(
200200
parent::provideBeforeAfterLast(),
201201
[
202-
['', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
203-
['', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
202+
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
203+
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
204204
['-दछेच्नु', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, false],
205205
['दछेच्नुअ-दछेच्नु-अद', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, true],
206206
]
@@ -212,8 +212,8 @@ public static function provideBeforeAfterLastIgnoreCase(): array
212212
return array_merge(
213213
parent::provideBeforeAfterLastIgnoreCase(),
214214
[
215-
['', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
216-
['', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
215+
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
216+
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
217217
['-दछेच्नु', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, false],
218218
['दछेच्नुअ-दछेच्नु-अद', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, true],
219219
]

0 commit comments

Comments
 (0)
0