-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEntityTools.php
More file actions
236 lines (223 loc) · 6.04 KB
/
Copy pathEntityTools.php
File metadata and controls
236 lines (223 loc) · 6.04 KB
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
<?php declare(strict_types=1);
/**
* Tools module.
*
* Copyright 2024 Daniil Gentili.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2024 Daniil Gentili <daniil@daniil.it>
* @license https://opensource.org/license/apache-2-0 Apache 2.0
* @link https://github.com/danog/telegram-entities TelegramEntities documentation
*/
namespace danog\TelegramEntities;
use Webmozart\Assert\Assert;
/**
* Telegram UTF-16 styled text entity tools.
*
* @api
*/
final class EntityTools
{
// @codeCoverageIgnoreStart
/**
* @psalm-suppress UnusedConstructor
*
* @internal Can only be used statically.
*/
private function __construct()
{
}
// @codeCoverageIgnoreEnd
/**
* Get length of string in UTF-16 code points.
*
* @param string $text Text
*/
public static function mbStrlen(string $text): int
{
$length = 0;
$textlength = \strlen($text);
for ($x = 0; $x < $textlength; $x++) {
$char = \ord($text[$x]);
if (($char & 0xc0) != 0x80) {
$length += 1 + ($char >= 0xf0 ? 1 : 0);
}
}
return $length;
}
/**
* Telegram UTF-16 multibyte substring.
*
* @param string $text Text to substring
* @param integer $offset Offset
* @param null|int $length Length
*/
public static function mbSubstr(string $text, int $offset, ?int $length = null): string
{
/** @var string */
$converted = \mb_convert_encoding($text, 'UTF-16');
/** @var string */
return \mb_convert_encoding(
\substr(
$converted,
$offset<<1,
$length === null ? null : ($length<<1),
),
'UTF-8',
'UTF-16',
);
}
/**
* Telegram UTF-16 multibyte split.
*
* @param string $text Text
* @param integer<0, max> $length Length
* @return list<string>
*/
public static function mbStrSplit(string $text, int $length): array
{
$result = [];
/** @var string */
$text = \mb_convert_encoding($text, 'UTF-16');
/** @psalm-suppress ArgumentTypeCoercion */
foreach (\str_split($text, $length<<1) as $chunk) {
$chunk = \mb_convert_encoding($chunk, 'UTF-8', 'UTF-16');
Assert::string($chunk);
$result []= $chunk;
}
/** @var list<string> */
return $result;
}
/**
* Telegram UTF-16 multibyte subreplace.
*
* @param string $string Text
* @param string $replace Replacement
* @param integer $offset Offset
* @param null|int $length Length
* @return string The result string is returned. If string is an array then array is returned.
*/
public static function mbSubstrReplace(
string $string,
string $replace,
int $offset,
int|null $length = null
): string {
/** @var string */
$string = \mb_convert_encoding($string, 'UTF-16');
/** @var string */
$replace = \mb_convert_encoding($replace, 'UTF-16');
/** @var string */
return \mb_convert_encoding(
\substr_replace(
$string,
$replace,
$offset << 1,
$length === null ? null : ($length << 1),
),
'UTF-8',
'UTF-16',
);
}
/**
* Escape string for this library's HTML entity converter.
*
* @param string $what String to escape
*/
public static function htmlEscape(string $what): string
{
return \htmlspecialchars($what, ENT_QUOTES|ENT_SUBSTITUTE|ENT_XML1);
}
/**
* Escape string for markdown.
*
* @param string $what String to escape
*/
public static function markdownEscape(string $what): string
{
return \str_replace(
[
'\\',
'_',
'*',
'[',
']',
'(',
')',
'~',
'`',
'>',
'#',
'+',
'-',
'=',
'|',
'{',
'}',
'.',
'!',
],
[
'\\\\',
'\\_',
'\\*',
'\\[',
'\\]',
'\\(',
'\\)',
'\\~',
'\\`',
'\\>',
'\\#',
'\\+',
'\\-',
'\\=',
'\\|',
'\\{',
'\\}',
'\\.',
'\\!',
],
$what
);
}
/**
* Escape string for markdown codeblock.
*
* @param string $what String to escape
*/
public static function markdownCodeblockEscape(string $what): string
{
return \str_replace('```', '\\```', $what);
}
/**
* Escape string for markdown code section.
*
* @param string $what String to escape
*/
public static function markdownCodeEscape(string $what): string
{
return \str_replace('`', '\\`', $what);
}
/**
* Escape string for URL.
*
* @param string $what String to escape
*/
public static function markdownUrlEscape(string $what): string
{
return \str_replace(')', '\\)', $what);
}
}