1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 21:17:46 +00:00

Merge pull request #541 from jbcrail/fix-echo

Upgrade echo to use recent Rust nightly build.
This commit is contained in:
Heather 2015-04-25 07:50:27 +03:00
commit 5b34d19a83

View file

@ -1,5 +1,5 @@
#![crate_name = "echo"] #![crate_name = "echo"]
#![feature(collections, core, old_io, rustc_private)] #![feature(rustc_private, str_char)]
/* /*
* This file is part of the uutils coreutils package. * This file is part of the uutils coreutils package.
@ -13,8 +13,6 @@
extern crate getopts; extern crate getopts;
extern crate libc; extern crate libc;
use std::old_io::{print, println};
use std::num::from_str_radix;
use std::str::from_utf8; use std::str::from_utf8;
#[path = "../common/util.rs"] #[path = "../common/util.rs"]
@ -32,7 +30,7 @@ struct EchoOptions {
#[inline(always)] #[inline(always)]
fn to_char(bytes: &Vec<u8>, base: u32) -> char { fn to_char(bytes: &Vec<u8>, base: u32) -> char {
from_str_radix::<usize>(from_utf8(bytes.as_slice()).unwrap(), base).unwrap() as u8 as char usize::from_str_radix(from_utf8(bytes.as_ref()).unwrap(), base).unwrap() as u8 as char
} }
#[inline(always)] #[inline(always)]
@ -60,7 +58,7 @@ fn convert_str(string: &[u8], index: usize, base: u32) -> (char, usize) {
}; };
let mut bytes = vec!(); let mut bytes = vec!();
for offset in range(0usize, max_digits) { for offset in (0usize .. max_digits) {
if string.len() <= index + offset as usize { if string.len() <= index + offset as usize {
break; break;
} }
@ -83,7 +81,7 @@ fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<Str
let mut echo_args = vec!(); let mut echo_args = vec!();
let program = args[0].clone(); let program = args[0].clone();
'argloop: for arg in args.into_iter().skip(1) { 'argloop: for arg in args.into_iter().skip(1) {
match arg.as_slice() { match arg.as_ref() {
"--help" | "-h" => { "--help" | "-h" => {
print_help(&program); print_help(&program);
return None; return None;
@ -96,10 +94,9 @@ fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<Str
"-e" => options.escape = true, "-e" => options.escape = true,
"-E" => options.escape = false, "-E" => options.escape = false,
_ => { _ => {
if arg.len() > 1 && arg.as_slice().char_at(0) == '-' { if arg.len() > 1 && arg.char_at(0) == '-' {
let mut newopts = options.clone(); let mut newopts = options.clone();
let argptr: *const String = &arg; // escape from the borrow checker for ch in arg.chars().skip(1) {
for ch in unsafe { (*argptr).as_slice() }.chars().skip(1) {
match ch { match ch {
'h' => { 'h' => {
print_help(&program); print_help(&program);
@ -113,7 +110,7 @@ fn parse_options(args: Vec<String>, options: &mut EchoOptions) -> Option<Vec<Str
'e' => newopts.escape = true, 'e' => newopts.escape = true,
'E' => newopts.escape = false, 'E' => newopts.escape = false,
_ => { _ => {
echo_args.push(arg); echo_args.push(arg.clone());
continue 'argloop; continue 'argloop;
} }
} }
@ -142,8 +139,8 @@ fn print_help(program: &String) {
println!(" {0} [SHORT-OPTION]... [STRING]...", *program); println!(" {0} [SHORT-OPTION]... [STRING]...", *program);
println!(" {0} LONG-OPTION", *program); println!(" {0} LONG-OPTION", *program);
println!(""); println!("");
println(getopts::usage("Echo the STRING(s) to standard output.", &opts).as_slice()); println!("{}", getopts::usage("Echo the STRING(s) to standard output.", &opts));
println("If -e is in effect, the following sequences are recognized: println!("{}", "If -e is in effect, the following sequences are recognized:
\\\\ backslash \\\\ backslash
\\a alert (BEL) \\a alert (BEL)
@ -178,7 +175,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
let string = free.connect(" "); let string = free.connect(" ");
if options.escape { if options.escape {
let mut prev_was_slash = false; let mut prev_was_slash = false;
let mut iter = string.as_slice().chars().enumerate(); let mut iter = string.chars().enumerate();
loop { loop {
match iter.next() { match iter.next() {
Some((index, c)) => { Some((index, c)) => {
@ -207,7 +204,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
print!("\\x"); print!("\\x");
} else { } else {
print!("{}", c); print!("{}", c);
for _ in range(0, num_char_used) { for _ in (0 .. num_char_used) {
iter.next(); // consume used characters iter.next(); // consume used characters
} }
} }
@ -218,7 +215,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
print!("\0"); print!("\0");
} else { } else {
print!("{}", c); print!("{}", c);
for _ in range(0, num_char_used) { for _ in (0 .. num_char_used) {
iter.next(); // consume used characters iter.next(); // consume used characters
} }
} }
@ -229,7 +226,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
print!("\\{}", c); print!("\\{}", c);
} else { } else {
print!("{}", esc_c); print!("{}", esc_c);
for _ in range(1, num_char_used) { for _ in (1 .. num_char_used) {
iter.next(); // consume used characters iter.next(); // consume used characters
} }
} }
@ -241,7 +238,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
} }
} }
} else { } else {
print(string.as_slice()); print!("{}", string);
} }
} }