From 3e2c58a562e93cbcc50d0820d7f14b7a7ee2a17d Mon Sep 17 00:00:00 2001 From: Heather Date: Wed, 22 Jan 2014 15:27:08 +0400 Subject: [PATCH 1/5] dirname fix according from_utf8 return option now --- dirname/dirname.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dirname/dirname.rs b/dirname/dirname.rs index 3dd37c4b8..a38c16cd4 100644 --- a/dirname/dirname.rs +++ b/dirname/dirname.rs @@ -60,7 +60,10 @@ directory).", opts)); if !matches.free.is_empty() { for path in matches.free.iter() { let p = std::path::Path::new(path.clone()); - print(std::str::from_utf8(p.dirname())); + let d = std::str::from_utf8(p.dirname()); + if d.is_some() { + print(d.unwrap()); + } print(separator); } } else { From 5581cd079b2d4da140deed8e338e65e6d57cbaf7 Mon Sep 17 00:00:00 2001 From: Heather Date: Wed, 22 Jan 2014 15:31:28 +0400 Subject: [PATCH 2/5] fix wc due from_utf8 changes and resolve warnings --- wc/wc.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/wc/wc.rs b/wc/wc.rs index c16177e71..de0f96f84 100644 --- a/wc/wc.rs +++ b/wc/wc.rs @@ -13,7 +13,7 @@ extern mod extra; use std::os; use std::io::{print, stdin, stderr, File, result, BufferedReader}; -use std::str::from_utf8_opt; +use std::str::from_utf8; use extra::getopts::{groups, Matches}; struct Result { @@ -58,7 +58,7 @@ fn main() { return; } - if (matches.opt_present("version")) { + if matches.opt_present("version") { println!("wc 1.0.0"); return; } @@ -111,14 +111,14 @@ pub fn wc(files: ~[~str], matches: &Matches) { match result(| | reader.read_until(LF)) { Ok(Some(raw_line)) => { // GNU 'wc' only counts lines that end in LF as lines - if (raw_line.iter().last().unwrap() == &LF) { + if raw_line.iter().last().unwrap() == &LF { line_count += 1; } byte_count += raw_line.iter().len(); // try and convert the bytes to UTF-8 first - match from_utf8_opt(raw_line) { + match from_utf8(raw_line) { Some(line) => { word_count += line.words().len(); current_char_count = line.chars().len(); @@ -138,7 +138,7 @@ pub fn wc(files: ~[~str], matches: &Matches) { } } - if (current_char_count > longest_line_length) { + if current_char_count > longest_line_length { // we subtract one here because `line.iter().len()` includes the LF // matches GNU 'wc' behaviour longest_line_length = current_char_count - 1; @@ -163,7 +163,7 @@ pub fn wc(files: ~[~str], matches: &Matches) { total_char_count += char_count; total_byte_count += byte_count; - if (longest_line_length > total_longest_line_length) { + if longest_line_length > total_longest_line_length { total_longest_line_length = longest_line_length; } @@ -175,38 +175,41 @@ pub fn wc(files: ~[~str], matches: &Matches) { print_stats(&result.filename, result.lines, result.words, result.chars, result.bytes, result.max_line_length, matches, max_str_len); } - if (files.len() > 1) { + if files.len() > 1 { print_stats(&~"total", total_line_count, total_word_count, total_char_count, total_byte_count, total_longest_line_length, matches, max_str_len); } } fn print_stats(filename: &~str, line_count: uint, word_count: uint, char_count: uint, byte_count: uint, longest_line_length: uint, matches: &Matches, max_str_len: uint) { - if (matches.opt_present("lines")) { + if matches.opt_present("lines") { print!("{:1$}", line_count, max_str_len); } - if (matches.opt_present("words")) { + if matches.opt_present("words") { print!("{:1$}", word_count, max_str_len); } - if (matches.opt_present("bytes")) { + if matches.opt_present("bytes") { print!("{:1$}", byte_count, max_str_len + 1); } - if (matches.opt_present("chars")) { + if matches.opt_present("chars") { print!("{:1$}", char_count, max_str_len); } - if (matches.opt_present("max-line-length")) { + if matches.opt_present("max-line-length") { print!("{:1$}", longest_line_length, max_str_len); } // defaults - if (!matches.opt_present("bytes") && !matches.opt_present("chars") && !matches.opt_present("lines") - && !matches.opt_present("words") && !matches.opt_present("max-line-length")) { + if !matches.opt_present("bytes") + && !matches.opt_present("chars") + && !matches.opt_present("lines") + && !matches.opt_present("words") + && !matches.opt_present("max-line-length") { print!("{:1$}", line_count, max_str_len); print!("{:1$}", word_count, max_str_len + 1); print!("{:1$}", byte_count, max_str_len + 1); } - if (*filename != ~"-") { + if *filename != ~"-" { println!(" {}", *filename); } else { From 7ac3e0fb2c85f5c0cee0ba9fadd2835cf8cf99b9 Mon Sep 17 00:00:00 2001 From: Heather Date: Tue, 4 Feb 2014 09:58:53 +0400 Subject: [PATCH 3/5] basename: use fail instead of writeln to stderr --- basename/basename.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/basename/basename.rs b/basename/basename.rs index 7f6540ec3..97467d1ed 100644 --- a/basename/basename.rs +++ b/basename/basename.rs @@ -11,7 +11,7 @@ extern mod extra; -use std::io::{print, println, stderr}; +use std::io::{print, println}; use std::os; use std::str; use std::str::StrSlice; @@ -27,20 +27,14 @@ fn main() { // // Argument parsing // - let opts = ~[ groups::optflag("h", "help", "display this help and exit"), groups::optflag("V", "version", "output version information and exit"), ]; let matches = match groups::getopts(args.tail(), opts) { - Ok(m) => m, - Err(f) => { - writeln!(&mut stderr() as &mut Writer, - "Invalid options\n{}", f.to_err_msg()); - os::set_exit_status(1); - return; - } + Ok(m) => m, + Err(f) => fail!("Invalid options\n{}", f.to_err_msg()) }; if matches.opt_present("help") { @@ -95,7 +89,6 @@ fn strip_dir(fullname :&~str) -> ~str { if c == '/' || c == '\\' { return name; } - name = str::from_char(c) + name; } From efae9b621f30556d0ab5727cedb068852205b663 Mon Sep 17 00:00:00 2001 From: Heather Date: Tue, 4 Feb 2014 11:14:26 +0400 Subject: [PATCH 4/5] update cat for current Rust syntax with ugly errors handling --- cat/cat.rs | 67 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/cat/cat.rs b/cat/cat.rs index fea871e4d..41b9e670c 100644 --- a/cat/cat.rs +++ b/cat/cat.rs @@ -15,7 +15,7 @@ extern mod extra; use std::os; -use std::io::{print, stdin, stderr, stdout, File, result}; +use std::io::{print, stdin, stdout, File}; use extra::getopts::groups; fn main() { @@ -36,12 +36,7 @@ fn main() { ]; let matches = match groups::getopts(args.tail(), opts) { Ok(m) => m, - Err(f) => { - writeln!(&mut stderr() as &mut Writer, - "Invalid options\n{}", f.to_err_msg()); - os::set_exit_status(1); - return - } + Err(f) => fail!("Invalid options\n{}", f.to_err_msg()) }; if matches.opt_present("help") { println!("cat 1.0.0"); @@ -118,11 +113,13 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end loop { // reading from a TTY seems to raise a condition on // EOF, rather than return Some(0) like a file. - match result(|| reader.read(buf)) { - Ok(Some(n)) if n != 0 => { + match reader.read(buf) { + Ok(n) if n != 0 => { for byte in buf.slice_to(n).iter() { if at_line_start && (number == NumberAll || (number == NumberNonEmpty && !is_newline_char(*byte))) { - write!(&mut writer as &mut Writer, "{0:6u}\t", counter); + match write!(&mut writer as &mut Writer, "{0:6u}\t", counter) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; counter += 1; at_line_start = false; } @@ -130,32 +127,45 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end at_line_start = true; } if show_tabs && *byte == TAB { - writer.write(bytes!("^I")); + match writer.write(bytes!("^I")) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } else if show_ends && *byte == LF { - writer.write(bytes!("$\n")); + match writer.write(bytes!("$\n")) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } else if show_nonprint && (*byte < 32 || *byte >= 127) && !is_newline_char(*byte) { let mut byte = *byte; if byte >= 128 { - writer.write(bytes!("M-")); + match writer.write(bytes!("M-")) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; byte = byte - 128; } if byte < 32 { - writer.write(['^' as u8, byte + 64]); + match writer.write(['^' as u8, byte + 64]) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } else if byte == 127 { - writer.write(['^' as u8, byte - 64]); + match writer.write(['^' as u8, byte - 64]) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } else { - writer.write([byte]); + match writer.write([byte]) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } } else { - writer.write([*byte]); + match writer.write([*byte]) { + Ok(_) => (), Err(err) => fail!("{}", err) + }; } } - } + }, _ => break } } } - return; } @@ -170,9 +180,12 @@ pub fn exec(files: ~[~str], number: NumberingMode, show_nonprint: bool, show_end loop { // reading from a TTY seems to raise a condition on EOF, // rather than return Some(0) like a file. - match result(|| reader.read(buf)) { - Ok(Some(n)) if n != 0 => writer.write(buf.slice_to(n)), - _ => break + match reader.read(buf) { + Ok(n) if n != 0 => { + match writer.write(buf.slice_to(n)) { + Ok(_) => (), Err(err) => fail!("{}", err) + } + }, _ => break } } } @@ -183,14 +196,8 @@ fn open(path: ~str) -> Option<~Reader> { return Some(~stdin() as ~Reader); } - match result(|| File::open(&std::path::Path::new(path.as_slice()))) { + match File::open(&std::path::Path::new(path.as_slice())) { Ok(fd) => return Some(~fd as ~Reader), - Err(e) => { - writeln!(&mut stderr() as &mut Writer, - "cat: {0:s}: {1:s}", path, e.to_str()); - os::set_exit_status(1); - } + Err(e) => fail!("cat: {0:s}: {1:s}", path, e.to_str()) } - - None } From c8a2b4f6e06b22dcf84eb11cb133a4881fc84a4a Mon Sep 17 00:00:00 2001 From: Heather Date: Tue, 4 Feb 2014 11:33:53 +0400 Subject: [PATCH 5/5] replace writing to stderr with fail --- dirname/dirname.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/dirname/dirname.rs b/dirname/dirname.rs index a38c16cd4..f2ba61139 100644 --- a/dirname/dirname.rs +++ b/dirname/dirname.rs @@ -12,7 +12,7 @@ extern mod extra; use std::os; -use std::io::{print, stderr}; +use std::io::print; use extra::getopts::groups; static VERSION: &'static str = "1.0.0"; @@ -28,12 +28,7 @@ fn main() { let matches = match groups::getopts(args.tail(), opts) { Ok(m) => m, - Err(f) => { - writeln!(&mut stderr() as &mut Writer, - "Invalid options\n{}", f.to_err_msg()); - os::set_exit_status(1); - return - } + Err(f) => fail!("Invalid options\n{}", f.to_err_msg()) }; if matches.opt_present("help") {