Skip to content

Commit 905902c

Browse files
committed
Simplify offsetGet even more
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent eb248b3 commit 905902c

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/UtfString.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ public function offsetGet($offset): string|null
116116

117117
if ($delta > 0) {
118118
// Fast forwarding.
119-
while ($delta-- > 0) {
120-
$this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4), 0, 1));
121-
++$this->charIdx;
122-
}
119+
$this->byteIdx += strlen(mb_substr(substr($this->str, $this->byteIdx, 4 * $delta), 0, $delta));
120+
$this->charIdx += $delta;
123121
} elseif ($delta < 0) {
124122
// Rewinding.
125123
while ($delta++ < 0) {
@@ -132,7 +130,7 @@ public function offsetGet($offset): string|null
132130
--$this->charIdx;
133131
}
134132
}
135-
133+
136134
// Fetch the first Unicode character within the next 4 bytes in the string.
137135
return mb_substr(substr($this->str, $this->byteIdx, 4), 0, 1);
138136
}

tests/Misc/UtfStringTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function utf8StringsProvider(): array
8989
'č',
9090
],
9191
'emoji' => [
92-
'😂😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯',
92+
'🦋😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯',
9393
'😂',
9494
'😋',
9595
],
@@ -98,6 +98,11 @@ public static function utf8StringsProvider(): array
9898
null,
9999
null,
100100
],
101+
'random' => [
102+
'xℤⅿↈⅬ⅀ↆℜℝ⅗ℾ℧ⅰℓⅯⅵⅣ⅒21⅞',
103+
'',
104+
'',
105+
],
101106
];
102107
}
103108
}

0 commit comments

Comments
 (0)