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