Safe str_pad alternative

@param   string $string
@param   int $padLen
@param   string $padStr
@param   int $padType
@return  string
964443
protected static function strPad($input, $padLen, $padStr = ' ', $padType = STR_PAD_RIGHT){
    $diff = strlen($input) - static::strLen($input);
    return str_pad($input, $padLen + $diff, $padStr, $padType);
  }
336756
protected function pkcs5_unpad($string)
    {
        $pad = ord($string{strlen($string)-1});
        if ($pad > strlen($string)) {
            return false;
        }

        if (strspn($string, chr($pad), strlen($string) - $pad) != $pad) {
            return false;
        }

        return substr($string, 0, -1 * $pad);
    }
19429
public function strPad($input, $padLength, $padString = ' ', $padType = STR_PAD_RIGHT)
    {
        if (StringUtils::isSingleByteEncoding($this->getEncoding())) {
            return str_pad($input, $padLength, $padString, $padType);
        }

        $lengthOfPadding = $padLength - $this->strlen($input);
        if ($lengthOfPadding <= 0) {
            return $input;
        }

        $padStringLength = $this->strlen($padString);
        if ($padStringLength === 0) {
            return $input;
        }

        $repeatCount = floor($lengthOfPadding / $padStringLength);

        if ($padType === STR_PAD_BOTH) {
            $repeatCountLeft = $repeatCountRight = ($repeatCount - $repeatCount % 2) / 2;

            $lastStringLength       = $lengthOfPadding - 2 * $repeatCountLeft * $padStringLength;
            $lastStringLeftLength   = $lastStringRightLength = floor($lastStringLength / 2);
            $lastStringRightLength += $lastStringLength % 2;

            $lastStringLeft  = $this->substr($padString, 0, $lastStringLeftLength);
            $lastStringRight = $this->substr($padString, 0, $lastStringRightLength);

            return str_repeat($padString, $repeatCountLeft) . $lastStringLeft
                . $input
                . str_repeat($padString, $repeatCountRight) . $lastStringRight;
        }

        $lastString = $this->substr($padString, 0, $lengthOfPadding % $padStringLength);

        if ($padType === STR_PAD_LEFT) {
            return str_repeat($padString, $repeatCount) . $lastString . $input;
        }

        return $input . str_repeat($padString, $repeatCount) . $lastString;
    }
939461
public function padStringFilter($value, $padCharacter, $maxLength, $padType = 'STR_PAD_RIGHT')
    {
        if ($this->isNullOrEmptyString($padCharacter)) {
            throw new \InvalidArgumentException('Pad String Filter cannot accept a null value or empty string as its first argument');
        }
        if ( ! is_int($maxLength)) {
            throw new \InvalidArgumentException('Pad String Filter expects its second argument to be an integer');
        }
        $diff = (function_exists('mb_strlen'))? strlen($value) - mb_strlen($value) : 0;
        $padType = constant($padType);

        return str_pad($value, $maxLength + $diff, $padCharacter, $padType);
    }
300064
public static function str_pad(&$str, int $pad_length, $pad_string=' ', int $pad_type=\STR_PAD_RIGHT) {
		cast::string($str, true);
		cast::string($pad_string, true);

		$current_length = v_mb::strlen($str);
		$pad_string_length = v_mb::strlen($pad_string);

		if ($pad_length <= $current_length || ! $pad_string_length) {
			return;
		}

		// Pad left.
		if (\STR_PAD_LEFT === $pad_type) {
			$str = \str_repeat($pad_string, \ceil(($pad_length - $current_length) / $pad_string_length)) . $str;
			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				$str = v_mb::substr($str, $new_length - $pad_length, null);
			}
		}
		// Pad both.
		elseif (\STR_PAD_BOTH === $pad_type) {
			$leftright = 'right';
			while (v_mb::strlen($str) < $pad_length) {
				$leftright = 'left' === $leftright ? 'right' : 'left';
				if ('left' === $leftright) {
					$str = "{$pad_string}{$str}";
				}
				else {
					$str .= $pad_string;
				}
			}

			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				if ('left' === $leftright) {
					$str = v_mb::substr($str, $new_length - $pad_length, null);
				}
				else {
					$str = v_mb::substr($str, 0, $pad_length);
				}
			}
		}
		// Pad right.
		else {
			$str .= \str_repeat($pad_string, \ceil(($pad_length - $current_length) / $pad_string_length));
			$new_length = v_mb::strlen($str);
			if ($new_length > $pad_length) {
				$str = v_mb::substr($str, 0, $pad_length);
			}
		}
	}
914298
public static function padBoth(string $str, int $length, string $with = ' ', string $encoding = null) : string
    {
        return static::pad($str, $length, $with, STR_PAD_BOTH, $encoding);
    }
827219
public static function padRight(
        $string,
        $length = 0,
        $substring = ' ',
        $encoding = null
    ) {
        $encoding = $encoding === null ? mb_internal_encoding() : $encoding;

        return static::doPad($string, 0, $length - Mbstring::strlen($string, $encoding), $substring, $encoding);
    }
1262101
public function padRight( $length, $padString ) {
		return new GString( str_pad( $this->value, $length, $padString, STR_PAD_RIGHT ) );
	}
348974
public function padRight($length, $padString)
    {
        if (!$this->isNumber($length)) {
            throw new StringObjectException(StringObjectException::MSG_INVALID_ARG, [
                '$length',
                'integer'
            ]);
        }

        if (!$this->isString($padString)) {
            throw new StringObjectException(StringObjectException::MSG_INVALID_ARG, [
                '$padString',
                'string'
            ]);
        }

        $this->val(str_pad($this->val(), $length, $padString, STR_PAD_RIGHT));

        return $this;
    }
777842
private static function addPKCS7Padding($str, $blocksize)
	{
		$len = strlen($str);
		$pad = $blocksize - ($len % $blocksize);
		return $str . str_repeat(chr($pad), $pad);
	}
853236
public function padLeft($length, $padString)
    {
        if (!$this->isNumber($length)) {
            throw new StringObjectException(StringObjectException::MSG_INVALID_ARG, [
                '$length',
                'integer'
            ]);
        }

        if (!$this->isString($padString)) {
            throw new StringObjectException(StringObjectException::MSG_INVALID_ARG, [
                '$padString',
                'string'
            ]);
        }

        $this->val(str_pad($this->val(), $length, $padString, STR_PAD_LEFT));

        return $this;
    }
503780
public static function safeStrcpy(string $string): string
    {
        $length = Binary::safeStrlen($string);
        $return = '';
        /** @var int $chunk */
        $chunk = $length >> 1;
        if ($chunk < 1) {
            $chunk = 1;
        }
        for ($i = 0; $i < $length; $i += $chunk) {
            $return .= Binary::safeSubstr($string, $i, $chunk);
        }
        return $return;
    }
905843
private function pad(
        int $length,
        string $character = ' ',
        int $direction = self::PAD_RIGHT
    ): self {
        return new self(\str_pad(
            $this->value,
            $length,
            $character,
            $direction
        ), $this->encoding);
    }
1131158
function pad(str, width) {
  var len = Math.max(0, width - str.length);
  return str + Array(len + 1).join(' ');
}
1285733
function pad( str, chr, n ) {
  return (chr.repeat( Math.max( n - str.length, 0 ) ) + str).substr(-n)
}
869645
function(str, len, padChar) {
    str = stringIt(str);
    if (str.length >= len) {
      return str;
    }
    var padStr = getPadding(padChar, len);
    return str + padStr.substr(str.length - len);
  }
122233
public static String pad(String s, int length)
  {
    if (s.length() < length)
    {
      StringBuffer a = new StringBuffer(length);
      a.append(s);
      for (int i = s.length(); i < length; i++)
      {
        a = a.append(" ");
      }
      return a.toString();
    }
    else if (s.length() > length)
    {
      return s.substring(0, length);
    }
    else
    {
      return s;
    }
  }