Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Using native str_pad #244

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading