seq: pass all Busybox tests

This commit is contained in:
Arcterus 2014-06-23 01:34:39 -07:00
parent 9597dca983
commit ac4b3b7103

View file

@ -8,6 +8,7 @@
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
use std::cmp;
use std::os; use std::os;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
@ -161,8 +162,15 @@ pub fn uumain(args: Vec<String>) -> int {
crash!(1, "too {} operands.\nTry '{} --help' for more information.", crash!(1, "too {} operands.\nTry '{} --help' for more information.",
if free.len() < 1 { "few" } else { "many" }, program); if free.len() < 1 { "few" } else { "many" }, program);
} }
let mut largest_dec = 0;
let mut padding = 0;
let first = if free.len() > 1 { let first = if free.len() > 1 {
match parse_float(free.get(0).as_slice()) { let slice = free.get(0).as_slice();
let len = slice.len();
let dec = slice.find('.').unwrap_or(len);
largest_dec = len - dec;
padding = dec;
match parse_float(slice) {
Ok(n) => n, Ok(n) => n,
Err(s) => { show_error!("{:s}", s); return 1; } Err(s) => { show_error!("{:s}", s); return 1; }
} }
@ -170,6 +178,11 @@ pub fn uumain(args: Vec<String>) -> int {
1.0 1.0
}; };
let step = if free.len() > 2 { let step = if free.len() > 2 {
let slice = free.get(1).as_slice();
let len = slice.len();
let dec = slice.find('.').unwrap_or(len);
largest_dec = cmp::max(largest_dec, len - dec);
padding = cmp::max(padding, dec);
match parse_float(free.get(1).as_slice()) { match parse_float(free.get(1).as_slice()) {
Ok(n) => n, Ok(n) => n,
Err(s) => { show_error!("{:s}", s); return 1; } Err(s) => { show_error!("{:s}", s); return 1; }
@ -177,46 +190,63 @@ pub fn uumain(args: Vec<String>) -> int {
} else { } else {
1.0 1.0
}; };
let last = match parse_float(free.get(free.len()-1).as_slice()) { let last = {
Ok(n) => n, let slice = free.get(free.len() - 1).as_slice();
Err(s) => { show_error!("{:s}", s); return 1; } padding = cmp::max(padding, slice.find('.').unwrap_or(slice.len()));
match parse_float(slice) {
Ok(n) => n,
Err(s) => { show_error!("{:s}", s); return 1; }
}
}; };
let separator = escape_sequences(options.separator.as_slice()); let separator = escape_sequences(options.separator.as_slice());
let terminator = match options.terminator { let terminator = match options.terminator {
Some(term) => escape_sequences(term.as_slice()), Some(term) => escape_sequences(term.as_slice()),
None => separator.clone() None => separator.clone()
}; };
print_seq(first, step, last, separator, terminator, options.widths); print_seq(first, step, last, largest_dec, separator, terminator, options.widths, padding);
0 0
} }
#[inline(always)] #[inline(always)]
fn done_printing(next: f64, step: f64, last: f64) -> bool { fn done_printing(next: f64, step: f64, last: f64) -> bool {
if step > 0f64 { if step >= 0f64 {
next > last next > last
} else { } else {
next < last next < last
} }
} }
fn print_seq(first: f64, step: f64, last: f64, separator: String, terminator: String, pad: bool) { fn print_seq(first: f64, step: f64, last: f64, largest_dec: uint, separator: String, terminator: String, pad: bool, padding: uint) {
let mut i = first; let mut i = 0;
let maxlen = first.max(last).to_str().len(); let mut value = first + i as f64 * step;
while !done_printing(i, step, last) { while !done_printing(value, step, last) {
let ilen = i.to_str().len(); let istr = value.to_str();
if pad && ilen < maxlen { let ilen = istr.len();
for _ in range(0, maxlen - ilen) { let before_dec = istr.as_slice().find('.').unwrap_or(ilen);
if pad && before_dec < padding {
for _ in range(0, padding - before_dec) {
print!("0"); print!("0");
} }
} }
print!("{:f}", i); print!("{}", istr);
i += step; let mut idec = ilen - before_dec;
if !done_printing(i, step, last) { if idec < largest_dec {
if idec == 0 {
print!(".");
idec += 1;
}
for _ in range(idec, largest_dec) {
print!("0")
}
}
i += 1;
value = first + i as f64 * step;
if !done_printing(value, step, last) {
print!("{:s}", separator); print!("{:s}", separator);
} }
} }
if i != first { if (first >= last && step < 0f64) || (first <= last && step > 0f64) {
print!("{:s}", terminator); print!("{:s}", terminator);
} }
} }