-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode.libr.php
203 lines (193 loc) · 6.5 KB
/
unicode.libr.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
<?php
/*
This file is part of DevOrSysAdminScripts library.
DevOrSysAdminScripts is free software:
you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License
as published by the Free Software Foundation,
either version 3 of the License,
or (at your option) any later version.
DevOrSysAdminScripts is distributed in the hope
that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of
the GNU Lesser General Public License
along with DevOrSysAdminScripts.
If not, see <https://www.gnu.org/licenses/>.
©Copyright 2023-2024 Laurent Frédéric Bernard François Lyaudet
This file was renamed from "unicode.php" to "unicode.libr.php".
*/
function decimal_code_point_to_UTF8(
$i_code_point_in_decimal_notation
){
// var_dump($i_code_point_in_decimal_notation);
// 0xxxxxxx ASCII
if($i_code_point_in_decimal_notation < 128){
return chr($i_code_point_in_decimal_notation);
}
$i_continuation_base_value = 128;
// 110xxxxx 10xxxxxx
if($i_code_point_in_decimal_notation < 256 * (2**3)){
$i_first_byte_base_value = 192;
$i_last_byte_significant_bits = (
$i_code_point_in_decimal_notation % (2**6)
);
$i_first_byte_significant_bits = (int) (
(
$i_code_point_in_decimal_notation
- $i_last_byte_significant_bits
) / (2**6)
);
$s_result = chr(
$i_first_byte_base_value
+ $i_first_byte_significant_bits
);
$s_result .= chr(
$i_continuation_base_value
+ $i_last_byte_significant_bits
);
return $s_result;
}
// 1110xxxx 10xxxxxx 10xxxxxx
if($i_code_point_in_decimal_notation < 256 * 256){
$i_first_byte_base_value = 224;
$arr_arr_data_per_byte_reverse = [
["i_bits" => 6, "i_base_value" => $i_continuation_base_value],
["i_bits" => 6, "i_base_value" => $i_continuation_base_value],
["i_bits" => 4, "i_base_value" => $i_first_byte_base_value],
];
$i_rest = $i_code_point_in_decimal_notation;
$s_result = "";
foreach($arr_arr_data_per_byte_reverse as $arr_data){
// _Pragma unroll
// J'aurais pu "roller" les 4 cas des ifs.
// Mais pour les perfs, il faudrait tout unroller vu le nombre
// d'itérations. Et j'aimerais bien comparer du code unrollé
// aux 2 niveaux à la main avec celui unrollé par le compilateur
// sur les 2 niveaux. Je préjuge/présuppose qu'il unroll
// l'intérieur d'abord (pour ne pas refaire le même boulot)
// puis l'extérieur et qu'il oublie de réoptimiser l'intérieur
// en fonction de l'extérieur unrollé. Est-ce que j'ai raison ?
$i_significant_bits = $i_rest % (2**$arr_data["i_bits"]);
$i_rest = (int) (
($i_rest - $i_significant_bits) / (2**$arr_data["i_bits"])
);
$s_result = ( // .= or =.
chr($i_significant_bits + $arr_data["i_base_value"])
.$s_result
);
}
return $s_result;
}
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if($i_code_point_in_decimal_notation < 256 * 256 * (2**5)){
$i_first_byte_base_value = 240;
$arr_arr_data_per_byte_reverse = [
["i_bits" => 6, "i_base_value" => $i_continuation_base_value],
["i_bits" => 6, "i_base_value" => $i_continuation_base_value],
["i_bits" => 6, "i_base_value" => $i_continuation_base_value],
["i_bits" => 3, "i_base_value" => $i_first_byte_base_value],
];
$i_rest = $i_code_point_in_decimal_notation;
$s_result = "";
foreach($arr_arr_data_per_byte_reverse as $arr_data){
// _Pragma unroll
$i_significant_bits = $i_rest % (2**$arr_data["i_bits"]);
$i_rest = (int) (
($i_rest - $i_significant_bits) / (2**$arr_data["i_bits"])
);
$s_result = ( // .= or =.
chr($i_significant_bits + $arr_data["i_base_value"])
.$s_result
);
}
return $s_result;
}
throw new Exception(
"UTF-8 avec jusqu'à 6 octets a été abandonné il y a longtemps."
." Autre anecdote \"débile\" : MySQL a de base UTF-8 avec jusqu'à"
." 3 octets au lieu de 4 :face-palm:."
." Du coup, ça peut donner quelques \"surprises\"."
." Sinon, y'a aussi ASCII dans MySQL."
." Et comme ça joue sur la mémoire et les performances,"
." pourquoi ils n'ont pas fait ASCII, UTF-8-2, UTF-8-3, UTF-8-4 ?"
." :)"
);
}
function hexa_code_point_to_UTF8(
$s_code_point_in_hexadecimal_notation
){
$s_code_point_in_hexadecimal_notation = strtoupper(
$s_code_point_in_hexadecimal_notation
);
$i_code_point_in_decimal_notation = 0;
$i_ord_0 = ord('0');
$i_ord_A = ord('A');
$i_digit_offset = -$i_ord_0;
$i_letter_offset = 10 - $i_ord_A;
for(
$i = 0, $i_max = strlen($s_code_point_in_hexadecimal_notation);
$i < $i_max;
++$i
){
$s_hexa_digit = $s_code_point_in_hexadecimal_notation[$i];
$i_code_point_in_decimal_notation *= 16;
if(ctype_digit($s_hexa_digit)){
$i_code_point_in_decimal_notation += (
ord($s_hexa_digit) + $i_digit_offset
);
continue;
}
if(ctype_xdigit($s_hexa_digit)){
$i_code_point_in_decimal_notation += (
ord($s_hexa_digit) + $i_letter_offset
);
continue;
}
throw new Exception("Not an hexadecimal digit.");
}
return decimal_code_point_to_UTF8(
$i_code_point_in_decimal_notation
);
}
/*
?>
<?php
require_once("./unicode.php");
var_dump(hexa_code_point_to_UTF8("002B"));
var_dump(hexa_code_point_to_UTF8("00E6"));
var_dump(hexa_code_point_to_UTF8("1400"));
var_dump(hexa_code_point_to_UTF8("10111"));
// https://en.wikibooks.org/wiki/Unicode/Character_reference/2000-2FFF
// Some invisible unicode characters that are a problem:
// 2000-200F 2028-202F 205F 2060-206F
$arr = [
'2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007',
'2008', '2009', '200A', '200B', '200C', '200D', '200E', '200F',
'2028', '2029', '202A', '202B', '202C', '202D', '202E', '202F',
'205F',
'2060', '2061', '2062', '2063', '2064', '2065', '2066', '2067',
'2068', '2069', '206A', '206B', '206C', '206D', '206E', '206F',
];
$arr_s = [];
$arr_s2 = [];
$arr_s3 = [];
foreach($arr as $s_hexa){
$s = hexa_code_point_to_UTF8($s_hexa);
$arr_s []= $s;
if(strlen($s) === 2){
$s2 = '\x'.bin2hex($s[0]).'\x'.bin2hex($s[1]);
}
if(strlen($s) === 3){
$s2 = '\x'.bin2hex($s[0]).'\x'.bin2hex($s[1])
.'\x'.bin2hex($s[2]);
}
$arr_s2 []= $s2;
$s3 = " | sed -e 's/".$s2."/ /g'\\";
$arr_s3 []= $s3;
echo($s3."\n");
}
//*/