Skip to content

Commit

Permalink
Fixing bug in String::insert that produces unexpected results with
Browse files Browse the repository at this point in the history
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 4, 2010
1 parent 4ca3266 commit ea11b2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cake/libs/string.php
Expand Up @@ -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);
Expand Down Expand Up @@ -323,4 +324,4 @@ function cleanInsert($str, $options) {
return $str;
}
}
?>
?>
22 changes: 21 additions & 1 deletion cake/tests/cases/libs/string.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -253,4 +273,4 @@ function testTokenize() {
$this->assertEqual($expected, $result);
}
}
?>
?>

0 comments on commit ea11b2a

Please sign in to comment.