Skip to content

Commit 3d151ba

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
Add case folding map
1 parent 3794858 commit 3d151ba

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed

src/Mbstring/Mbstring.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final class Mbstring
6969
{
7070
public const MB_CASE_FOLD = \PHP_INT_MAX;
7171

72-
private const CASE_FOLD = [
72+
private const SIMPLE_CASE_FOLD = [
7373
['µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"],
7474
['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'],
7575
];
@@ -301,7 +301,11 @@ public static function mb_convert_case($s, $mode, $encoding = null)
301301
$map = $upper;
302302
} else {
303303
if (self::MB_CASE_FOLD === $mode) {
304-
$s = str_replace(self::CASE_FOLD[0], self::CASE_FOLD[1], $s);
304+
static $caseFolding = null;
305+
if (null === $caseFolding) {
306+
$caseFolding = self::getData('caseFolding');
307+
}
308+
$s = strtr($s, $caseFolding);
305309
}
306310

307311
static $lower = null;
@@ -638,8 +642,10 @@ public static function mb_substr($s, $start, $length = null, $encoding = null)
638642

639643
public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
640644
{
641-
$haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
642-
$needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
645+
[$haystack, $needle] = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [
646+
self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding),
647+
self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding),
648+
]);
643649

644650
return self::mb_strpos($haystack, $needle, $offset, $encoding);
645651
}
@@ -674,8 +680,11 @@ public static function mb_strrichr($haystack, $needle, $part = false, $encoding
674680

675681
public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
676682
{
677-
$haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
678-
$needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
683+
$haystack = self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding);
684+
$needle = self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding);
685+
686+
$haystack = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack);
687+
$needle = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle);
679688

680689
return self::mb_strrpos($haystack, $needle, $offset, $encoding);
681690
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
return [
4+
'İ' => '',
5+
'µ' => 'μ',
6+
'ſ' => 's',
7+
'ͅ' => 'ι',
8+
'ς' => 'σ',
9+
'ϐ' => 'β',
10+
'ϑ' => 'θ',
11+
'ϕ' => 'φ',
12+
'ϖ' => 'π',
13+
'ϰ' => 'κ',
14+
'ϱ' => 'ρ',
15+
'ϵ' => 'ε',
16+
'' => '',
17+
'' => 'ι',
18+
'ß' => 'ss',
19+
'ʼn' => 'ʼn',
20+
'ǰ' => 'ǰ',
21+
'ΐ' => 'ΐ',
22+
'ΰ' => 'ΰ',
23+
'և' => 'եւ',
24+
'' => '',
25+
'' => '',
26+
'' => '',
27+
'' => '',
28+
'' => '',
29+
'' => 'ss',
30+
'' => '',
31+
'' => '',
32+
'' => '',
33+
'' => '',
34+
'' => 'ἀι',
35+
'' => 'ἁι',
36+
'' => 'ἂι',
37+
'' => 'ἃι',
38+
'' => 'ἄι',
39+
'' => 'ἅι',
40+
'' => 'ἆι',
41+
'' => 'ἇι',
42+
'' => 'ἀι',
43+
'' => 'ἁι',
44+
'' => 'ἂι',
45+
'' => 'ἃι',
46+
'' => 'ἄι',
47+
'' => 'ἅι',
48+
'' => 'ἆι',
49+
'' => 'ἇι',
50+
'' => 'ἠι',
51+
'' => 'ἡι',
52+
'' => 'ἢι',
53+
'' => 'ἣι',
54+
'' => 'ἤι',
55+
'' => 'ἥι',
56+
'' => 'ἦι',
57+
'' => 'ἧι',
58+
'' => 'ἠι',
59+
'' => 'ἡι',
60+
'' => 'ἢι',
61+
'' => 'ἣι',
62+
'' => 'ἤι',
63+
'' => 'ἥι',
64+
'' => 'ἦι',
65+
'' => 'ἧι',
66+
'' => 'ὠι',
67+
'' => 'ὡι',
68+
'' => 'ὢι',
69+
'' => 'ὣι',
70+
'' => 'ὤι',
71+
'' => 'ὥι',
72+
'' => 'ὦι',
73+
'' => 'ὧι',
74+
'' => 'ὠι',
75+
'' => 'ὡι',
76+
'' => 'ὢι',
77+
'' => 'ὣι',
78+
'' => 'ὤι',
79+
'' => 'ὥι',
80+
'' => 'ὦι',
81+
'' => 'ὧι',
82+
'' => 'ὰι',
83+
'' => 'αι',
84+
'' => 'άι',
85+
'' => '',
86+
'' => 'ᾶι',
87+
'' => 'αι',
88+
'' => 'ὴι',
89+
'' => 'ηι',
90+
'' => 'ήι',
91+
'' => '',
92+
'' => 'ῆι',
93+
'' => 'ηι',
94+
'' => '',
95+
'' => '',
96+
'' => '',
97+
'' => '',
98+
'' => '',
99+
'' => '',
100+
'' => '',
101+
'' => 'ὼι',
102+
'' => 'ωι',
103+
'' => 'ώι',
104+
'' => '',
105+
'' => 'ῶι',
106+
'' => 'ωι',
107+
'' => 'ff',
108+
'' => 'fi',
109+
'' => 'fl',
110+
'' => 'ffi',
111+
'' => 'ffl',
112+
'' => 'st',
113+
'' => 'st',
114+
'' => 'մն',
115+
'' => 'մե',
116+
'' => 'մի',
117+
'' => 'վն',
118+
'' => 'մխ',
119+
];

tests/Mbstring/MbstringTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public function testStrCase()
266266
$this->assertSame('Déjà Σσσ Iı Ii İi̇', p::mb_convert_case('DÉJÀ ΣΣΣ ıı iI İİ', \MB_CASE_TITLE));
267267
}
268268
$this->assertSame('ab', str_replace('?', '', mb_strtolower(urldecode('a%A1%C0b'))));
269+
$this->assertSame('hi ssΐὤιմխ', p::mb_convert_case('HI ßΐᾬﬗ', p::MB_CASE_FOLD));
269270
}
270271

271272
/**

0 commit comments

Comments
 (0)