From 88321feeb17e271cbaa582dd2507f3591f65f5f7 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Mon, 14 Nov 2011 18:52:12 +0000 Subject: faster it::replace, tests --- it.class | 15 ++++++++++----- tests/it.t | 4 ++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/it.class b/it.class index 772364a..3055cb0 100644 --- a/it.class +++ b/it.class @@ -397,12 +397,17 @@ static function replace($replacements, $string, $p = array()) $patterns = array(); foreach (array_keys($replacements) as $pattern) - $patterns[] = it::convertregex($pattern, $p); + $patterns[] = !preg_match('/\\\\[wb]|[!\x80-\xff]|\[\[:/i', $pattern) && !$p ? "!$pattern!i" : it::convertregex($complex = $pattern, $p); - $oldlocale = setlocale(LC_CTYPE, 0); - setlocale(LC_CTYPE, 'de_CH'); - $result = preg_replace($patterns, array_values($replacements), $string, isset($p['limit']) ? $p['limit'] : -1); - setlocale(LC_CTYPE, $oldlocale); + if (!$complex) + $result = preg_replace($patterns, array_values($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); + setlocale(LC_CTYPE, $oldlocale); + } return $result; } diff --git a/tests/it.t b/tests/it.t index 721fbf7..7b95b7e 100755 --- a/tests/it.t +++ b/tests/it.t @@ -210,6 +210,10 @@ is( "test all=>1,pattern_order=>1" ); +is(it::replace(array('\w' => "x"), "oö"), "xx"); +is(it::replace(array('\w' => "x", '#' => "!"), "#ö"), "!x"); +is(it::replace(array('#' => "!", '\w' => "x"), "#ö"), "!x"); +is(it::replace(array('ö' => "x"), "Ö"), "x"); # it::filter_keys tests -- cgit v1.2.3