Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Added Zend\Stdlib\StringUtils::hasPcreUnicodeSupport()
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Jan 19, 2013
1 parent fe7cfad commit 4913c64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions library/Zend/Stdlib/StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Zend\Stdlib;

use Zend\Stdlib\ErrorHandler;
use Zend\Stdlib\StringWrapper\StringWrapperInterface;

/**
Expand Down Expand Up @@ -45,6 +46,13 @@ abstract class StringUtils
// TODO
);

/**
* Is PCRE compiled with Unicode support?
*
* @var bool
**/
protected static $hasPcreUnicodeSupport = null;

/**
* Get registered wrapper classes
*
Expand Down Expand Up @@ -167,4 +175,19 @@ public static function isValidUtf8($str)
{
return is_string($str) && ($str === '' || preg_match('/^./su', $str) == 1);
}

/**
* Is PCRE compiled with Unicode support?
*
* @return bool
*/
public static function hasPcreUnicodeSupport()
{
if (static::$hasPcreUnicodeSupport === null) {
ErrorHandler::start();
static::$hasPcreUnicodeSupport = defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
ErrorHandler::stop();
}
return static::$hasPcreUnicodeSupport;
}
}
10 changes: 10 additions & 0 deletions tests/ZendTest/Stdlib/StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\Stdlib;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Stdlib\ErrorHandler;
use Zend\Stdlib\StringUtils;

class StringUtilsTest extends TestCase
Expand Down Expand Up @@ -147,4 +148,13 @@ public function testIsValidUtf8($str, $valid)
{
$this->assertSame($valid, StringUtils::isValidUtf8($str));
}

public function testHasPcreUnicodeSupport()
{
ErrorHandler::start();
$expected = defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
ErrorHandler::stop();

$this->assertSame($expected, StringUtils::hasPcreUnicodeSupport());
}
}

0 comments on commit 4913c64

Please sign in to comment.