Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec81f1c

Browse files
Ayeshnicolas-grekas
authored andcommittedOct 22, 2020
Intl Grapheme: Adjust grapheme_substr PHP 8 compatibility
1 parent ed6b5f0 commit ec81f1c

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed
 

‎src/Iconv/Iconv.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,11 @@ public static function iconv_substr($s, $start, $length = 2147483647, $encoding
541541
$start += $slen;
542542
}
543543
if (0 > $start) {
544-
return false;
544+
if (\PHP_VERSION_ID < 80000) {
545+
return false;
546+
}
547+
548+
$start = 0;
545549
}
546550
if ($start >= $slen) {
547551
return \PHP_VERSION_ID >= 80000 ? '' : false;
@@ -556,7 +560,7 @@ public static function iconv_substr($s, $start, $length = 2147483647, $encoding
556560
return '';
557561
}
558562
if (0 > $length) {
559-
return false;
563+
return \PHP_VERSION_ID >= 80000 ? '' : false;
560564
}
561565

562566
if ($length > $rx) {

‎src/Intl/Grapheme/Grapheme.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ public static function grapheme_substr($s, $start, $len = null)
120120
$start += $slen;
121121
}
122122
if (0 > $start) {
123-
return false;
123+
if (\PHP_VERSION_ID < 80000) {
124+
return false;
125+
}
126+
127+
$start = 0;
124128
}
125129
if ($start >= $slen) {
126-
return false;
130+
return \PHP_VERSION_ID >= 80000 ? '' : false;
127131
}
128132

129133
$rem = $slen - $start;
@@ -135,7 +139,7 @@ public static function grapheme_substr($s, $start, $len = null)
135139
return '';
136140
}
137141
if (0 > $len) {
138-
return false;
142+
return \PHP_VERSION_ID >= 80000 ? '' : false;
139143
}
140144
if ($len > $rem) {
141145
$len = $rem;

‎tests/Iconv/IconvTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,26 @@ public function testIconvSubstr()
7676

7777
/**
7878
* @covers \Symfony\Polyfill\Iconv\Iconv::iconv_substr
79-
* @requires PHP < 8.0.0
79+
* @requires PHP < 8
8080
*/
8181
public function testIconvSubstrReturnsFalsePrePHP8()
8282
{
83-
$this->assertFalse(iconv_substr('x', 5, 1, 'UTF-8'));
83+
$c = 'déjà';
84+
$this->assertFalse(iconv_substr($c, 42, 1, 'UTF-8'));
85+
$this->assertFalse(iconv_substr($c, -42, 1));
86+
$this->assertFalse(iconv_substr($c, 42, 26));
8487
}
8588

8689
/**
8790
* @covers \Symfony\Polyfill\Iconv\Iconv::iconv_substr
88-
* @requires PHP >= 8.0.0
91+
* @requires PHP 8
8992
*/
9093
public function testIconvSubstrReturnsEmptyPostPHP8()
9194
{
92-
$this->assertSame('', iconv_substr('x', 5, 1, 'UTF-8'));
95+
$c = 'déjà';
96+
$this->assertSame('', iconv_substr($c, 42, 1, 'UTF-8'));
97+
$this->assertSame('d', iconv_substr($c, -42, 1));
98+
$this->assertSame('', iconv_substr($c, 42, 26));
9399
}
94100

95101
/**

‎tests/Intl/Grapheme/GraphemeTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ public function testGraphemeSubstr()
9393
$this->assertSame('', grapheme_substr($c, -2, null));
9494
}
9595

96-
if (\PHP_VERSION_ID >= 50418 && \PHP_VERSION_ID !== 50500) {
96+
if (\PHP_VERSION_ID >= 80000) {
97+
$this->assertSame('', grapheme_substr($c, -2, 3));
98+
$this->assertSame('', grapheme_substr($c, -1, 0));
99+
$this->assertSame('', grapheme_substr($c, 1, -4));
100+
} elseif (\PHP_VERSION_ID >= 50418 && \PHP_VERSION_ID !== 50500) {
97101
// See http://bugs.php.net/62759 and 55562
98102
$this->assertSame('', grapheme_substr($c, -2, 3));
99103
$this->assertSame('', grapheme_substr($c, -1, 0));
@@ -104,8 +108,32 @@ public function testGraphemeSubstr()
104108
$this->assertSame('', grapheme_substr($c, -2));
105109
$this->assertSame('j', grapheme_substr($c, -2, -1));
106110
$this->assertSame('', grapheme_substr($c, -2, -2));
107-
$this->assertFalse(grapheme_substr($c, 5, 0));
108-
$this->assertFalse(grapheme_substr($c, -5, 0));
111+
}
112+
113+
/**
114+
* @covers \Symfony\Polyfill\Intl\Grapheme\Grapheme::grapheme_substr
115+
* @requires PHP < 8
116+
*/
117+
public function testGraphemeSubstrReturnsFalsePrePHP8()
118+
{
119+
$c = 'déjà';
120+
$this->assertFalse(grapheme_substr($c, 5, 1));
121+
$this->assertFalse(grapheme_substr($c, -5, 1));
122+
$this->assertFalse(grapheme_substr($c, -42, 1));
123+
$this->assertFalse(grapheme_substr($c, 42, 5));
124+
}
125+
126+
/**
127+
* @covers \Symfony\Polyfill\Intl\Grapheme\Grapheme::grapheme_substr
128+
* @requires PHP 8
129+
*/
130+
public function testGraphemeSubstrReturnsEmptyPostPHP8()
131+
{
132+
$c = 'déjà';
133+
$this->assertSame('', grapheme_substr($c, 5, 1));
134+
$this->assertSame('d', grapheme_substr($c, -5, 1));
135+
$this->assertSame('d', grapheme_substr($c, -42, 1));
136+
$this->assertSame('', grapheme_substr($c, 42, 5));
109137
}
110138

111139
/**

0 commit comments

Comments
 (0)
Failed to load comments.