Skip to content

Commit f44fb7c

Browse files
authored
Merge branch 'master' into seq-bigdecimal-extended-numbers
2 parents 621ffa7 + 03a037e commit f44fb7c

File tree

26 files changed

+177
-62
lines changed

26 files changed

+177
-62
lines changed

src/uu/arch/src/arch.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
// For the full copyright and license information, please view the LICENSE
77
// file that was distributed with this source code.
88

9-
#[macro_use]
10-
extern crate uucore;
11-
129
use platform_info::*;
1310

1411
use clap::{crate_version, App};

src/uu/base32/src/base32.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// For the full copyright and license information, please view the LICENSE file
66
// that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use std::io::{stdin, Read};
129

1310
use clap::App;

src/uu/base64/src/base64.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
// For the full copyright and license information, please view the LICENSE file
77
// that was distributed with this source code.
88

9-
#[macro_use]
10-
extern crate uucore;
11-
129
use uu_base32::base_common;
1310
pub use uu_base32::uu_app;
1411

src/uu/basenc/src/basenc.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

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

11-
#[macro_use]
12-
extern crate uucore;
13-
1411
use clap::{App, Arg};
1512
use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR};
1613

src/uu/cat/src/cat.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#[cfg(unix)]
1414
extern crate unix_socket;
15-
#[macro_use]
16-
extern crate uucore;
1715

1816
// last synced with: cat (GNU coreutils) 8.13
1917
use clap::{crate_version, App, Arg};

src/uu/chgrp/src/chgrp.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

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

10-
#[macro_use]
11-
extern crate uucore;
1210
use uucore::display::Quotable;
1311
pub use uucore::entries;
1412
use uucore::error::{FromIo, UResult, USimpleError};

src/uu/chown/src/chown.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

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

10-
#[macro_use]
11-
extern crate uucore;
1210
use uucore::display::Quotable;
1311
pub use uucore::entries::{self, Group, Locate, Passwd};
1412
use uucore::perms::{chown_base, options, IfFrom};

src/uu/df/src/df.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// For the full copyright and license information, please view the LICENSE file
77
// that was distributed with this source code.
88

9-
#[macro_use]
10-
extern crate uucore;
119
use uucore::error::UError;
1210
use uucore::error::UResult;
1311
#[cfg(unix)]

src/uu/dirname/src/dirname.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// For the full copyright and license information, please view the LICENSE
66
// file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use clap::{crate_version, App, Arg};
129
use std::path::Path;
1310
use uucore::display::print_verbatim;

src/uu/echo/src/echo.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
// For the full copyright and license information, please view the LICENSE
77
// file that was distributed with this source code.
88

9-
#[macro_use]
10-
extern crate uucore;
11-
129
use clap::{crate_version, App, Arg};
1310
use std::io::{self, Write};
1411
use std::iter::Peekable;

src/uu/expr/src/expr.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
//* For the full copyright and license information, please view the LICENSE
66
//* file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use clap::{crate_version, App, Arg};
129
use uucore::error::{UResult, USimpleError};
1310
use uucore::InvalidEncodingHandling;

src/uu/false/src/false.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// * For the full copyright and license information, please view the LICENSE
66
// * file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use clap::App;
129
use uucore::error::UResult;
1310

src/uu/hostid/src/hostid.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
// spell-checker:ignore (ToDO) gethostid
99

10-
#[macro_use]
11-
extern crate uucore;
12-
1310
use clap::{crate_version, App};
1411
use libc::c_long;
1512
use uucore::error::UResult;

src/uu/hostname/src/hostname.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

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

10-
#[macro_use]
11-
extern crate uucore;
12-
1310
use std::collections::hash_set::HashSet;
1411
use std::net::ToSocketAddrs;
1512
use std::str;

src/uu/mktemp/src/mktemp.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
// spell-checker:ignore (paths) GPGHome
1010

11-
#[macro_use]
12-
extern crate uucore;
13-
1411
use clap::{crate_version, App, Arg};
1512
use uucore::display::{println_verbatim, Quotable};
1613
use uucore::error::{FromIo, UError, UResult};

src/uu/pwd/src/pwd.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// * For the full copyright and license information, please view the LICENSE
66
// * file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use clap::{crate_version, App, Arg};
129
use std::env;
1310
use std::io;

src/uu/seq/src/seq.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,115 @@ struct SeqOptions {
4444
widths: bool,
4545
}
4646

47+
enum Number {
48+
/// Negative zero, as if it were an integer.
49+
MinusZero,
50+
BigInt(BigInt),
51+
F64(f64),
52+
}
53+
54+
impl Number {
55+
fn is_zero(&self) -> bool {
56+
match self {
57+
Number::MinusZero => true,
58+
Number::BigInt(n) => n.is_zero(),
59+
Number::F64(n) => n.is_zero(),
60+
}
61+
}
62+
63+
fn into_f64(self) -> f64 {
64+
match self {
65+
Number::MinusZero => -0.,
66+
// BigInt::to_f64() can not return None.
67+
Number::BigInt(n) => n.to_f64().unwrap(),
68+
Number::F64(n) => n,
69+
}
70+
}
71+
72+
/// Convert this number into a bigint, consuming it.
73+
///
74+
/// For floats, this returns the [`BigInt`] corresponding to the
75+
/// floor of the number.
76+
fn into_bigint(self) -> BigInt {
77+
match self {
78+
Number::MinusZero => BigInt::zero(),
79+
Number::F64(x) => BigInt::from(x.floor() as i64),
80+
Number::BigInt(n) => n,
81+
}
82+
}
83+
}
84+
85+
impl FromStr for Number {
86+
type Err = String;
87+
fn from_str(mut s: &str) -> Result<Self, Self::Err> {
88+
s = s.trim_start();
89+
if s.starts_with('+') {
90+
s = &s[1..];
91+
}
92+
let is_neg = s.starts_with('-');
93+
94+
match s.to_lowercase().find("0x") {
95+
Some(i) if i <= 1 => match &s.as_bytes()[i + 2] {
96+
b'-' | b'+' => Err(format!(
97+
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
98+
s.quote(),
99+
uucore::execution_phrase(),
100+
)),
101+
// TODO: hexadecimal floating point parsing (see #2660)
102+
b'.' => Err(format!(
103+
"NotImplemented: hexadecimal floating point numbers: {}\nTry '{} --help' for more information.",
104+
s.quote(),
105+
uucore::execution_phrase(),
106+
)),
107+
_ => {
108+
let num = BigInt::from_str_radix(&s[i + 2..], 16)
109+
.map_err(|_| format!(
110+
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
111+
s.quote(),
112+
uucore::execution_phrase(),
113+
))?;
114+
match (is_neg, num == BigInt::zero()) {
115+
(true, true) => Ok(Number::MinusZero),
116+
(true, false) => Ok(Number::BigInt(-num)),
117+
(false, _) => Ok(Number::BigInt(num)),
118+
}
119+
}
120+
},
121+
Some(_) => Err(format!(
122+
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
123+
s.quote(),
124+
uucore::execution_phrase(),
125+
)),
126+
127+
None => match s.parse::<BigInt>() {
128+
Ok(n) => {
129+
// If `s` is '-0', then `parse()` returns
130+
// `BigInt::zero()`, but we need to return
131+
// `Number::MinusZero` instead.
132+
if n == BigInt::zero() && is_neg {
133+
Ok(Number::MinusZero)
134+
} else {
135+
Ok(Number::BigInt(n))
136+
}
137+
}
138+
Err(_) => match s.parse::<f64>() {
139+
Ok(value) if value.is_nan() => Err(format!(
140+
"invalid 'not-a-number' argument: {}\nTry '{} --help' for more information.",
141+
s.quote(),
142+
uucore::execution_phrase(),
143+
)),
144+
Ok(value) => Ok(Number::F64(value)),
145+
Err(_) => Err(format!(
146+
"invalid floating point argument: {}\nTry '{} --help' for more information.",
147+
s.quote(),
148+
uucore::execution_phrase(),
149+
)),
150+
},
151+
},
152+
}
153+
}
154+
}
155+
47156
/// A range of integers.
48157
///
49158
/// The elements are (first, increment, last).

src/uu/sleep/src/sleep.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// * For the full copyright and license information, please view the LICENSE
66
// * file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use std::thread;
129
use std::time::Duration;
1310

src/uu/true/src/true.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// * For the full copyright and license information, please view the LICENSE
66
// * file that was distributed with this source code.
77

8-
#[macro_use]
9-
extern crate uucore;
10-
118
use clap::App;
129
use uucore::error::UResult;
1310

src/uu/unlink/src/unlink.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

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

10-
#[macro_use]
11-
extern crate uucore;
12-
1310
use std::fs::remove_file;
1411
use std::path::Path;
1512

src/uu/uptime/src/uptime.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
use chrono::{Local, TimeZone, Utc};
1212
use clap::{crate_version, App, Arg};
1313

14-
#[macro_use]
15-
extern crate uucore;
1614
// import crate time from utmpx
1715
pub use uucore::libc;
1816
use uucore::libc::time_t;

src/uu/users/src/users.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::path::Path;
1212

1313
use clap::{crate_version, App, Arg};
14+
use uucore::error::UResult;
1415
use uucore::utmpx::{self, Utmpx};
1516

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

32-
pub fn uumain(args: impl uucore::Args) -> i32 {
33+
#[uucore_procs::gen_uumain]
34+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3335
let usage = usage();
3436
let after_help = get_long_usage();
3537

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

62-
0
64+
Ok(())
6365
}
6466

6567
pub fn uu_app() -> App<'static, 'static> {

src/uu/whoami/src/whoami.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#[macro_use]
1111
extern crate clap;
12-
#[macro_use]
13-
extern crate uucore;
1412

1513
use clap::App;
1614

src/uu/yes/src/yes.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use std::io::{self, Write};
1212

1313
#[macro_use]
1414
extern crate clap;
15-
#[macro_use]
16-
extern crate uucore;
1715

1816
use clap::{App, Arg};
1917
use uucore::error::{UResult, USimpleError};

src/uucore_procs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub fn gen_uumain(_args: TokenStream, stream: TokenStream) -> TokenStream {
101101
Err(e) => {
102102
let s = format!("{}", e);
103103
if s != "" {
104-
show_error!("{}", s);
104+
uucore::show_error!("{}", s);
105105
}
106106
if e.usage() {
107107
eprintln!("Try '{} --help' for more information.", uucore::execution_phrase());

0 commit comments

Comments
 (0)