Merge pull request #824 from dogmatic69/string-simplify-code

simplify the code in the string class
This commit is contained in:
Mark Story 2012-09-12 11:49:40 -07:00
commit 0d7db89a43

View file

@ -72,10 +72,6 @@ class String {
} elseif ($node !== '127.0.0.1') { } elseif ($node !== '127.0.0.1') {
$node = ip2long($node); $node = ip2long($node);
} else { } else {
$node = null;
}
if (empty($node)) {
$node = crc32(Configure::read('Security.salt')); $node = crc32(Configure::read('Security.salt'));
} }
@ -92,12 +88,10 @@ class String {
} }
list($timeMid, $timeLow) = explode(' ', microtime()); list($timeMid, $timeLow) = explode(' ', microtime());
$uuid = sprintf( return sprintf(
"%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff, "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
); );
return $uuid;
} }
/** /**
@ -156,7 +150,6 @@ class String {
$open = true; $open = true;
} else { } else {
$depth--; $depth--;
$open = false;
} }
} }
} }
@ -171,11 +164,10 @@ class String {
} }
if (!empty($results)) { if (!empty($results)) {
$data = array_map('trim', $results); return array_map('trim', $results);
} else {
$data = array();
} }
return $data;
return array();
} }
/** /**
@ -227,25 +219,25 @@ class String {
$str = substr_replace($str, $val, $pos, 1); $str = substr_replace($str, $val, $pos, 1);
} }
return ($options['clean']) ? String::cleanInsert($str, $options) : $str; return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
} else { }
asort($data);
$hashKeys = array(); asort($data);
foreach ($data as $key => $value) {
$hashKeys[] = crc32($key);
}
$tempData = array_combine(array_keys($data), array_values($hashKeys)); $hashKeys = array();
krsort($tempData); foreach ($data as $key => $value) {
foreach ($tempData as $key => $hashVal) { $hashKeys[] = crc32($key);
$key = sprintf($format, preg_quote($key, '/')); }
$str = preg_replace($key, $hashVal, $str);
} $tempData = array_combine(array_keys($data), array_values($hashKeys));
$dataReplacements = array_combine($hashKeys, array_values($data)); krsort($tempData);
foreach ($dataReplacements as $tmpHash => $tmpValue) { foreach ($tempData as $key => $hashVal) {
$tmpValue = (is_array($tmpValue)) ? '' : $tmpValue; $key = sprintf($format, preg_quote($key, '/'));
$str = str_replace($tmpHash, $tmpValue, $str); $str = preg_replace($key, $hashVal, $str);
} }
$dataReplacements = array_combine($hashKeys, array_values($data));
foreach ($dataReplacements as $tmpHash => $tmpValue) {
$tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
$str = str_replace($tmpHash, $tmpValue, $str);
} }
if (!isset($options['format']) && isset($options['before'])) { if (!isset($options['format']) && isset($options['before'])) {
@ -397,14 +389,14 @@ class String {
} }
return preg_replace($replace, $with, $text); return preg_replace($replace, $with, $text);
} else {
$phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
} }
$phrase = '(' . preg_quote($phrase, '|') . ')';
if ($html) {
$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
}
return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
} }
/** /**
@ -447,9 +439,9 @@ class String {
if (mb_strlen($text) <= $length) { if (mb_strlen($text) <= $length) {
return $text; return $text;
} else {
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
} }
$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
if (!$exact) { if (!$exact) {
$spacepos = mb_strpos($truncate, ' '); $spacepos = mb_strpos($truncate, ' ');
$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos)); $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
@ -542,9 +534,8 @@ class String {
} else { } else {
if (mb_strlen($text) <= $length) { if (mb_strlen($text) <= $length) {
return $text; return $text;
} else {
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
} }
$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
} }
if (!$exact) { if (!$exact) {
$spacepos = mb_strrpos($truncate, ' '); $spacepos = mb_strrpos($truncate, ' ');
@ -642,9 +633,8 @@ class String {
public static function toList($list, $and = 'and', $separator = ', ') { public static function toList($list, $and = 'and', $separator = ', ') {
if (count($list) > 1) { if (count($list) > 1) {
return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list); return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
} else {
return array_pop($list);
} }
}
return array_pop($list);
}
} }