Skip to content

Commit

Permalink
Merge pull request #3 from PHPOffice/develop
Browse files Browse the repository at this point in the history
Version 0.1.1
  • Loading branch information
Progi1984 committed Jul 1, 2015
2 parents 43d19c8 + 2901d27 commit 8b8ae85
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Common/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ public static function numberFormat($number, $decimals)
{
return number_format($number, $decimals, '.', '');
}

/**
* @param int $dec
* @link http://stackoverflow.com/a/7153133/2235790
* @author velcrow
*/
public static function chr($dec)
{
if ($dec<=0x7F) {
return chr($dec);
}
if ($dec<=0x7FF) {
return chr(($dec>>6)+192).chr(($dec&63)+128);
}
if ($dec<=0xFFFF) {
return chr(($dec>>12)+224).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
}
if ($dec<=0x1FFFFF) {
return chr(($dec>>18)+240).chr((($dec>>12)&63)+128).chr((($dec>>6)&63)+128).chr(($dec&63)+128);
}
return '';
}
}
13 changes: 13 additions & 0 deletions tests/Common/Tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ public function testNumberFormat()
$this->assertEquals('2.1', String::numberFormat('2.12', 1));
$this->assertEquals('1234', String::numberFormat(1234, 1));
}

public function testChr()
{
$this->assertEquals('A', String::chr(65));
$this->assertEquals('A', String::chr(0x41));
$this->assertEquals('é', String::chr(233));
$this->assertEquals('é', String::chr(0xE9));
$this->assertEquals('', String::chr(12083));
$this->assertEquals('', String::chr(0x2F33));
$this->assertEquals('🌃', String::chr(127747));
$this->assertEquals('🌃', String::chr(0x1F303));
$this->assertEquals('', String::chr(2097152));
}
}

0 comments on commit 8b8ae85

Please sign in to comment.