Permalink
Browse files

Improving performance of String::insert().

  • Loading branch information...
1 parent 1795733 commit d38e50805d471d8ab953b99035c117a3cd0b4b82 @jperras jperras committed Oct 7, 2009
Showing with 12 additions and 14 deletions.
  1. +12 −14 cake/libs/string.php
View
@@ -225,6 +225,7 @@ function insert($str, $data, $options = array()) {
);
$options += $defaults;
$format = $options['format'];
+ $data = (array)$data;
if (!isset($format)) {
$format = sprintf(
@@ -234,42 +235,39 @@ function insert($str, $data, $options = array()) {
str_replace('%', '%%', preg_quote($options['after'], '/'))
);
}
- if (!is_array($data)) {
- $data = array($data);
- }
- if (array_keys($data) === array_keys(array_values($data))) {
+ if (strpos($str, '?') !== false) {
$offset = 0;
while (($pos = strpos($str, '?', $offset)) !== false) {
$val = array_shift($data);
$offset = $pos + strlen($val);
$str = substr_replace($str, $val, $pos, 1);
}
+ return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
} else {
asort($data);
- $hashKeys = array_map('md5', array_keys($data));
+ $hashKeys = array();
+ foreach ($data as $key => $value) {
+ $hashKeys[] = crc32($key);
+ }
+
$tempData = array_combine(array_keys($data), array_values($hashKeys));
foreach ($tempData as $key => $hashVal) {
$key = sprintf($format, preg_quote($key, '/'));
$str = preg_replace($key, $hashVal, $str);
}
$dataReplacements = array_combine($hashKeys, array_values($data));
- foreach ($dataReplacements as $tmpHash => $data) {
- if (is_array($data)) {
- $data = '';
- }
- $str = str_replace($tmpHash, $data, $str);
+ foreach ($dataReplacements as $tmpHash => $tmpValue) {
+ $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
+ $str = str_replace($tmpHash, $tmpValue, $str);
}
}
if (!isset($options['format']) && isset($options['before'])) {
$str = str_replace($options['escape'].$options['before'], $options['before'], $str);
}
- if (!$options['clean']) {
- return $str;
- }
- return String::cleanInsert($str, $options);
+ return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
}
/**

0 comments on commit d38e508

Please sign in to comment.