Skip to content

Commit

Permalink
refactor: using native str_pad
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Nov 13, 2023
1 parent 8eb5f2c commit f611bfd
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions src/System/Text/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,32 +374,6 @@ public static function template(string $template, array $data, string $open_deli
return \str_replace($keys, $data, $template);
}

/**
* Fill string with string if length is less.
*
* @param string $text String Text
* @param string $fill String fill for miss length
* @param int $max_length max length of output string
* @param bool $fill_start If true filling from start
*
* @return string
*/
private static function fillText(string $text, string $fill, int $max_length, bool $fill_start)
{
$max_length = $max_length < \strlen($text) ? \strlen($text) : $max_length;
$deviation = $max_length - \strlen($text);

if (0 === $deviation) {
return $text;
}

$prefix = \str_repeat($fill, $deviation);

return $fill_start
? $prefix . $text
: $text . $prefix;
}

/**
* Fill string (start) with string if length is less.
*
Expand All @@ -411,7 +385,7 @@ private static function fillText(string $text, string $fill, int $max_length, bo
*/
public static function fill(string $text, string $fill, int $max_length)
{
return static::fillText($text, $fill, $max_length, true);
return \str_pad($text, $max_length, $fill, STR_PAD_LEFT);
}

/**
Expand All @@ -425,7 +399,7 @@ public static function fill(string $text, string $fill, int $max_length)
*/
public static function fillEnd(string $text, string $fill, int $max_length)
{
return static::fillText($text, $fill, $max_length, false);
return \str_pad($text, $max_length, $fill, STR_PAD_RIGHT);
}

/**
Expand Down

0 comments on commit f611bfd

Please sign in to comment.