Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

dev release

  • Loading branch information...
commit 3993991f39050d4c00a99b262f708d8c7bd80dcb 0 parents
heartsentwined authored
3  .gitignore
@@ -0,0 +1,3 @@
+.git
+composer.lock
+vendor/
10 .travis.yml
@@ -0,0 +1,10 @@
+language: php
+php:
+ - "5.3"
+ - "5.4"
+before_script: composer install
+script: phpunit --configuration test/phpunit.xml
+notifications:
+ email:
+ on_success: always
+ on_failure: always
33 README.md
@@ -0,0 +1,33 @@
+# Heartsentwined\Utf8
+
+[![Build Status](https://secure.travis-ci.org/heartsentwined/utf8.png)](http://travis-ci.org/heartsentwined/utf8)
+
+A collection of utf8-related functions.
+
+# Installation
+
+[Composer](http://getcomposer.org/):
+
+```json
+{
+ "require": {
+ "heartsentwined/utf8": "1.*"
+ }
+}
+```
+
+# Usage
+
+Utf-8 version of [chr](http://php.net/manual/en/function.chr.php):
+
+```php
+use Heartsentwined\Utf8\Utf8;
+$char = Utf8::uchr($num);
+```
+
+Utf-8 version of [ord](http://php.net/manual/en/function.ord.php):
+
+```php
+use Heartsentwined\Utf8\Utf8;
+$num = Utf8::uord($char);
+```
23 composer.json
@@ -0,0 +1,23 @@
+{
+ "name": "heartsentwined/utf8",
+ "description": "A collection of utf8-related functions",
+ "license": "GPL-3.0",
+ "keywords": ["utf8", "utf-8"],
+ "homepage": "https://github.com/heartsentwined/utf8",
+ "authors": [
+ {
+ "name": "heartsentwined",
+ "email": "heartsentwined@cogito-lab.com",
+ "role": "Developer"
+ }
+ ],
+ "autoload": {
+ "psr-0": {
+ "Heartsentwined\\Utf8": "src/"
+ }
+ },
+ "require": {
+ "php": ">=5.3.3",
+ "heartsentwined/arg-validator": "1.*"
+ }
+}
43 src/Heartsentwined/Utf8/Utf8.php
@@ -0,0 +1,43 @@
+<?php
+namespace Heartsentwined\Utf8;
+
+use Heartsentwined\ArgValidator\ArgValidator;
+
+class Utf8
+{
+ /**
+ * UTF-8 version of chr()
+ *
+ * @param int|float|string $num ('numeric')
+ * @return string
+ */
+ public static function uchr($num)
+ {
+ ArgValidator::assert($num, 'numeric');
+
+ if($num<=0x7F) return chr($num);
+ if($num<=0x7FF) return chr(($num>>6)+192).chr(($num&63)+128);
+ if($num<=0xFFFF) return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);
+ if($num<=0x1FFFFF) return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128).chr(($num&63)+128);
+ return '';
+ }
+
+ /**
+ * UTF-8 version of ord()
+ *
+ * @param string $c
+ * @return int
+ */
+ public static function uord($c)
+ {
+ ArgValidator::assert($c, 'string');
+
+ if (empty($c)) return false;
+
+ $ord0 = ord($c{0}); if ($ord0>=0 && $ord0<=127) return $ord0;
+ $ord1 = ord($c{1}); if ($ord0>=192 && $ord0<=223) return ($ord0-192)*64 + ($ord1-128);
+ $ord2 = ord($c{2}); if ($ord0>=224 && $ord0<=239) return ($ord0-224)*4096 + ($ord1-128)*64 + ($ord2-128);
+ $ord3 = ord($c{3}); if ($ord0>=240 && $ord0<=247) return ($ord0-240)*262144 + ($ord1-128)*4096 + ($ord2-128)*64 + ($ord3-128);
+ return false;
+ }
+}
42 test/Heartsentwined/Test/Utf8/Utf8Test.php
@@ -0,0 +1,42 @@
+<?php
+namespace Heartsentwined\Test\Utf8;
+
+use Heartsentwined\Utf8\Utf8;
+
+class Utf8Test extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ // test on boundaries
+ $this->map = array(
+ 0 => "\0",
+
+ 0x7F => '',
+ 0x80 => '€',
+
+ 0x7FF => '߿',
+ 0x800 => '',
+
+ 0xFFFF => '￿',
+ 0x10000 => '𐀀',
+
+ 0x1000FF => '􀃿',
+ );
+ }
+
+ public function testUchr()
+ {
+ foreach ($this->map as $point => $char) {
+ $this->assertSame($char, Utf8::uchr($point));
+ }
+ $this->assertSame('', Utf8::uchr(0x200000));
+ }
+
+ public function testUord()
+ {
+ foreach ($this->map as $point => $char) {
+ $this->assertSame($point, Utf8::uord($char));
+ }
+ $this->assertSame(false, Utf8::uord(''));
+ }
+}
3  test/bootstrap.php
@@ -0,0 +1,3 @@
+<?php
+$loader = require_once __DIR__ . '/../vendor/autoload.php';
+$loader->add('Heartsentwined\Test', __DIR__);
7 test/phpunit.xml
@@ -0,0 +1,7 @@
+<phpunit bootstrap="./bootstrap.php" colors="true">
+ <testsuites>
+ <testsuite>
+ <directory>./</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
Please sign in to comment.
Something went wrong with that request. Please try again.