-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathtransliteration.php
212 lines (196 loc) · 6.35 KB
/
transliteration.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
/**
* @file
* Transliteration processing functions.
*/
/**
* Transliterates UTF-8 encoded text to US-ASCII.
*
* Based on Mediawiki's UtfNormal::quickIsNFCVerify().
* Based on Drupal 7 transliteration module.
*
* @param $string
* UTF-8 encoded text input.
* @param $unknown
* Replacement string for characters that do not have a suitable ASCII
* equivalent.
* @param $source_langcode
* Optional ISO 639 language code that denotes the language of the input and
* is used to apply language-specific variations. If the source language is
* not known at the time of transliteration, it is recommended to set this
* argument to the site default language to produce consistent results.
* Otherwise the current display language will be used.
* @return
* Transliterated text.
*/
function _transliteration_process($string, $unknown = '?', $source_langcode = NULL) {
// ASCII is always valid NFC! If we're only ever given plain ASCII, we can
// avoid the overhead of initializing the decomposition tables by skipping
// out early.
if (!preg_match('/[\x80-\xff]/', $string)) {
return $string;
}
static $tail_bytes;
if (!isset($tail_bytes)) {
// Each UTF-8 head byte is followed by a certain number of tail bytes.
$tail_bytes = array();
for ($n = 0; $n < 256; $n++) {
if ($n < 0xc0) {
$remaining = 0;
}
elseif ($n < 0xe0) {
$remaining = 1;
}
elseif ($n < 0xf0) {
$remaining = 2;
}
elseif ($n < 0xf8) {
$remaining = 3;
}
elseif ($n < 0xfc) {
$remaining = 4;
}
elseif ($n < 0xfe) {
$remaining = 5;
}
else {
$remaining = 0;
}
$tail_bytes[chr($n)] = $remaining;
}
}
// Chop the text into pure-ASCII and non-ASCII areas; large ASCII parts can
// be handled much more quickly. Don't chop up Unicode areas for punctuation,
// though, that wastes energy.
preg_match_all('/[\x00-\x7f]+|[\x80-\xff][\x00-\x40\x5b-\x5f\x7b-\xff]*/', $string, $matches);
$result = '';
foreach ($matches[0] as $str) {
if ($str[0] < "\x80") {
// ASCII chunk: guaranteed to be valid UTF-8 and in normal form C, so
// skip over it.
$result .= $str;
continue;
}
// We'll have to examine the chunk byte by byte to ensure that it consists
// of valid UTF-8 sequences, and to see if any of them might not be
// normalized.
//
// Since PHP is not the fastest language on earth, some of this code is a
// little ugly with inner loop optimizations.
$head = '';
$chunk = strlen($str);
// Counting down is faster. I'm *so* sorry.
$len = $chunk + 1;
for ($i = -1; --$len; ) {
$c = $str[++$i];
if ($remaining = $tail_bytes[$c]) {
// UTF-8 head byte!
$sequence = $head = $c;
do {
// Look for the defined number of tail bytes...
if (--$len && ($c = $str[++$i]) >= "\x80" && $c < "\xc0") {
// Legal tail bytes are nice.
$sequence .= $c;
}
else {
if ($len == 0) {
// Premature end of string! Drop a replacement character into
// output to represent the invalid UTF-8 sequence.
$result .= $unknown;
break 2;
}
else {
// Illegal tail byte; abandon the sequence.
$result .= $unknown;
// Back up and reprocess this byte; it may itself be a legal
// ASCII or UTF-8 sequence head.
--$i;
++$len;
continue 2;
}
}
} while (--$remaining);
$n = ord($head);
if ($n <= 0xdf) {
$ord = ($n - 192) * 64 + (ord($sequence[1]) - 128);
}
elseif ($n <= 0xef) {
$ord = ($n - 224) * 4096 + (ord($sequence[1]) - 128) * 64 + (ord($sequence[2]) - 128);
}
elseif ($n <= 0xf7) {
$ord = ($n - 240) * 262144 + (ord($sequence[1]) - 128) * 4096 + (ord($sequence[2]) - 128) * 64 + (ord($sequence[3]) - 128);
}
elseif ($n <= 0xfb) {
$ord = ($n - 248) * 16777216 + (ord($sequence[1]) - 128) * 262144 + (ord($sequence[2]) - 128) * 4096 + (ord($sequence[3]) - 128) * 64 + (ord($sequence[4]) - 128);
}
elseif ($n <= 0xfd) {
$ord = ($n - 252) * 1073741824 + (ord($sequence[1]) - 128) * 16777216 + (ord($sequence[2]) - 128) * 262144 + (ord($sequence[3]) - 128) * 4096 + (ord($sequence[4]) - 128) * 64 + (ord($sequence[5]) - 128);
} else {
$ord = $n;
}
$result .= _transliteration_replace($ord, $unknown, $source_langcode);
$head = '';
} elseif ($c < "\x80") {
// ASCII byte.
$result .= $c;
$head = '';
} elseif ($c < "\xc0") {
// Illegal tail bytes.
if ($head == '') {
$result .= $unknown;
}
} else {
// Miscellaneous freaks.
$result .= $unknown;
$head = '';
}
}
}
return $result;
}
/**
* Replaces a Unicode character using the transliteration database.
*
* @param $ord
* An ordinal Unicode character code.
* @param $unknown
* Replacement string for characters that do not have a suitable ASCII
* equivalent.
* @param $langcode
* Optional ISO 639 language code that denotes the language of the input and
* is used to apply language-specific variations. Defaults to the current
* display language.
* @return
* ASCII replacement character.
*/
function _transliteration_replace($ord, $unknown = '?', $langcode = NULL) {
static $map = array();
//GL: set language later
/*
if (!isset($langcode)) {
global $language;
$langcode = $language->language;
}
*/
$bank = $ord >> 8;
if (!isset($map[$bank][$langcode])) {
$file = './resources/transliteration-data/' . sprintf('x%02x', $bank) . '.php';
if (file_exists($file)) {
$base = array();
$variant = array();
include $file;
if ($langcode != 'en' && isset($variant[$langcode])) {
// Merge in language specific mappings.
$map[$bank][$langcode] = $variant[$langcode] + $base;
}
else {
$map[$bank][$langcode] = $base;
}
}
else {
$map[$bank][$langcode] = array();
}
}
$ord = $ord & 255;
return isset($map[$bank][$langcode][$ord]) ? $map[$bank][$langcode][$ord] : $unknown;
}