Skip to content

Commit

Permalink
more optimizations for issue #7.
Browse files Browse the repository at this point in the history
  • Loading branch information
prataprc committed Jun 3, 2018
1 parent 547e4d5 commit 27df9b2
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/lib.rs
Expand Up @@ -926,22 +926,28 @@ fn each_split_within(desc: &str, lim: usize) -> Vec<String> {
}).0;

let mut row = String::new();
for word in words.iter().filter(|word| word.len() > 0) {
let mut width = row.width() + word.width();
if row.len() > 0 { width += " ".width(); }
for word in words.iter() {
let sep = if row.len() > 0 { Some(" ") } else { None };
let width = row.width()
+ word.width()
+ sep.map(UnicodeWidthStr::width).unwrap_or(0);

if width <= lim {
if row.len() > 0 { row.push_str(" ") }
if let Some(sep) = sep { row.push_str(sep) }
row.push_str(word);
continue
}
rows.push(row.trim().to_string());
row = String::new();
if row.len() > 0 {
rows.push(row.clone());
row.clear();
}
row.push_str(word);
}
rows.push(row.trim().to_string());
if row.len() > 0 {
rows.push(row);
}
}
rows.iter().filter(|row| row.len() > 0 )
.map(|row| row.to_string()).collect()
rows
}

#[test]
Expand Down

0 comments on commit 27df9b2

Please sign in to comment.