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
90
91
92
93
/*!
This crate provides left-padding for strings (including both `&str` and `String`).

Import with `extern crate left_pad;`.

Usage example:

```
use left_pad::{leftpad, leftpad_with};

assert_eq!(leftpad("blübb", 7), "  blübb");
assert_eq!(leftpad_with("blübb", 7, '→'), "→→blübb");

let s: String = "blübb".to_owned();
assert_eq!(leftpad(s, 7), "  blübb");
```
*/

#![deny(missing_docs)]

use std::borrow::Cow;
use std::borrow::Borrow;

/// Pads a string to the given number of chars by inserting the character `pad_char` from the left.
///
/// If the given string has more than or exactly the desired number of codepoints, it will be
/// returned as-is.
///
/// Codepoints are not graphemes, so the result might always not be what a human would expect.
///
/// # Examples
///
/// ```
/// use left_pad::leftpad_with;
///
/// assert_eq!(leftpad_with("blübb", 7, ' '), "  blübb");
/// assert_eq!(leftpad_with("blübb", 7, '→'), "→→blübb");
///
/// assert_eq!(leftpad_with("blübb", 5, ' '), "blübb");
/// assert_eq!(leftpad_with("blübb", 3, ' '), "blübb");
///
/// assert_eq!(leftpad_with("čömbiñiñg märks", 22, ' '),  "  čömbiñiñg märks");
/// ```
pub fn leftpad_with<'a, S>(string: S, codepoints: usize, pad_char: char) -> Cow<'a, str>
    where S: Into<Cow<'a, str>>
{
    let cow = string.into();

    let cow_codepoints = cow.chars().count();
    if codepoints <= cow_codepoints {
        return cow;
    }

    let to_pad = codepoints - cow_codepoints;
    let mut padded = String::with_capacity(cow.len() + to_pad);

    for _ in 0..to_pad {
        padded.push(pad_char);
    }

    padded.push_str(cow.borrow());

    padded.into()
}

/// Pads a string to the given number of chars by inserting spaces from the left.
///
/// If the given string has more than or exactly the desired number of codepoints, it will be
/// returned as-is.
///
/// Codepoints are not graphemes, so the result might not always be what a human would expect.
///
/// This function is equal to calling `leftpad_with(string, codepoints, ' ')`.
///
/// # Examples
///
/// ```
/// use left_pad::{leftpad,leftpad_with};
///
/// assert_eq!(leftpad("blübb", 7), "  blübb");
///
/// assert_eq!(leftpad("blübb", 5), "blübb");
/// assert_eq!(leftpad("blübb", 3), "blübb");
///
/// assert_eq!(leftpad("blübb", 7), leftpad_with("blübb", 7, ' '));
///
/// assert_eq!(leftpad("čömbiñiñg märks", 22),  "  čömbiñiñg märks");
/// ```
pub fn leftpad<'a, S>(string: S, codepoints: usize) -> Cow<'a, str>
    where S: Into<Cow<'a, str>>
{
    leftpad_with(string, codepoints, ' ')
}