-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
212 lines (198 loc) · 5.9 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
use Patchwork\PHP\Shim\Intl;
use Patchwork\Utf8\Bootup;
use Symfony\Polyfill\Intl\Grapheme\Grapheme;
require_once('vendor/autoload.php');
/**
* Test online with https://3v4l.org/
* Create function to reverse a string in php
* Ex: abcd is returned as dcba
* 1) simplest possible
* 2) without any built-in functions
* 3) you can use only one string in memory on low resources.
* 4) that works with utf-8 too
* 5) with full grapheme support
* 6) using IntlBreakIterator
*/
/**
* @param $string
* @return string
*/
function reverse1($string)
{
return strrev($string);
}
/**
* @param $string
* @return string
*/
function reverse2($string)
{
$size = strlen($string);
$reversed = '';
for ($i = 0; $i < $size; $i++) {
$reversed .= $string[$size - $i - 1];
}
return $reversed;
}
/**
* @param $string
* @return mixed
*/
function reverse3($string)
{
$size = strlen($string);
for ($i = (int)($size / 2.0) - 1; $i >= 0; $i--) {
$tempChar = $string[$i]; // save iterating character
$string[$i] = $string[$size - $i - 1]; // send last character to new position
$string[$size - $i - 1] = $tempChar; // put character to the end
}
return $string;
}
/**
* Pattern modifiers: u(PCRE_UTF8), s(PCRE_DOTALL)
* Use utf-8 and match dot.
* @param $str
* @return string
*/
function reverse4($str)
{
preg_match_all('/./us', $str, $ar);
return join('', array_reverse($ar[0]));
}
/**
* This function pass all the tests visually if test in real browser.
* Set as standard for tests.
* All other function won't work correct with some grapheme
* @param $string
* @return string
*/
function reverse5($string)
{
if (!extension_loaded('intl')) {
return 'intl extension is not loaded';
}
$length = grapheme_strlen($string);
$ret = [];
for ($i = $length; $i >= 0; $i -= 1) {
$ret[] = grapheme_substr($string, $i, 1);
}
return implode($ret);
}
/**
* IntlBreakIterator can be used since PHP 5.5 (intl 3.0)
* @param $str
* @return string
*/
function reverse6($str)
{
if (!extension_loaded('intl')) {
return 'intl extension is not loaded';
}
$it = IntlBreakIterator::createCodePointInstance();
$it->setText($str);
$ret = '';
$prev = 0;
foreach ($it as $pos) {
$ret = substr($str, $prev, $pos - $prev) . $ret;
$prev = $pos;
}
return $ret;
}
/**
* Test symfony/polyfill-intl-grapheme library
* Fails:
* - Emoji with skintone variations
* - Emoji with sex variations
* - Apple-invented combined emoji
*
* @param $string
* @return string
*/
function reverse7($string)
{
$length = Grapheme::grapheme_strlen($string);
$ret = [];
for ($i = 0; $i < $length; $i += 1) {
$ret[] = Grapheme::grapheme_substr($string, $i, 1);
}
return implode(array_reverse($ret));
}
/**
* Test patchwork/utf8 library
* Fails:
* - Emoji with skintone variations
* - Emoji with sex variations
* - Apple-invented combined emoji
*
* @param $string
* @return string
*/
function reverse8($string)
{
Bootup::initAll();
$length = Intl::grapheme_strlen($string);
$ret = [];
for ($i = 0; $i < $length; $i += 1) {
$ret[] = Intl::grapheme_substr($string, $i, 1);
}
return implode(array_reverse($ret));
}
/**
* @param $str
* @param $functionName
* @param $correctReverse
*/
function compareStrings($str, $functionName, $correctReverse)
{
$reversed = $functionName($str);
$testPass = ($reversed === $correctReverse);
echo $functionName[-1] . '. ' . $reversed . (($testPass) ? ' <b style="color:green">PASS</b>' : ' <b style="color:red">FAIL</b>') . PHP_EOL;
}
/**
* @param $str
* @param $correctReverse
*/
function testMe($str, $correctReverse)
{
echo "--- Testing: '$str'" . PHP_EOL;
compareStrings($str, 'reverse1', $correctReverse);
compareStrings($str, 'reverse2', $correctReverse);
compareStrings($str, 'reverse3', $correctReverse);
compareStrings($str, 'reverse4', $correctReverse);
compareStrings($str, 'reverse5', $correctReverse); // best candidate
compareStrings($str, 'reverse6', $correctReverse);
compareStrings($str, 'reverse7', $correctReverse);
compareStrings($str, 'reverse8', $correctReverse);
echo "----------------------------" . PHP_EOL;
}
echo '<pre>';
testMe("ŎŏĞğÇ窺ЎўҒғЧчШш‘", "‘шШчЧғҒўЎşŞçÇğĞŏŎ"); // uzbek problematic letters test
testMe('Hello from github', 'buhtig morf olleH');
testMe("の\r\n", "\r\nの");
testMe('', '');
testMe('1', '1');
testMe('ab', 'ba');
testMe('тест по UTF8', '8FTU оп тсет');
testMe('اهلا بك', 'كب الها');
testMe('👹👺💀👻', '👻💀👺👹');
testMe("abca\xCC\x8Ao\xCC\x88", 'öåcba');
testMe("\u{1000}\u{1F7C9}\u{12043}𒁂\u{12042}\u{12030}\u{12031}\u{10ffff}", '𒀱𒀰𒁂𒁂𒁃🟉က');
echo 'Vertically-stacked characters:';
testMe('Z̤͔ͧ̑̓ä͖̭̈̇lͮ̒ͫǧ̗͚̚o̙̔ͮ̇͐̇', 'o̙̔ͮ̇͐̇ǧ̗͚̚lͮ̒ͫä͖̭̈̇Z̤͔ͧ̑̓');
echo 'Right-to-left words:';
testMe('اختبار النص', 'صنلا رابتخا');
echo 'Mixed-direction words:';
testMe('من left اليمين to الى right اليسار', 'راسيلا thgir ىلا ot نيميلا tfel نم');
echo 'Mixed-direction characters:';
testMe('abcdefg', 'gfedcba');
echo 'Very long characters:';
testMe('﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽', '﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽﷽');
testMe('👭👬⚧⚥⚣⚢⚤', '⚤⚢⚣⚥⚧👬👭');
echo 'Emoji with skintone variations:';
testMe('👱👱🏻👱🏼👱🏽👱🏾👱🏿', '👱🏿👱🏾👱🏽👱🏼👱🏻👱');
echo 'Emoji with sex variations:';
testMe('🧟♀️🧟♂️', '🧟♂️🧟♀️');
echo 'Apple-invented combined emoji:';
testMe('👨❤️💋👨👩👩👧👦', '👩👩👧👦👨❤️💋👨');
echo '</pre>';