Skip to content
Permalink
Browse files

Fixing bug in String::insert that produces unexpected results with

multiple keys that start with the same substring. Fixes #984

Signed-off-by: mark_story <mark@mark-story.com>
  • Loading branch information...
d1rk authored and markstory committed Aug 3, 2010
1 parent 4ca3266 commit ea11b2aa1eec4bb25cb21e95df20b9b91898b083
Showing with 23 additions and 2 deletions.
  1. +2 −1 cake/libs/string.php
  2. +21 −1 cake/tests/cases/libs/string.test.php
@@ -240,6 +240,7 @@ function insert($str, $data, $options = array()) {
$hashKeys = array_map('md5', array_keys($data));
$tempData = array_combine(array_keys($data), array_values($hashKeys));
krsort($tempData);
foreach ($tempData as $key => $hashVal) {
$key = sprintf($format, preg_quote($key, '/'));
$str = preg_replace($key, $hashVal, $str);
@@ -323,4 +324,4 @@ function cleanInsert($str, $options) {
return $str;
}
}
?>
?>
@@ -197,6 +197,26 @@ function testInsert() {
$result = String::insert('?-pended result', array('Pre'));
$expected = "Pre-pended result";
$this->assertEqual($result, $expected);
$string = 'switching :timeout / :timeout_count';
$expected = 'switching 5 / 10';
$result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
$this->assertEqual($result, $expected);
$string = 'switching :timeout / :timeout_count';
$expected = 'switching 5 / 10';
$result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
$this->assertEqual($result, $expected);
$string = 'switching :timeout_count by :timeout';
$expected = 'switching 10 by 5';
$result = String::insert($string, array('timeout' => 5, 'timeout_count' => 10));
$this->assertEqual($result, $expected);
$string = 'switching :timeout_count by :timeout';
$expected = 'switching 10 by 5';
$result = String::insert($string, array('timeout_count' => 10, 'timeout' => 5));
$this->assertEqual($result, $expected);
}
/**
* test Clean Insert
@@ -253,4 +273,4 @@ function testTokenize() {
$this->assertEqual($expected, $result);
}
}
?>
?>

0 comments on commit ea11b2a

Please sign in to comment.
You can’t perform that action at this time.