summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2011-11-15 14:41:34 +0000
committerUrban Müller2011-11-15 14:41:34 +0000
commit62a80d4a07941b1783dcbd3151f33f8f2c3b9cfa (patch)
tree2ff2c8228e9b37624d84f5e15ded53662ecd47eb
parent88321feeb17e271cbaa582dd2507f3591f65f5f7 (diff)
downloaditools-62a80d4a07941b1783dcbd3151f33f8f2c3b9cfa.tar.gz
itools-62a80d4a07941b1783dcbd3151f33f8f2c3b9cfa.tar.bz2
itools-62a80d4a07941b1783dcbd3151f33f8f2c3b9cfa.zip
faster it::replace
-rw-r--r--it.class8
-rwxr-xr-xtests/it.t3
2 files changed, 6 insertions, 5 deletions
diff --git a/it.class b/it.class
index 3055cb0..dd3a118 100644
--- a/it.class
+++ b/it.class
@@ -394,18 +394,16 @@ static function match($pattern, $string, $p = null)
*/
static function replace($replacements, $string, $p = array())
{
- $patterns = array();
-
- foreach (array_keys($replacements) as $pattern)
+ foreach ($replacements as $pattern => $dummy)
$patterns[] = !preg_match('/\\\\[wb]|[!\x80-\xff]|\[\[:/i', $pattern) && !$p ? "!$pattern!i" : it::convertregex($complex = $pattern, $p);
if (!$complex)
- $result = preg_replace($patterns, array_values($replacements), $string);
+ $result = preg_replace($patterns, $replacements, $string);
else
{
$oldlocale = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'de_CH');
- $result = preg_replace($patterns, array_values($replacements), $string, isset($p['limit']) ? $p['limit'] : -1);
+ $result = preg_replace($patterns, $replacements, $string, isset($p['limit']) ? $p['limit'] : -1);
setlocale(LC_CTYPE, $oldlocale);
}
diff --git a/tests/it.t b/tests/it.t
index 7b95b7e..75062b6 100755
--- a/tests/it.t
+++ b/tests/it.t
@@ -210,7 +210,10 @@ is(
"test all=>1,pattern_order=>1"
);
+is(it::replace(array('a' => "1", 'b' => "2"), "ab"), "12");
+is(it::replace(array('!' => "x"), "!"), "x");
is(it::replace(array('\w' => "x"), "oö"), "xx");
+is(it::replace(array('[[:alpha:]]' => "x"), "ö"), "x");
is(it::replace(array('\w' => "x", '#' => "!"), "#ö"), "!x");
is(it::replace(array('#' => "!", '\w' => "x"), "#ö"), "!x");
is(it::replace(array('ö' => "x"), "Ö"), "x");