-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCharUtils.php
More file actions
429 lines (385 loc) · 12.3 KB
/
CharUtils.php
File metadata and controls
429 lines (385 loc) · 12.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* FlorianWolters\Component\Core\CharUtils
*
* PHP Version 5.3
*
* @author Florian Wolters <wolters.fl@gmail.com>
* @copyright 2010-2014 Florian Wolters (http://blog.florianwolters.de)
* @license https://gnu.org/licenses/lgpl.txt LGPL-3.0+
* @link https://github.com/FlorianWolters/PHP-Component-Core-StringUtils
*/
namespace FlorianWolters\Component\Core;
use \InvalidArgumentException;
/**
* The class {@see CharUtils} offers operations on characters.
*
* This class is inspired by the Java class {@link
* https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/CharUtils.html
* CharUtils} from the {@link https://commons.apache.org/lang Apache Commons
* Lang Application Programming Interface (API)}.
*
* @since Class available since Release 0.2.0
*/
final class CharUtils
{
/**
* The carriage return (CR) character ("\r").
*
* @var string
*/
const CR = "\r";
/**
* The linefeed (LF) character ("\n").
*
* @var string
*/
const LF = "\n";
// @codeCoverageIgnoreStart
/**
* {@see CharUtils} instances can **NOT** be constructed in standard
* programming.
*
* Instead, the class should be used as:
*
* CharUtils::isAscii('a');
*
*/
protected function __construct()
{
// NOOP
}
// @codeCoverageIgnoreEnd
/**
* Returns all ASCII 7 bit characters in an array.
*
* @return string[] All ASCII 7 bit characters.
*/
public static function charAsciiArray()
{
static $result = array();
if (!$result) {
$result = \array_merge(
self::charAsciiControlArray(),
self::charAsciiPrintableArray()
);
}
return $result;
}
/**
* Returns all ASCII 7 bit alphabetic characters in an array.
*
* @return string[] All ASCII 7 bit alphabetic characters.
*/
public static function charAsciiAlphaArray()
{
static $result = array();
if (!$result) {
$result = \array_merge(
self::charAsciiAlphaLowerArray(),
self::charAsciiAlphaUpperArray()
);
}
return $result;
}
/**
* Returns all ASCII 7 bit alphabetic lowercase characters in an array.
*
* @return string[] All ASCII 7 bit alphabetic lowercase characters.
*/
public static function charAsciiAlphaLowerArray()
{
static $result = array();
if (!$result) {
for ($i = 97; $i < 123; ++$i) {
$result[] = \chr($i);
}
}
return $result;
}
/**
* Returns all ASCII 7 bit alphabetic uppercase characters in an array.
*
* @return string[] All ASCII 7 bit alphabetic uppercase characters.
*/
public static function charAsciiAlphaUpperArray()
{
static $result = array();
if (!$result) {
for ($i = 65; $i < 91; ++$i) {
$result[] = \chr($i);
}
}
return $result;
}
/**
* Returns all ASCII 7 bit alphanumeric characters in an array.
*
* @return string[] All ASCII 7 bit alphanumeric characters.
*/
public static function charAsciiAlphanumericArray()
{
static $result = array();
if (null === $result) {
$result = \array_merge(
self::charAsciiNumericArray(),
self::charAsciiAlphaArray()
);
}
return $result;
}
/**
* Returns all ASCII 7 bit control characters in an array.
*
* @return string[] All ASCII 7 bit control characters.
*/
public static function charAsciiControlArray()
{
static $result = array();
if (!$result) {
for ($i = 0; $i < 32; ++$i) {
$result[] = \chr($i);
}
$result[] = \chr(127);
}
return $result;
}
/**
* Returns all ASCII 7 bit printable characters in an array.
*
* @return string[] All ASCII 7 bit printable characters.
*/
public static function charAsciiPrintableArray()
{
static $result = array();
if (!$result) {
for ($i = 32; $i < 127; ++$i) {
$result[] = \chr($i);
}
}
return $result;
}
/**
* Returns all ASCII 7 bit numeric characters in an array.
*
* @return string[] All ASCII 7 bit numeric characters.
*/
public static function charAsciiNumericArray()
{
static $result = array();
if (!$result) {
for ($i = 48; $i < 58; ++$i) {
$result[] = (string) ($i - 48);
}
}
return $result;
}
/**
* Checks whether the specified character is ASCII 7 bit.
*
* CharUtils::isAscii('a'); // true
* CharUtils::isAscii('A'); // true
* CharUtils::isAscii('3'); // true
* CharUtils::isAscii('-'); // true
* CharUtils::isAscii('\n'); // true
* CharUtils::isAscii('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if the character is ASCII 7 bit; `false`
* otherwise.
*/
public static function isAscii($char)
{
return (true === \is_string($char))
&& (1 === \preg_match('/^[[:ascii:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit alphabetic.
*
* CharUtils::isAsciiAlpha('a'); // true
* CharUtils::isAsciiAlpha('A'); // true
* CharUtils::isAsciiAlpha('3'); // false
* CharUtils::isAsciiAlpha('-'); // false
* CharUtils::isAsciiAlpha('\n'); // false
* CharUtils::isAsciiAlpha('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if the character is ASCII 7 bit alphabetic;
* `false` otherwise.
*/
public static function isAsciiAlpha($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:alpha:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit alphabetic lowercase.
*
* CharUtils::isAsciiAlphaLower('a'); // true
* CharUtils::isAsciiAlphaLower('A'); // false
* CharUtils::isAsciiAlphaLower('3'); // false
* CharUtils::isAsciiAlphaLower('-'); // false
* CharUtils::isAsciiAlphaLower('\n'); // false
* CharUtils::isAsciiAlphaLower('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if the character is ASCII 7 bit alphabetic
* lowercase; `false` otherwise.
*/
public static function isAsciiAlphaLower($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:lower:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit alphabetic uppercase.
*
* CharUtils::isAsciiAlphaUpper('a'); // false
* CharUtils::isAsciiAlphaUpper('A'); // true
* CharUtils::isAsciiAlphaUpper('3'); // false
* CharUtils::isAsciiAlphaUpper('-'); // false
* CharUtils::isAsciiAlphaUpper('\n'); // false
* CharUtils::isAsciiAlphaUpper('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if the character is ASCII 7 bit alphabetic
* uppercase; `false` otherwise.
*/
public static function isAsciiAlphaUpper($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:upper:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit numeric.
*
* CharUtils::isAsciiAlphanumeric('a'); // true
* CharUtils::isAsciiAlphanumeric('A'); // true
* CharUtils::isAsciiAlphanumeric('3'); // true
* CharUtils::isAsciiAlphanumeric('-'); // false
* CharUtils::isAsciiAlphanumeric('\n'); // false
* CharUtils::isAsciiAlphanumeric('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if the character is ASCII 7 bit numeric; `false`
* otherwise.
*/
public static function isAsciiAlphanumeric($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:alnum:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit control.
*
* CharUtils::isAsciiControl('a'); // false
* CharUtils::isAsciiControl('A'); // false
* CharUtils::isAsciiControl('3'); // false
* CharUtils::isAsciiControl('-'); // false
* CharUtils::isAsciiControl('\n'); // true
* CharUtils::isAsciiControl('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if `$char` is less than the ASCII code `32` or
* equal to the ASCII code `127`; `false` otherwise.
*/
public static function isAsciiControl($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:cntrl:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit numeric.
*
* CharUtils::isAsciiNumeric('a'); // false
* CharUtils::isAsciiNumeric('A'); // false
* CharUtils::isAsciiNumeric('3'); // true
* CharUtils::isAsciiNumeric('-'); // false
* CharUtils::isAsciiNumeric('\n'); // false
* CharUtils::isAsciiNumeric('©'); // false
*
* @param string $char The character to check.
*
* @return boolean `true` if `$char` is one of the ASCII codes between `48`
* and `57` inclusive: `false` otherwise.
*/
public static function isAsciiNumeric($char)
{
return (true === self::isAscii($char))
&& (1 === \preg_match('/^[[:digit:]]$/', $char));
}
/**
* Checks whether the character is ASCII 7 bit printable.
*
* CharUtils::isAsciiPrintable('a'); // true
* CharUtils::isAsciiPrintable('A'); // true
* CharUtils::isAsciiPrintable('3'); // true
* CharUtils::isAsciiPrintable('-'); // true
* CharUtils::isAsciiPrintable('\n'); // false
* CharUtils::isAsciiPrintable('©'); // false
*
* @param string $char The character to check.
*
* @return true if between 32 and 126 inclusive
*/
public static function isAsciiPrintable($char)
{
return (true === self::isAscii($char))
&& (false === self::isAsciiControl($char));
}
/**
* Returns the ASCII character of the specified ASCII value.
*
* @param integer $char The ASCII value.
*
* @return string The ASCII character.
* @throws InvalidArgumentException If `$int` is not an ASCII value.
*/
public static function fromAsciiValueToChar($int)
{
if (false === self::isAsciiValue($int)) {
throw new \InvalidArgumentException(
'The specified argument is not an ASCII value.'
);
}
return \chr($int);
}
/**
* Checks whether the specified integer is the value for an ASCII 7 bit
* character.
*
* @param integer $int The integer to check.
*
* @return boolean `true` whether the integer is an ASCII value; `false`
* otherwise.
*/
private static function isAsciiValue($int)
{
return (true === \is_int($int))
&& ($int > -1)
&& ($int < 128);
}
/**
* Returns the ASCII value of the specified ASCII character.
*
* @param string $char The ASCII character.
*
* @return integer The ASCII value.
* @throws InvalidArgumentException If `$char` is not an ASCII character.
*/
public static function fromAsciiCharToValue($char)
{
if (false === self::isAscii($char)) {
throw new \InvalidArgumentException(
'The specified argument is not an ASCII character.'
);
}
return \ord($char);
}
}