hiphop-php /hphp/test/slow/ext_icu/ext_icu.php

Language PHP Lines 257
MD5 Hash e706dbe8337f2430d174364b9c7c072a Estimated Cost $5,379 (why?)
Repository git://github.com/facebook/hiphop-php.git View Raw File View Project SPDX
  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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php

function VS($x, $y) {
  var_dump($x === $y);
  if ($x !== $y) { echo "Failed: $y\n"; echo "Got: $x\n";
                   var_dump(debug_backtrace()); }
}
function VERIFY($x) { VS($x, true); }

//////////////////////////////////////////////////////////////////////

function test_icu_match() {
  // Test subject strings.
  $subject = "\xd7\x96\xf0\x90\xa4\x85". " PHP is a scripting language. " .
             "\xef\xba\xb0\xef\xbb\xb3";
  $subject_32 =
    "\xf0\x90\xa4\x85\xf0\x90\xa4\x85\xf0\x90\xa4\x85\xf0\x90\xa4\x85" .
    "\xf0\x90\xa4\x85\xf0\x90\xa4\x85";
  $subject_en = "this is an english string";
  // "this is a hebrew string"
  $subject_he =
    "\xd7\x96\xd7\x94\x20" .
    "\xd7\x94\xd7\x95\xd7\x90\x20\xd7\x9e\xd7\x97\xd7\xa8\xd7\x95\xd7" .
    "\x96\xd7\xaa\x20\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa";
  // "this is an arabic string"
  $subject_ar =
    "\xef\xbb\xa9\xef".
    "\xba\xab\xef\xba\x8d\x20\xef\xbb\xa9\xef\xbb\xad\x20\xef\xba\x8e".
    "\xef\xbb\xa0\xef\xbb\xa8\xef\xba\xbb\x20\xef\xba\x8d\xef\xbb\xba".
    "\xef\xbb\xa8\xef\xba\xa0\xef\xbb\xa0\xef\xbb\xb3\xef\xba\xb0\xef".
    "\xbb\xb3";
  // "this is a hebrew string"
  $subject_mixed =
    "this is a ".
    "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa"
    ." string";

  // Test basic regex parsing functionality.
  VERIFY(icu_match("scripting", $subject) != false);
  VERIFY(icu_match("php", $subject) == false);
  VERIFY(icu_match("(\\bPHP\\b)", $subject) != false);
  VERIFY(icu_match("(\\bPHP\\b))", $subject) == false);

  // Test returning matches functionality.
  VERIFY(icu_match("(PHP) is", $subject, $matches) != false);
  VS(print_r($matches, true),
    "Array\n".
    "(\n".
    "    [0] => PHP is\n".
    "    [1] => PHP\n".
    ")\n");
  VERIFY(icu_match("is (a)", $subject, $matches,
                     UREGEX_OFFSET_CAPTURE) != false);
  VS(print_r($matches, true),
     "Array\n".
     "(\n".
     "    [0] => Array\n".
     "        (\n".
     "            [0] => is a\n".
     "            [1] => 7\n".
     "        )\n".
     "\n".
     "    [1] => Array\n".
     "        (\n".
     "            [0] => a\n".
     "            [1] => 10\n".
     "        )\n".
     "\n".
     ")\n");
  VERIFY(icu_match("\\. \xef\xba\xb0", $subject, $matches,
                     UREGEX_OFFSET_CAPTURE) != false);
  VS(print_r($matches, true),
    "Array\n".
    "(\n".
    "    [0] => Array\n".
    "        (\n".
    "            [0] => . \xef\xba\xb0\n".
    "            [1] => 30\n".
    "        )\n".
    "\n".
    ")\n");
  $junk1="\xef\xbb\xa9\xef\xbb\xad";
  $junk2="\xef\xba\x8e\xef\xbb\xa0\xef\xbb\xa8\xef\xba\xbb";
  VERIFY(icu_match("$junk1 ($junk2)",
                     $subject_ar, $matches, UREGEX_OFFSET_CAPTURE) != false);
  VS(print_r($matches, true),
    "Array\n".
    "(\n".
    "    [0] => Array\n".
    "        (\n".
    "            [0] => $junk1 $junk2\n".
    "            [1] => 4\n".
    "        )\n".
    "\n".
    "    [1] => Array\n".
    "        (\n".
    "            [0] => $junk2\n".
    "            [1] => 7\n".
    "        )\n".
    "\n".
    ")\n");

  // Test match for 32-bit code points.
  VERIFY(icu_match(".*", $subject_32, $matches) != false);
  $expected="\xf0\x90\xa4\x85\xf0\x90\xa4\x85\xf0\x90\xa4".
    "\x85\xf0\x90\xa4\x85\xf0\x90\xa4\x85\xf0\x90\xa4\x85";
  VS(print_r($matches, true),
    "Array\n".
    "(\n".
    "    [0] => $expected\n".
    ")\n");

  // Test regex caching functionality.
  VERIFY(icu_match("(php)", $subject, $ignore, UREGEX_CASE_INSENSITIVE) != false);
  VERIFY(icu_match("(php)", $subject) == false);

  // Test ICU specific (ie bidi) functionality.
  $pattern_ltr = "\\p{Bidi_Class=Left_To_Right}";
  $pattern_rtl = "\\p{Bidi_Class=Right_To_Left}";
  $pattern_arl = "\\p{Bidi_Class=Arabic_Letter}";

  VERIFY(icu_match($pattern_ltr, $subject_en) != false);
  VERIFY(icu_match($pattern_rtl, $subject_en) == false);

  VERIFY(icu_match($pattern_ltr, $subject_he) == false);
  VERIFY(icu_match($pattern_rtl, $subject_he) != false);
  VERIFY(icu_match($pattern_arl, $subject_he) == false);

  VERIFY(icu_match($pattern_ltr, $subject_ar) == false);
  VERIFY(icu_match($pattern_rtl, $subject_ar) == false);
  VERIFY(icu_match($pattern_arl, $subject_ar) != false);

  VERIFY(icu_match($pattern_ltr, $subject_mixed) != false);
  VERIFY(icu_match($pattern_rtl, $subject_mixed) != false);
}

// Test string lifted from tests/intl/utf8.h
function test_icu_transliterate() {
  $input_ru = "\xd1\x84\xd0\xb5\xd0\xb9\xd1".
           "\x81\xd0\xb1\xd1\x83\xc5\x93\xd0\xba";
  $output_ru = icu_transliterate($input_ru, false);
  // Note: different than php test ('y' -> 'j')
  VERIFY($output_ru == "fejsbu\xc5\x93k");

  // Verify that removing accents works.
  $input_de = "Ich m\xc3\xb6".
                           "chte \xc3\xbc".
                           "berzeugend ".
                            "oder \xc3\xa4hnliche sein";
  $output_de = icu_transliterate($input_de, true);
  VERIFY($output_de == "Ich mochte uberzeugend oder ahnliche sein");

  // Verify that keeping accents works.
  VERIFY(icu_transliterate($input_de, false) == $input_de);

  // Check a non-Latin language.
  $input_zh = "\xe5\x9b\x9b".
                           "\xe5\x8d\x81\xe5\x9b\x9b\xe7".
                           "\x9f\xb3\xe7\x8d\x85\xe5\xad\x90";
  $output_zh = icu_transliterate($input_zh, true);
  VERIFY($output_zh == "si shi si shi shi zi");
}


function test_icu_tokenize() {
  $input_eng = "Hello World";
  $output_eng = icu_tokenize($input_eng);

  VS(print_r($output_eng, true),
     "Array\n".
     "(\n".
     "    [0] => _B_\n".
     "    [1] => hello\n".
     "    [2] => world\n".
     "    [3] => _E_\n".
     ")\n"
    );
  $input_long = "Hello! You are visitor #1234 to ".
                            "http://www.facebook.com! ".
                            "<3 How are you today (6/14/2011),".
                            " hello@world.com?";

  $output_long = icu_tokenize($input_long);

  VS(print_r($output_long, true),
     "Array\n".
     "(\n".
     "    [0] => _B_\n".
     "    [1] => hello\n".
     "    [2] => !\n".
     "    [3] => you\n".
     "    [4] => are\n".
     "    [5] => visitor\n".
     "    [6] => #\n".
     "    [7] => XXXX\n".
     "    [8] => to\n".
     "    [9] => TOKEN_URL\n".
     "    [10] => !\n".
     "    [11] => TOKEN_HEART\n".
     "    [12] => how\n".
     "    [13] => are\n".
     "    [14] => you\n".
     "    [15] => today\n".
     "    [16] => (\n".
     "    [17] => TOKEN_DATE\n".
     "    [18] => )\n".
     "    [19] => ,\n".
     "    [20] => TOKEN_EMAIL\n".
     "    [21] => ?\n".
     "    [22] => _E_\n".
     ")\n"
    );

  $input_de = "Ich möchte überzeugend oder ähnliche sein";
  $output_de = icu_tokenize($input_de);

  VS(print_r($output_de, true),
     "Array\n".
     "(\n".
     "    [0] => _B_\n".
     "    [1] => ich\n".
     "    [2] => mã\n".
     "    [3] => ¶\n".
     "    [4] => chte\n".
     "    [5] => ã\n".
     "    [6] => ¼\n".
     "    [7] => berzeugend\n".
     "    [8] => oder\n".
     "    [9] => ã\n".
     "    [10] => ¤\n".
     "    [11] => hnliche\n".
     "    [12] => sein\n".
     "    [13] => _E_\n".
     ")\n");

  $input_hebrew = "היום יום רביעי, וזה ממש טוב.";
  $output_hebrew = icu_tokenize($input_hebrew);
  VS(print_r($output_hebrew, true),
     "Array\n".
     "(\n".
     "    [0] => _B_\n".
     "    [1] => היום\n".
     "    [2] => יום\n".
     "    [3] => רביעי\n".
     "    [4] => ,\n".
     "    [5] => וזה\n".
     "    [6] => ממש\n".
     "    [7] => טוב\n".
     "    [8] => .\n".
     "    [9] => _E_\n".
     ")\n");
}

test_icu_match();
test_icu_transliterate();
test_icu_tokenize();
Back to Top