Skip to content
Browse files

StringUtils: optimations

  • Loading branch information...
1 parent 5f42457 commit 1c82b00e14041bcfa050b66bd15c66f646435839 @marc-mabe committed Nov 29, 2012
Showing with 23 additions and 26 deletions.
  1. +23 −26 library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php
View
49 library/Zend/Stdlib/StringWrapper/AbstractStringWrapper.php
@@ -75,7 +75,7 @@ public function convert($str, $toEncoding, $fromEncoding = 'UTF-8')
* @param string $break
* @param boolean $cut
* @param string $encoding
- * @return string
+ * @return string|false
*/
public function wordWrap($string, $width = 75, $break = "\n", $cut = false, $encoding = 'UTF-8')
{
@@ -168,41 +168,38 @@ public function strPad($input, $padLength, $padString = ' ', $padType = \STR_PAD
return str_pad($input, $padLength, $padString, $padType);
}
- $return = '';
$lengthOfPadding = $padLength - $this->strlen($input, $encoding);
$padStringLength = $this->strlen($padString, $encoding);
if ($padStringLength === 0 || $lengthOfPadding <= 0) {
- $return = $input;
- } else {
- $repeatCount = floor($lengthOfPadding / $padStringLength);
+ return $input;
+ }
- if ($padType === \STR_PAD_BOTH) {
- $lastStringLeft = '';
- $lastStringRight = '';
- $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
+ $repeatCount = floor($lengthOfPadding / $padStringLength);
- $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
- $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
- $lastStringRightLength += $lastStringLength % 2;
+ if ($padType === \STR_PAD_BOTH) {
+ $lastStringLeft = '';
+ $lastStringRight = '';
+ $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;
- $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding);
- $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding);
+ $lastStringLength = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
+ $lastStringLeftLength = $lastStringRightLength = floor($lastStringLength / 2);
+ $lastStringRightLength += $lastStringLength % 2;
- $return = str_repeat($padString, $repeatCountLeft) . $lastStringLeft
- . $input
- . str_repeat($padString, $repeatCountRight) . $lastStringRight;
- } else {
- $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding);
+ $lastStringLeft = $this->substr($padString, 0, $lastStringLeftLength, $encoding);
+ $lastStringRight = $this->substr($padString, 0, $lastStringRightLength, $encoding);
- if ($padType === \STR_PAD_LEFT) {
- $return = str_repeat($padString, $repeatCount) . $lastString . $input;
- } else {
- $return = $input . str_repeat($padString, $repeatCount) . $lastString;
- }
+ return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
+ . $input
+ . str_repeat($padString, $repeatCountRight) . $lastStringRight;
+ } else {
+ $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength, $encoding);
+
+ if ($padType === \STR_PAD_LEFT) {
+ return str_repeat($padString, $repeatCount) . $lastString . $input;
+ } else {
+ return $input . str_repeat($padString, $repeatCount) . $lastString;
}
}
-
- return $return;
}
}

0 comments on commit 1c82b00

Please sign in to comment.
Something went wrong with that request. Please try again.