Align columns: Difference between revisions

→‎{{header|Rust}}: Updated to Rust 1.2.0.
(→‎{{header|Ruby}}: align String -> Symbol)
(→‎{{header|Rust}}: Updated to Rust 1.2.0.)
Line 4,508:
=={{header|Rust}}==
<lang Rust>use std::iter::{Extend, repeat};
 
use std::str::StrExt;
enum AlignmentType { Left, Center, Right }
 
fn get_column_widths(text: &str) -> Vec<usize> {
let mut widths = Vec::new();
for line in text.lines().map(|s| s.trim_matches(' ').trim_right_matches('$')) {
let lens = line.split('$').map(|s| s.chars().count());
for (idx, len) in lens.enumerate() {
if idx < widths.len() {
widths[idx] = std::cmp::max(widths[idx], len);
}
}
else {
widths.push(len);
}
}
}
}
}
}
widths
}
 
fn align_columns(text: &str, alignment: AlignmentType) -> String {
let widths = get_column_widths(text);
let mut result = String::new();
for line in text.lines().map(|s| s.trim_matches(' ').trim_right_matches('$')) {
for (s, w) in line.split('$').zip(widths.iter()) {
let blank_count = w - s.chars().count();
let (pre, post) = match alignment {
AlignmentType::Left => (0, blank_count),
AlignmentType::Center => (blank_count / 2, (blank_count + 1) / 2),
AlignmentType::Right => (blank_count, 0),
};
};
result.extend(repeat(' ').take(pre));
result.push_str(s);
result.extend(repeat(' ').take(post));
result.push(' ');
}
}
result.push_str("\n");
}
}
result
}
 
fn main() {
let text = r#"Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
Line 4,556 ⟶ 4,555:
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column."#;
 
println!("{}", align_columns(text, AlignmentType::Left));
println!("{}", repeat('-').take(110).collect::<String>());