-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENHANCEMENT] - Better unicode matching (#35)
- Loading branch information
Showing
5 changed files
with
166 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
// build ordered list of codepoints | ||
$shortcodes = require(__DIR__ . '/../src/shortcodes-array.php'); | ||
|
||
$codepoints = []; | ||
foreach ($shortcodes as $hexcodes) { | ||
foreach (explode('-', $hexcodes) as $hexcode) { | ||
$codepoints[] = hexdec($hexcode); | ||
} | ||
} | ||
|
||
sort($codepoints); | ||
|
||
// convert codepoints to UTF-8 and build structured list | ||
$utf8 = []; | ||
foreach ($codepoints as $dec) { | ||
$chr = mb_chr($dec, 'UTF-8'); | ||
$hex = unpack('H*', $chr); | ||
$chars = array_chunk(str_split(reset($hex)), 2); | ||
$bytes = array_map('implode', $chars); | ||
|
||
foreach (range(0, 3) as $padding) { | ||
if ($padding >= count($bytes)) { | ||
array_unshift($bytes, '00'); | ||
} | ||
} | ||
|
||
$arr = &$utf8; | ||
foreach ($bytes as $offset => $byte) { | ||
if ($offset === 3) { | ||
$arr[] = $byte; | ||
continue; | ||
} | ||
|
||
if (!isset($arr[$byte])) { | ||
$arr[$byte] = []; | ||
} | ||
|
||
$arr = &$arr[$byte]; | ||
} | ||
} | ||
|
||
// build simplified regex | ||
$expressions = []; | ||
foreach ($utf8 as $firstByte => $secondBytes) { | ||
foreach ($secondBytes as $secondByte => $thirdBytes) { | ||
$first = $last = null; | ||
$little = []; | ||
|
||
foreach ($thirdBytes as $thirdByte => $fourthBytes) { | ||
if (!$first) { | ||
$first = $last = hexdec($thirdByte); | ||
$little = $fourthBytes; | ||
continue; | ||
} | ||
|
||
if ((hexdec($thirdByte) - $last) > 1) { | ||
sort($little); | ||
|
||
$littleFirst = array_shift($little); | ||
$littleLast = array_pop($little); | ||
|
||
if (!$littleLast) { | ||
$littleLast = $littleFirst; | ||
} | ||
|
||
$expression = sprintf( | ||
'\x%s\x%s%s%s', | ||
strtoupper($firstByte), | ||
strtoupper($secondByte), | ||
$first === $last ? sprintf('\x%s', strtoupper(dechex($first))) : sprintf('[\x%s-\x%s]', strtoupper(dechex($first)), strtoupper(dechex($last))), | ||
$littleFirst === $littleLast ? sprintf('\x%s', strtoupper($littleFirst)) : sprintf('[\x%s-\x%s]', strtoupper($littleFirst), strtoupper($littleLast)), | ||
); | ||
|
||
$expression = preg_replace('/^(\\\\x00)+/', '', $expression); | ||
$expressions[] = $expression; | ||
|
||
$first = $last = hexdec($thirdByte); | ||
$little = []; | ||
} | ||
|
||
$last = hexdec($thirdByte); | ||
|
||
foreach ($fourthBytes as $fourthByte) { | ||
$little[] = $fourthByte; | ||
} | ||
} | ||
|
||
sort($little); | ||
|
||
$littleFirst = array_shift($little); | ||
$littleLast = array_pop($little); | ||
|
||
if (!$littleLast) { | ||
$littleLast = $littleFirst; | ||
} | ||
|
||
$expression = sprintf( | ||
'\x%s\x%s%s%s', | ||
strtoupper($firstByte), | ||
strtoupper($secondByte), | ||
$first === $last ? sprintf('\x%s', strtoupper(dechex($first))) : sprintf('[\x%s-\x%s]', strtoupper(dechex($first)), strtoupper(dechex($last))), | ||
$littleFirst === $littleLast ? sprintf('\x%s', strtoupper($littleFirst)) : sprintf('[\x%s-\x%s]', strtoupper($littleFirst), strtoupper($littleLast)), | ||
); | ||
|
||
$expression = preg_replace('/^(\\\\x00)+/', '', $expression); | ||
$expressions[] = $expression; | ||
} | ||
} | ||
|
||
$match = sprintf('/(%s)/x', implode(PHP_EOL . '|', $expressions)); | ||
file_put_contents(__DIR__ . '/../src/unicode-patterns.php', sprintf("<?php\nreturn '%s';", $match)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
return '/(\xC2[\xA9-\xAE] | ||
|\xE2[\x80-\x81][\x89-\xBC] | ||
|\xE2[\x83-\x84][\xA2-\xB9] | ||
|\xE2\x86[\x94-\xAA] | ||
|\xE2\x8C[\x9A-\xA8] | ||
|\xE2\x8F[\x8F-\xBA] | ||
|\xE2\x93\x82 | ||
|\xE2[\x96-\x9E][\x80-\xBF] | ||
|\xE2\xA4[\xB4-\xB5] | ||
|\xE2[\xAC-\xAD][\x85-\x9C] | ||
|\xE3\x80[\xB0-\xBD] | ||
|\xE3\x8A[\x97-\x99] | ||
|\xEF\xB8\x8F | ||
|\xF0\x9F\x80\x84 | ||
|\xF0\x9F\x83\x8F | ||
|\xF0\x9F[\x85-\x89][\x81-\xBF] | ||
|\xF0\x9F[\x8C-\x9B][\x80-\xBF] | ||
|\xF0\x9F\x9F[\xA0-\xB0] | ||
|\xF0\x9F[\xA4-\xA7][\x80-\xBF] | ||
|\xF0\x9F[\xA9-\xAB][\x80-\xBC] | ||
|\xF3\xA0\x81[\xA2-\xBF])/x'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters