Skip to content

Commit

Permalink
Merge pull request danielstjules#137 from danielstjules/mbstring
Browse files Browse the repository at this point in the history
Fix danielstjules#134: Improve support without mbstring
  • Loading branch information
danielstjules committed May 2, 2016
2 parents bd90918 + 30d1742 commit fcc2969
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ integer, boolean, etc.
```php
use Stringy\StaticStringy as S;

// Translates to Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
// Translates to Stringy::create('fòôbàř')->slice(0, 3);
// Returns a Stringy object with the string "fòô"
S::slice('fòôbàř', 0, 3, 'UTF-8');
S::slice('fòôbàř', 0, 3);
```

## Class methods
Expand All @@ -255,7 +255,7 @@ then returns the initialized object. Throws an InvalidArgumentException
if the first argument is an array or object without a __toString method.

```php
$stringy = S::create('fòôbàř', 'UTF-8'); // 'fòôbàř'
$stringy = S::create('fòôbàř'); // 'fòôbàř'
```

## Instance Methods
Expand Down Expand Up @@ -420,7 +420,7 @@ s('fòôbàř')->first(3); // 'fòô'
Returns the encoding used by the Stringy object.

```php
s('fòôbàř', 'UTF-8')->getEncoding(); // 'UTF-8'
s('fòôbàř')->getEncoding(); // 'UTF-8'
```

##### hasLowerCase()
Expand Down
100 changes: 80 additions & 20 deletions src/Stringy.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,14 @@ public function dasherize()
*/
public function delimit($delimiter)
{
// Save current regex encoding so we can reset it after
$regexEncoding = \mb_regex_encoding();
\mb_regex_encoding($this->encoding);
$regexEncoding = $this->regexEncoding();
$this->regexEncoding($this->encoding);

$str = \mb_ereg_replace('\B([A-Z])', '-\1', $this->trim());
$str = $this->eregReplace('\B([A-Z])', '-\1', $this->trim());
$str = \mb_strtolower($str, $this->encoding);
$str = \mb_ereg_replace('[-_\s]+', $delimiter, $str);
$str = $this->eregReplace('[-_\s]+', $delimiter, $str);

\mb_regex_encoding($regexEncoding);
$this->regexEncoding($regexEncoding);

return static::create($str, $this->encoding);
}
Expand Down Expand Up @@ -692,7 +691,7 @@ public function length()
*/
public function lines()
{
$array = \mb_split('[\r\n]{1,2}', $this->str);
$array = $this->split('[\r\n]{1,2}', $this->str);
for ($i = 0; $i < count($array); $i++) {
$array[$i] = static::create($array[$i], $this->encoding);
}
Expand Down Expand Up @@ -983,11 +982,11 @@ public function prepend($string)
*/
public function regexReplace($pattern, $replacement, $options = 'msr')
{
$regexEncoding = \mb_regex_encoding();
\mb_regex_encoding($this->encoding);
$regexEncoding = $this->regexEncoding();
$this->regexEncoding($this->encoding);

$str = \mb_ereg_replace($pattern, $replacement, $this->str, $options);
\mb_regex_encoding($regexEncoding);
$str = $this->eregReplace($pattern, $replacement, $this->str, $options);
$this->regexEncoding($regexEncoding);

return static::create($str, $this->encoding);
}
Expand Down Expand Up @@ -1214,19 +1213,30 @@ public function split($pattern, $limit = null)
}

// mb_split errors when supplied an empty pattern in < PHP 5.4.13
// and current versions of HHVM (3.8 and below)
// and HHVM < 3.8
if ($pattern === '') {
return array(static::create($this->str, $this->encoding));
}

$regexEncoding = \mb_regex_encoding();
\mb_regex_encoding($this->encoding);
$regexEncoding = $this->regexEncoding();
$this->regexEncoding($this->encoding);

// mb_split returns the remaining unsplit string in the last index when
// supplying a limit
$limit = ($limit > 0) ? $limit += 1 : -1;
$array = \mb_split($pattern, $this->str, $limit);
\mb_regex_encoding($regexEncoding);

static $functionExists;
if ($functionExists === null) {
$functionExists = function_exists('\mb_split');
}

if ($functionExists) {
$array = \mb_split($pattern, $this->str, $limit);
} else if ($this->supportsEncoding()) {
$array = \preg_split("/$pattern/", $this->str, $limit);
}

$this->regexEncoding($regexEncoding);

if ($limit > 0 && count($array) === $limit) {
array_pop($array);
Expand Down Expand Up @@ -1632,7 +1642,8 @@ protected function charsArray()
'', 'і', 'ї', 'и', '', '', '', 'ည်', 'ǐ', '',
'', 'ی'),
'j' => array('ĵ', 'ј', 'Ј', '', 'ج'),
'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', '', '', 'ک'),
'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', '', '',
'ک'),
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', '', ''),
'm' => array('м', 'μ', 'م', '', ''),
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', '',
Expand Down Expand Up @@ -1795,12 +1806,61 @@ private function applyPadding($left = 0, $right = 0, $padStr = ' ')
*/
private function matchesPattern($pattern)
{
$regexEncoding = \mb_regex_encoding();
\mb_regex_encoding($this->encoding);
$regexEncoding = $this->regexEncoding();
$this->regexEncoding($this->encoding);

$match = \mb_ereg_match($pattern, $this->str);
\mb_regex_encoding($regexEncoding);
$this->regexEncoding($regexEncoding);

return $match;
}

/**
* Alias for mb_ereg_replace with a fallback to preg_replace if the
* mbstring module is not installed.
*/
private function eregReplace($pattern, $replacement, $string, $option = 'msr')
{
static $functionExists;
if ($functionExists === null) {
$functionExists = function_exists('\mb_split');
}

if ($functionExists) {
return \mb_ereg_replace($pattern, $replacement, $string, $option);
} else if ($this->supportsEncoding()) {
$option = str_replace('r', '', $option);
return \preg_replace("/$pattern/u$option", $replacement, $string);
}
}

/**
* Alias for mb_regex_encoding which default to a noop if the mbstring
* module is not installed.
*/
private function regexEncoding()
{
static $functionExists;

if ($functionExists === null) {
$functionExists = function_exists('\mb_regex_encoding');
}

if ($functionExists) {
$args = func_get_args();
return call_user_func_array('\mb_regex_encoding', $args);
}
}

private function supportsEncoding()
{
$supported = array('UTF-8' => true, 'ASCII' => true);

if (isset($supported[$this->encoding])) {
return true;
} else {
throw new \RuntimeExpception('Stringy method requires the ' .
'mbstring module for encodings other than ASCII and UTF-8');
}
}
}

0 comments on commit fcc2969

Please sign in to comment.