Skip to content

Commit

Permalink
Fix the behaviour -> version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hfiguiere committed Mar 23, 2016
1 parent 46c6076 commit 1cb52ba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leftpad"
version = "0.1.0"
version = "0.2.0"
authors = ["Hubert Figuière <[email protected]>"]
license = "BSD-2-Clause"
description = "Pad a string to the left"
Expand Down
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0.2.0
- actually implement the proper behaviour

0.1.0
- first release.
24 changes: 15 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@



/// pad a string to the left with ``pad`` spaces
pub fn left_pad(s: &str, pad: u32) -> String
/// pad a string to the left to ``pad`` length with spaces
/// If str.len() is less than pad, then the string is returned verbatim
pub fn left_pad(s: &str, pad: usize) -> String
{
left_pad_char(s, pad, ' ')
}

/// pad a string to the left with ``pad`` ``padchar``
pub fn left_pad_char(s: &str, pad: u32, padchar: char) -> String
/// pad a string to the left to ``pad`` length with ``padchar``
/// If str.len() is less than pad, then the string is returned verbatim
pub fn left_pad_char(s: &str, pad: usize, padchar: char) -> String
{
let mut out = String::new();

for _ in 0..pad {
out.push(padchar);
let len = s.len();
if pad > len {
for _ in 0..pad-len {
out.push(padchar);
}
}
out.push_str(s);

Expand All @@ -26,9 +31,10 @@ pub fn left_pad_char(s: &str, pad: u32, padchar: char) -> String
fn pad_test() {

assert_eq!(left_pad("foo", 0), "foo");
assert_eq!(left_pad("foo", 2), "foo");
assert_eq!(left_pad_char("bar", 0, 'Y'), "bar");
assert_eq!(left_pad("foo", 2), " foo");
assert_eq!(left_pad_char("foo", 2, 'X'), "XXfoo");
assert_eq!(left_pad_char("bar", 5, '-'), "-----bar");
assert_eq!(left_pad("foo", 5), " foo");
assert_eq!(left_pad_char("foo", 7, 'X'), "XXXXfoo");
assert_eq!(left_pad_char("bar", 5, '-'), "--bar");
}

0 comments on commit 1cb52ba

Please sign in to comment.