1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge branch 'master' of github.com:backwaterred/coreutils into implement-dd-bwtr

This commit is contained in:
ty 2021-03-16 14:37:01 -07:00
commit 5271427193
7 changed files with 97 additions and 20 deletions

View file

@ -18,7 +18,7 @@ matrix:
- rust: nightly - rust: nightly
fast_finish: true fast_finish: true
include: include:
- rust: 1.32.0 - rust: 1.33.0
env: FEATURES=unix env: FEATURES=unix
# - rust: stable # - rust: stable
# os: linux # os: linux

View file

@ -10,7 +10,7 @@ extern crate uucore;
use clap::{App, Arg, ArgMatches}; use clap::{App, Arg, ArgMatches};
use std::fs::File; use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Result, Write};
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
@ -61,8 +61,7 @@ impl Uniq {
let delimiters = &self.delimiters; let delimiters = &self.delimiters;
let line_terminator = self.get_line_terminator(); let line_terminator = self.get_line_terminator();
for io_line in reader.split(line_terminator) { for line in reader.split(line_terminator).map(get_line_string) {
let line = String::from_utf8(crash_if_err!(1, io_line)).unwrap();
if !lines.is_empty() && self.cmp_keys(&lines[0], &line) { if !lines.is_empty() && self.cmp_keys(&lines[0], &line) {
let print_delimiter = delimiters == &Delimiters::Prepend let print_delimiter = delimiters == &Delimiters::Prepend
|| (delimiters == &Delimiters::Separate && first_line_printed); || (delimiters == &Delimiters::Separate && first_line_printed);
@ -80,22 +79,19 @@ impl Uniq {
fn skip_fields<'a>(&self, line: &'a str) -> &'a str { fn skip_fields<'a>(&self, line: &'a str) -> &'a str {
if let Some(skip_fields) = self.skip_fields { if let Some(skip_fields) = self.skip_fields {
if line.split_whitespace().count() > skip_fields { let mut i = 0;
let mut field = 0; let mut char_indices = line.char_indices();
let mut i = 0; for _ in 0..skip_fields {
while field < skip_fields && i < line.len() { if char_indices.find(|(_, c)| !c.is_whitespace()) == None {
while i < line.len() && line.chars().nth(i).unwrap().is_whitespace() { return "";
i += 1; }
} match char_indices.find(|(_, c)| c.is_whitespace()) {
while i < line.len() && !line.chars().nth(i).unwrap().is_whitespace() { None => return "",
i += 1;
} Some((next_field_i, _)) => i = next_field_i,
field += 1;
} }
&line[i..]
} else {
""
} }
&line[i..]
} else { } else {
line line
} }
@ -199,6 +195,11 @@ impl Uniq {
} }
} }
fn get_line_string(io_line: Result<Vec<u8>>) -> String {
let line_bytes = crash_if_err!(1, io_line);
crash_if_err!(1, String::from_utf8(line_bytes))
}
fn opt_parsed<T: FromStr>(opt_name: &str, matches: &ArgMatches) -> Option<T> { fn opt_parsed<T: FromStr>(opt_name: &str, matches: &ArgMatches) -> Option<T> {
matches.value_of(opt_name).map(|arg_str| { matches.value_of(opt_name).map(|arg_str| {
let opt_val: Option<T> = arg_str.parse().ok(); let opt_val: Option<T> = arg_str.parse().ok();

View file

@ -45,3 +45,68 @@ fn test_sleep_h_suffix() {
let duration = before_test.elapsed(); let duration = before_test.elapsed();
assert!(duration >= millis_360); assert!(duration >= millis_360);
} }
#[test]
fn test_sleep_negative_duration() {
new_ucmd!().args(&["-1"]).fails();
new_ucmd!().args(&["-1s"]).fails();
new_ucmd!().args(&["-1m"]).fails();
new_ucmd!().args(&["-1h"]).fails();
new_ucmd!().args(&["-1d"]).fails();
}
#[test]
fn test_sleep_zero_duration() {
new_ucmd!().args(&["0"]).succeeds().stdout_only("");
new_ucmd!().args(&["0s"]).succeeds().stdout_only("");
new_ucmd!().args(&["0m"]).succeeds().stdout_only("");
new_ucmd!().args(&["0h"]).succeeds().stdout_only("");
new_ucmd!().args(&["0d"]).succeeds().stdout_only("");
}
#[test]
fn test_sleep_no_argument() {
new_ucmd!().fails();
}
#[test]
fn test_sleep_sum_duration_same_suffix() {
let millis_200 = Duration::from_millis(100 + 100);
let before_test = Instant::now();
new_ucmd!()
.args(&["0.1s", "0.1s"])
.succeeds()
.stdout_only("");
let duration = before_test.elapsed();
assert!(duration >= millis_200);
}
#[test]
fn test_sleep_sum_duration_different_suffix() {
let millis_700 = Duration::from_millis(100 + 600);
let before_test = Instant::now();
new_ucmd!()
.args(&["0.1s", "0.01m"])
.succeeds()
.stdout_only("");
let duration = before_test.elapsed();
assert!(duration >= millis_700);
}
#[test]
fn test_sleep_sum_duration_many() {
let millis_900 = Duration::from_millis(100 + 100 + 300 + 400);
let before_test = Instant::now();
new_ucmd!()
.args(&["0.1s", "0.1s", "0.3s", "0.4s"])
.succeeds()
.stdout_only("");
let duration = before_test.elapsed();
assert!(duration >= millis_900);
}

View file

@ -138,3 +138,12 @@ fn test_stdin_zero_terminated() {
.run() .run()
.stdout_is_fixture("sorted-zero-terminated.expected"); .stdout_is_fixture("sorted-zero-terminated.expected");
} }
#[test]
fn test_invalid_utf8() {
new_ucmd!()
.arg("not-utf8-sequence.txt")
.run()
.failure()
.stderr_only("uniq: error: invalid utf-8 sequence of 1 bytes from index 0");
}

View file

@ -0,0 +1,2 @@
Next line contains two bytes - 0xCC and 0xCD - which are not a valid utf-8 sequence
ÌÍ

View file

@ -1,2 +1,2 @@
aaa aa a aaa ⟪⟫ a
aa a aa a

View file

@ -1,4 +1,4 @@
aaa aa a aaa ⟪⟫ a
ZZZ aa a ZZZ aa a
ZZZ aa a ZZZ aa a
ZZZ bb a ZZZ bb a