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

pre-allocate the buffer, including a correctness test #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ pub fn left_pad(s: &str, pad: usize) -> String
/// If str.len() is not 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();
// pad_len is the number of padchar that actually need to be put before the string.
let pad_len = pad.saturating_sub(s.len());
let required_capacity = s.as_bytes().len() + (pad_len as usize) * padchar.len_utf8();
let mut out = String::with_capacity(required_capacity);

let len = s.len();
if pad > len {
for _ in 0..pad-len {
out.push(padchar);
}
for _ in 0..pad_len {
out.push(padchar);
}
out.push_str(s);
// Assert a correct prediction of the required capacity.
debug_assert_eq!(out.as_bytes().len(), required_capacity);
// Assert no further allocation was done.
debug_assert_eq!(out.capacity(), required_capacity);

out
}
Expand All @@ -36,5 +40,6 @@ fn pad_test() {
assert_eq!(left_pad("foo", 5), " foo");
assert_eq!(left_pad_char("foo", 7, 'X'), "XXXXfoo");
assert_eq!(left_pad_char("bar", 5, '-'), "--bar");
assert_eq!(left_pad_char("rust", 7, '\u{2764}'), "❤❤❤rust");
}