phpeggy
Version:
PHP target for Peggy parser generator
45 lines (41 loc) • 1.56 kB
JavaScript
"use strict";
module.exports = function(
phpGlobalNamePrefixOrNamespaceEscaped
) {
return [
"/* BEGIN Utility functions */",
"/* chr_unicode - get unicode character from its char code */",
`if (!\\function_exists("${phpGlobalNamePrefixOrNamespaceEscaped}chr_unicode")) {`,
" /** @param float|int $code */",
" function chr_unicode(",
" $code",
" ): string {",
' return \\html_entity_decode("&#" . (int) $code . ";", ENT_QUOTES, "UTF-8");',
" }",
"}",
"",
"/* ord_unicode - get unicode char code from string */",
`if (!\\function_exists("${phpGlobalNamePrefixOrNamespaceEscaped}ord_unicode")) {`,
" function ord_unicode(",
" string $character",
" ): int {",
" if (\\strlen($character) === 1) {",
" return \\ord($character);",
" }",
" $json = \\json_encode($character, \\JSON_THROW_ON_ERROR);",
" $utf16_1 = (int) \\hexdec(\\substr($json, 3, 4));",
// A character inside the BMP has a JSON representation like "\uXXXX".
// A character outside the BMP looks like "\uXXXX\uXXXX".
' if (substr($json, 7, 2) === "\\u") {',
// Outside the BMP. Math from https://stackoverflow.com/a/6240819
" $utf16_2 = (int) \\hexdec(\\substr($json, 9, 4));",
" return 0x10000 + (($utf16_1 & 0x3ff) << 10) + ($utf16_2 & 0x3ff);",
" }",
"",
" return $utf16_1;",
" }",
"}",
"/* END Utility functions */",
"",
];
};