Merge pull request #114 from hacksaar/master

fix utils::prefix bug: #113
This commit is contained in:
Alexandre Bury 2017-03-06 10:27:47 -08:00 committed by GitHub
commit b18ca17b73

View File

@ -50,14 +50,16 @@ pub fn prefix<'a, I>(iter: I, available_width: usize, delimiter: &str) -> Prefix
let mut current_width = 0; let mut current_width = 0;
let sum = iter.take_while(|token| { let sum = iter.take_while(|token| {
let width = token.width(); let needed_width = if current_width == 0 {
if current_width + width > available_width { token.width()
} else {
// We also need to insert the delimiter, if not at the beginning.
token.width() + delimiter_width
};
if current_width + needed_width > available_width {
false false
} else { } else {
if current_width != 0 { current_width += needed_width;
current_width += delimiter_width;
}
current_width += width;
true true
} }
}) })
@ -66,7 +68,9 @@ pub fn prefix<'a, I>(iter: I, available_width: usize, delimiter: &str) -> Prefix
// We counted delimiter once too many times, // We counted delimiter once too many times,
// but only if the iterator was non empty. // but only if the iterator was non empty.
let length = if sum == 0 { sum } else { sum - delimiter_len }; let length = sum.saturating_sub(delimiter_len);
debug_assert!(current_width <= available_width);
Prefix { Prefix {
length: length, length: length,