Initial commit

This commit is contained in:
Chris 2016-03-17 00:11:31 +01:00
commit dea0fc5228
5 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vendor

22
README.md Normal file
View File

@ -0,0 +1,22 @@
noccylabs/unicode
=================
This library adds support for writing unicode characters to the console. It will
register a number of global functions for this purpose:
* `uchr($numeric)` - Returns the unicode character matching the numeric argument.
* `uprintf($fmt, $arg..)` - A wrapper for `printf` that enables `\u` to embed
unicode strings.
* `usprintf($fmt, $arg..)` - A wrapper like `uprintf` but for `sprintf`.
## Examples
To output a specific UTF-8 glyph:
echo uchr(0x1000)
The same thing in a printf fashion:
uprintf("Unicode glyph x1000: \u1000");

18
composer.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "noccylabs/unicode",
"description": "Functions to output unicode characters to the console",
"type": "library",
"license": "GPL-3.0",
"authors": [
{
"name": "Christopher Vagnetoft",
"email": "cvagnetoft@gmail.com"
}
],
"require": {},
"autoload": {
"files": [
"lib/unicode.php"
]
}
}

13
examples/unicode.php Normal file
View File

@ -0,0 +1,13 @@
<?php
require_once __DIR__."/../vendor/autoload.php";
echo uchr(0x25B6);
uprintf("Hello \u1000 \"world\"\n");
uprintf("\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2589 \u2591\u2592\u2593\u2589\n");
uprintf("\u2610 Unchecked box\n");
uprintf("\u2611 Checked box\n");
uprintf("\u2612 Crossed out box\n");

37
lib/unicode.php Normal file
View File

@ -0,0 +1,37 @@
<?php
class UnicodeShim
{
public static function bind()
{
static $init;
if ($init++) return;
function uchr($ord)
{
return json_decode('"'.'\u'.dechex($ord).'"');
}
function uprintf($arg)
{
$str = call_user_func_array('sprintf', func_get_args());
$str = preg_replace_callback("/(\\\\u[0-9a-f]{1,4})/i", function ($chr) {
return json_decode('"'.$chr[1].'"');
}, $str);
echo $str;
}
function usprintf($arg)
{
$str = call_user_func_array('sprintf', func_get_args());
$str = preg_replace_callback("/(\\\\u[0-9a-f]{1,4})/i", function ($chr) {
return json_decode('"'.$chr[1].'"');
}, $str);
return $str;
}
}
}
UnicodeShim::bind();