Skip to content

Commit

Permalink
Merge branch 'master' into seq-bigdecimal-extended-numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre authored Oct 9, 2021
2 parents 621ffa7 + 03a037e commit f44fb7c
Show file tree
Hide file tree
Showing 26 changed files with 177 additions and 62 deletions.
3 changes: 0 additions & 3 deletions src/uu/arch/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use platform_info::*;

use clap::{crate_version, App};
Expand Down
3 changes: 0 additions & 3 deletions src/uu/base32/src/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.

#[macro_use]
extern crate uucore;

use std::io::{stdin, Read};

use clap::App;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/base64/src/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.

#[macro_use]
extern crate uucore;

use uu_base32::base_common;
pub use uu_base32::uu_app;

Expand Down
3 changes: 0 additions & 3 deletions src/uu/basenc/src/basenc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

//spell-checker:ignore (args) lsbf msbf

#[macro_use]
extern crate uucore;

use clap::{App, Arg};
use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR};

Expand Down
2 changes: 0 additions & 2 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

#[cfg(unix)]
extern crate unix_socket;
#[macro_use]
extern crate uucore;

// last synced with: cat (GNU coreutils) 8.13
use clap::{crate_version, App, Arg};
Expand Down
2 changes: 0 additions & 2 deletions src/uu/chgrp/src/chgrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

// spell-checker:ignore (ToDO) COMFOLLOW Chowner RFILE RFILE's derefer dgid nonblank nonprint nonprinting

#[macro_use]
extern crate uucore;
use uucore::display::Quotable;
pub use uucore::entries;
use uucore::error::{FromIo, UResult, USimpleError};
Expand Down
2 changes: 0 additions & 2 deletions src/uu/chown/src/chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

// spell-checker:ignore (ToDO) COMFOLLOW Passwd RFILE RFILE's derefer dgid duid groupname

#[macro_use]
extern crate uucore;
use uucore::display::Quotable;
pub use uucore::entries::{self, Group, Locate, Passwd};
use uucore::perms::{chown_base, options, IfFrom};
Expand Down
2 changes: 0 additions & 2 deletions src/uu/df/src/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.

#[macro_use]
extern crate uucore;
use uucore::error::UError;
use uucore::error::UResult;
#[cfg(unix)]
Expand Down
3 changes: 0 additions & 3 deletions src/uu/dirname/src/dirname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use std::path::Path;
use uucore::display::print_verbatim;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/echo/src/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use std::io::{self, Write};
use std::iter::Peekable;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
//* For the full copyright and license information, please view the LICENSE
//* file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use uucore::error::{UResult, USimpleError};
use uucore::InvalidEncodingHandling;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/false/src/false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::App;
use uucore::error::UResult;

Expand Down
3 changes: 0 additions & 3 deletions src/uu/hostid/src/hostid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

// spell-checker:ignore (ToDO) gethostid

#[macro_use]
extern crate uucore;

use clap::{crate_version, App};
use libc::c_long;
use uucore::error::UResult;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/hostname/src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

// spell-checker:ignore (ToDO) MAKEWORD addrs hashset

#[macro_use]
extern crate uucore;

use std::collections::hash_set::HashSet;
use std::net::ToSocketAddrs;
use std::str;
Expand Down
3 changes: 0 additions & 3 deletions src/uu/mktemp/src/mktemp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

// spell-checker:ignore (paths) GPGHome

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use uucore::display::{println_verbatim, Quotable};
use uucore::error::{FromIo, UError, UResult};
Expand Down
3 changes: 0 additions & 3 deletions src/uu/pwd/src/pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::{crate_version, App, Arg};
use std::env;
use std::io;
Expand Down
109 changes: 109 additions & 0 deletions src/uu/seq/src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,115 @@ struct SeqOptions {
widths: bool,
}

enum Number {
/// Negative zero, as if it were an integer.
MinusZero,
BigInt(BigInt),
F64(f64),
}

impl Number {
fn is_zero(&self) -> bool {
match self {
Number::MinusZero => true,
Number::BigInt(n) => n.is_zero(),
Number::F64(n) => n.is_zero(),
}
}

fn into_f64(self) -> f64 {
match self {
Number::MinusZero => -0.,
// BigInt::to_f64() can not return None.
Number::BigInt(n) => n.to_f64().unwrap(),
Number::F64(n) => n,
}
}

/// Convert this number into a bigint, consuming it.
///
/// For floats, this returns the [`BigInt`] corresponding to the
/// floor of the number.
fn into_bigint(self) -> BigInt {
match self {
Number::MinusZero => BigInt::zero(),
Number::F64(x) => BigInt::from(x.floor() as i64),
Number::BigInt(n) => n,
}
}
}

impl FromStr for Number {
type Err = String;
fn from_str(mut s: &str) -> Result<Self, Self::Err> {
s = s.trim_start();
if s.starts_with('+') {
s = &s[1..];
}
let is_neg = s.starts_with('-');

match s.to_lowercase().find("0x") {
Some(i) if i <= 1 => match &s.as_bytes()[i + 2] {
b'-' | b'+' => Err(format!(
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
// TODO: hexadecimal floating point parsing (see #2660)
b'.' => Err(format!(
"NotImplemented: hexadecimal floating point numbers: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
_ => {
let num = BigInt::from_str_radix(&s[i + 2..], 16)
.map_err(|_| format!(
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
))?;
match (is_neg, num == BigInt::zero()) {
(true, true) => Ok(Number::MinusZero),
(true, false) => Ok(Number::BigInt(-num)),
(false, _) => Ok(Number::BigInt(num)),
}
}
},
Some(_) => Err(format!(
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),

None => match s.parse::<BigInt>() {
Ok(n) => {
// If `s` is '-0', then `parse()` returns
// `BigInt::zero()`, but we need to return
// `Number::MinusZero` instead.
if n == BigInt::zero() && is_neg {
Ok(Number::MinusZero)
} else {
Ok(Number::BigInt(n))
}
}
Err(_) => match s.parse::<f64>() {
Ok(value) if value.is_nan() => Err(format!(
"invalid 'not-a-number' argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
Ok(value) => Ok(Number::F64(value)),
Err(_) => Err(format!(
"invalid floating point argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
},
},
}
}
}

/// A range of integers.
///
/// The elements are (first, increment, last).
Expand Down
3 changes: 0 additions & 3 deletions src/uu/sleep/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use std::thread;
use std::time::Duration;

Expand Down
3 changes: 0 additions & 3 deletions src/uu/true/src/true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

#[macro_use]
extern crate uucore;

use clap::App;
use uucore::error::UResult;

Expand Down
3 changes: 0 additions & 3 deletions src/uu/unlink/src/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

/* last synced with: unlink (GNU coreutils) 8.21 */

#[macro_use]
extern crate uucore;

use std::fs::remove_file;
use std::path::Path;

Expand Down
2 changes: 0 additions & 2 deletions src/uu/uptime/src/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use chrono::{Local, TimeZone, Utc};
use clap::{crate_version, App, Arg};

#[macro_use]
extern crate uucore;
// import crate time from utmpx
pub use uucore::libc;
use uucore::libc::time_t;
Expand Down
6 changes: 4 additions & 2 deletions src/uu/users/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::path::Path;

use clap::{crate_version, App, Arg};
use uucore::error::UResult;
use uucore::utmpx::{self, Utmpx};

static ABOUT: &str = "Print the user names of users currently logged in to the current host";
Expand All @@ -29,7 +30,8 @@ If FILE is not specified, use {}. /var/log/wtmp as FILE is common.",
)
}

pub fn uumain(args: impl uucore::Args) -> i32 {
#[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let usage = usage();
let after_help = get_long_usage();

Expand Down Expand Up @@ -59,7 +61,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
println!("{}", users.join(" "));
}

0
Ok(())
}

pub fn uu_app() -> App<'static, 'static> {
Expand Down
2 changes: 0 additions & 2 deletions src/uu/whoami/src/whoami.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#[macro_use]
extern crate clap;
#[macro_use]
extern crate uucore;

use clap::App;

Expand Down
2 changes: 0 additions & 2 deletions src/uu/yes/src/yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use std::io::{self, Write};

#[macro_use]
extern crate clap;
#[macro_use]
extern crate uucore;

use clap::{App, Arg};
use uucore::error::{UResult, USimpleError};
Expand Down
2 changes: 1 addition & 1 deletion src/uucore_procs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn gen_uumain(_args: TokenStream, stream: TokenStream) -> TokenStream {
Err(e) => {
let s = format!("{}", e);
if s != "" {
show_error!("{}", s);
uucore::show_error!("{}", s);
}
if e.usage() {
eprintln!("Try '{} --help' for more information.", uucore::execution_phrase());
Expand Down
Loading

0 comments on commit f44fb7c

Please sign in to comment.