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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use Write;
/// Pads a string on the left with a specified character to reach a given length.
///
/// # Arguments
/// * `str` - The string to be padded.
/// * `len` - The total length of the resulting string.
/// * `chr` - The character used for padding.
///
/// # Returns
/// A new `String` that is the padded string.
///
/// # Examples
///
/// ```
/// use pad_left::left_pad;
///
/// assert_eq!(left_pad("".to_string(), 0, ' '), "");
/// assert_eq!(left_pad("".to_string(), 10, ' '), " ");
/// assert_eq!(left_pad("hello".to_string(), 5, ' '), "hello");
/// assert_eq!(left_pad("hello".to_string(), 10, ' '), " hello");
/// assert_eq!(left_pad("hello".to_string(), 10, '*'), "*****hello");
/// assert_eq!(left_pad("".to_string(), 10, ' '), " ".to_string());
/// assert_eq!(left_pad("hello".to_string(), 0, ' '), "hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 5, ' '), "hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 10, ' '), " hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 15, ' '), " hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 10, '\0'), " hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 15, '\0'), " hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 10, '-'), "-----hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 15, '-'), "----------hello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 10, 'π'), "πππππhello".to_string());
/// assert_eq!(left_pad("hello".to_string(), 15, 'π'), "ππππππππππhello".to_string());
/// ```
///
/// # Complexity
///
/// # Time complexity
/// O(log(n)), where `n` is the length difference between the input string and the desired length.
///
/// # Space complexity
/// O(log(n)), where `n` is the length difference between the input string and the desired length,
/// since we are constructing a new string with the padded characters. However, the actual space
/// used may be less than this if the `ch` parameter is a space character and the length difference
/// is less than 20, in which case we construct a pre-allocated string instead of dynamically creating one.