mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
Update fold to nightly build.
This commit is contained in:
parent
b4c106b004
commit
dd19bc27c1
1 changed files with 19 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
||||||
#![crate_name = "fold"]
|
#![crate_name = "fold"]
|
||||||
#![feature(collections, core, old_io, old_path, rustc_private, unicode)]
|
#![feature(collections, rustc_private)]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the uutils coreutils package.
|
* This file is part of the uutils coreutils package.
|
||||||
|
@ -13,9 +13,9 @@
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::old_io as io;
|
use std::fs::File;
|
||||||
use std::old_io::fs::File;
|
use std::io::{BufRead, BufReader, Read, stdin, Write};
|
||||||
use std::old_io::BufferedReader;
|
use std::path::Path;
|
||||||
|
|
||||||
#[path = "../common/util.rs"]
|
#[path = "../common/util.rs"]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -25,7 +25,7 @@ static NAME: &'static str = "fold";
|
||||||
static VERSION: &'static str = "1.0.0";
|
static VERSION: &'static str = "1.0.0";
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let (args, obs_width) = handle_obsolete(args.as_slice());
|
let (args, obs_width) = handle_obsolete(&args[..]);
|
||||||
let program = args[0].clone();
|
let program = args[0].clone();
|
||||||
|
|
||||||
let opts = [
|
let opts = [
|
||||||
|
@ -36,7 +36,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
getopts::optflag("V", "version", "output version information and exit")
|
getopts::optflag("V", "version", "output version information and exit")
|
||||||
];
|
];
|
||||||
|
|
||||||
let matches = match getopts::getopts(args.tail(), &opts) {
|
let matches = match getopts::getopts(&args[1..], &opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => crash!(1, "{}", f)
|
Err(f) => crash!(1, "{}", f)
|
||||||
};
|
};
|
||||||
|
@ -82,8 +82,8 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
|
fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
|
||||||
for (i, arg) in args.iter().enumerate() {
|
for (i, arg) in args.iter().enumerate() {
|
||||||
let slice = arg.as_slice();
|
let slice = &arg;
|
||||||
if slice.char_at(0) == '-' && slice.len() > 1 && slice.char_at(1).is_digit(10) {
|
if slice.chars().next().unwrap() == '-' && slice.len() > 1 && slice.chars().nth(1).unwrap().is_digit(10) {
|
||||||
return (args[..i].to_vec() + &args[i + 1..],
|
return (args[..i].to_vec() + &args[i + 1..],
|
||||||
Some(slice[1..].to_string()));
|
Some(slice[1..].to_string()));
|
||||||
}
|
}
|
||||||
|
@ -94,16 +94,16 @@ fn handle_obsolete(args: &[String]) -> (Vec<String>, Option<String>) {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
|
fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
|
||||||
for filename in filenames.iter() {
|
for filename in filenames.iter() {
|
||||||
let filename: &str = filename.as_slice();
|
let filename: &str = &filename;
|
||||||
let mut stdin_buf;
|
let mut stdin_buf;
|
||||||
let mut file_buf;
|
let mut file_buf;
|
||||||
let buffer = BufferedReader::new(
|
let buffer = BufReader::new(
|
||||||
if filename == "-" {
|
if filename == "-" {
|
||||||
stdin_buf = io::stdio::stdin_raw();
|
stdin_buf = stdin();
|
||||||
&mut stdin_buf as &mut Reader
|
&mut stdin_buf as &mut Read
|
||||||
} else {
|
} else {
|
||||||
file_buf = safe_unwrap!(File::open(&Path::new(filename)));
|
file_buf = safe_unwrap!(File::open(Path::new(filename)));
|
||||||
&mut file_buf as &mut Reader
|
&mut file_buf as &mut Read
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
fold_file(buffer, bytes, spaces, width);
|
fold_file(buffer, bytes, spaces, width);
|
||||||
|
@ -111,11 +111,9 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn fold_file<T: io::Reader>(file: BufferedReader<T>, bytes: bool, spaces: bool, width: usize) {
|
fn fold_file<T: Read>(mut file: BufReader<T>, bytes: bool, spaces: bool, width: usize) {
|
||||||
let mut file = file;
|
let mut line = String::new();
|
||||||
for line in file.lines() {
|
while safe_unwrap!(file.read_line(&mut line)) > 0 {
|
||||||
let line_string = safe_unwrap!(line);
|
|
||||||
let mut line = line_string.as_slice();
|
|
||||||
if bytes {
|
if bytes {
|
||||||
let len = line.len();
|
let len = line.len();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
@ -143,15 +141,15 @@ fn fold_file<T: io::Reader>(file: BufferedReader<T>, bytes: bool, spaces: bool,
|
||||||
println!("");
|
println!("");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
line = &line[..line.len() - 1];
|
|
||||||
len -= 1;
|
len -= 1;
|
||||||
|
line.truncate(len);
|
||||||
}
|
}
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for (i, ch) in line.chars().enumerate() {
|
for (i, ch) in line.chars().enumerate() {
|
||||||
if count >= width {
|
if count >= width {
|
||||||
let (val, ncount) = {
|
let (val, ncount) = {
|
||||||
let slice = output.as_slice();
|
let slice = &output[..];
|
||||||
let (out, val, ncount) =
|
let (out, val, ncount) =
|
||||||
if spaces && i + 1 < len {
|
if spaces && i + 1 < len {
|
||||||
match rfind_whitespace(slice) {
|
match rfind_whitespace(slice) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue