mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 07:44:12 +02:00
Merge pull request #21 from Bilge/dry-regex-replace
Optimized replace() and collapseWhitespace() by reusing regexReplace().
This commit is contained in:
@@ -383,13 +383,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function collapseWhitespace()
|
public function collapseWhitespace()
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
return $this->regexReplace('[[:space:]]+', ' ')->trim();
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace('[[:space:]]+', ' ', $this->str);
|
|
||||||
mb_regex_encoding($regexEncoding);
|
|
||||||
|
|
||||||
return self::create($str, $this->encoding)->trim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1267,17 +1261,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function replace($search, $replacement)
|
public function replace($search, $replacement)
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
return $this->regexReplace(preg_quote($search), $replacement);
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
// Don't want the args being parsed as regex
|
|
||||||
$search = preg_quote($search);
|
|
||||||
$replacement = preg_quote($replacement);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace($search, $replacement, $this->str);
|
|
||||||
mb_regex_encoding($regexEncoding);
|
|
||||||
|
|
||||||
return self::create($str, $this->encoding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -835,6 +835,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
array('foo', '', '', 'foo'),
|
array('foo', '', '', 'foo'),
|
||||||
array('foo', '\s', '\s', 'foo'),
|
array('foo', '\s', '\s', 'foo'),
|
||||||
array('foo bar', 'foo bar', '', ''),
|
array('foo bar', 'foo bar', '', ''),
|
||||||
|
array('foo bar', 'foo bar', 'f(o)o', '\1'),
|
||||||
|
array('\1 bar', 'foo bar', 'foo', '\1'),
|
||||||
array('bar', 'foo bar', 'foo ', ''),
|
array('bar', 'foo bar', 'foo ', ''),
|
||||||
array('far bar', 'foo bar', 'foo', 'far'),
|
array('far bar', 'foo bar', 'foo', 'far'),
|
||||||
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
||||||
@@ -853,6 +855,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
array('', '', '', ''),
|
array('', '', '', ''),
|
||||||
array('bar', 'foo', 'f[o]+', 'bar'),
|
array('bar', 'foo', 'f[o]+', 'bar'),
|
||||||
|
array('o bar', 'foo bar', 'f(o)o', '\1'),
|
||||||
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
|
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
|
||||||
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
||||||
array('', '', '', '', 'msr', 'UTF-8'),
|
array('', '', '', '', 'msr', 'UTF-8'),
|
||||||
|
Reference in New Issue
Block a user