forked from muglug/psl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pad_right.php
33 lines (26 loc) · 933 Bytes
/
pad_right.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
declare(strict_types=1);
namespace Psl\Str\Byte;
use Psl;
use function str_pad;
use const STR_PAD_RIGHT;
/**
* Returns the string padded to the total length by appending the `$pad_string`
* to the right.
*
* If the length of the input string plus the pad string exceeds the total
* length, the pad string will be truncated. If the total length is less than or
* equal to the length of the input string, no padding will occur.
*
* @param non-empty-string $pad_string
*
* @pure
*
* @throws Psl\Exception\InvariantViolationException If $pad_string is empty, or $total_length is negative.
*/
function pad_right(string $string, int $total_length, string $pad_string = ' '): string
{
Psl\invariant('' !== $pad_string, 'Expected a non-empty pad string.');
Psl\invariant($total_length >= 0, 'Expected a non-negative total length.');
return str_pad($string, $total_length, $pad_string, STR_PAD_RIGHT);
}