Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printf rewrite (with a lot of seq changes) #5128

Merged
merged 33 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a3e68d5
uucore: start work on a completely new printf implementation
tertsdiepraam Aug 2, 2023
66eb64e
dd, printf, seq: update to new printf
tertsdiepraam Aug 2, 2023
407bccc
some more work on printf spec
tertsdiepraam Aug 10, 2023
2881090
Merge branch 'main' into printf-rewrite
tertsdiepraam Oct 28, 2023
69b7095
printf rewrite: fix compilation
tertsdiepraam Oct 28, 2023
f117fc1
printf rewrite: fix compilation
tertsdiepraam Oct 28, 2023
bdfe5f1
Merge branch 'printf-rewrite' of github.com:tertsdiepraam/coreutils i…
tertsdiepraam Oct 28, 2023
198f7c7
printf: move number formatting to separate module
tertsdiepraam Nov 9, 2023
39c6758
uucore/format: move types for num_format
tertsdiepraam Nov 9, 2023
ee0e2c0
dd: use num_format::Float directly instead of printf
tertsdiepraam Nov 9, 2023
6481d63
uucore/format: implement single specifier formats
tertsdiepraam Nov 13, 2023
e7d58f6
seq: simplify and use new printf implementation
tertsdiepraam Nov 13, 2023
eaf5006
printf: parse arguments and handle escape codes
tertsdiepraam Nov 16, 2023
a45ff8c
printf: more flexible parsing of unparsed arguments
tertsdiepraam Nov 17, 2023
cd0c24a
printf: implement %b
tertsdiepraam Nov 17, 2023
f83e0d1
printf: accept multiple length parameters
tertsdiepraam Nov 17, 2023
f3da081
printf: support precision for integers
tertsdiepraam Nov 17, 2023
76eca8d
uucore/format: fix doctests
tertsdiepraam Nov 17, 2023
4aafb3f
printf: exit correctly on \c
tertsdiepraam Nov 17, 2023
955640a
printf: fix and test float formatting
tertsdiepraam Nov 17, 2023
fef84f7
printf: add emoji character test
tertsdiepraam Nov 17, 2023
ce18e0a
printf: ignore hexadecimal floats test
tertsdiepraam Nov 17, 2023
5f2374b
printf: fix negative hex argument parsing
tertsdiepraam Nov 17, 2023
c43ee01
printf: allow precision in string
tertsdiepraam Nov 20, 2023
066d8ba
printf: coerce missing and invalid arguments to 0
tertsdiepraam Nov 20, 2023
68d036c
printf: basic support for unicode escape sequences
tertsdiepraam Nov 20, 2023
6d2698b
Merge branch 'main' into printf-rewrite
tertsdiepraam Nov 20, 2023
5c04283
printf: address fmt, clippy, spelling and failing test
tertsdiepraam Nov 20, 2023
07aaf61
Merge branch 'main' into printf-rewrite
sylvestre Nov 21, 2023
0822511
test/printf: ignoring rounding up to 2
tertsdiepraam Nov 21, 2023
4b9fca8
Merge branch 'printf-rewrite' of github.com:tertsdiepraam/coreutils i…
tertsdiepraam Nov 21, 2023
e95add7
uucore/format: fix license headers and improve docs
tertsdiepraam Nov 22, 2023
8eb66ab
printf: remove whitespace, remove redundant spelling ignore and rever…
tertsdiepraam Nov 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/uu/dd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/dd.rs"
clap = { workspace = true }
gcd = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true, features = ["memo", "quoting-style"] }
uucore = { workspace = true, features = ["format", "quoting-style"] }

[target.'cfg(any(target_os = "linux"))'.dependencies]
nix = { workspace = true, features = ["fs"] }
Expand Down
15 changes: 12 additions & 3 deletions src/uu/dd/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use std::sync::mpsc;
use std::time::Duration;

use uucore::error::UResult;
use uucore::memo::sprintf;
use uucore::{
error::UResult,
format::num_format::{FloatVariant, Formatter},
};

use crate::numbers::{to_magnitude_and_suffix, SuffixType};

Expand Down Expand Up @@ -152,7 +154,14 @@
let (carriage_return, newline) = if rewrite { ("\r", "") } else { ("", "\n") };

// The duration should be formatted as in `printf %g`.
let duration_str = sprintf("%g", &[duration.to_string()])?;
let mut duration_str = Vec::new();
uucore::format::num_format::Float {
variant: FloatVariant::Shortest,
..Default::default()
}
.fmt(&mut duration_str, duration)?;

Check warning on line 162 in src/uu/dd/src/progress.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/dd/src/progress.rs#L162

Added line #L162 was not covered by tests
// We assume that printf will output valid UTF-8
let duration_str = std::str::from_utf8(&duration_str).unwrap();

// If the number of bytes written is sufficiently large, then
// print a more concise representation of the number, like
Expand Down
2 changes: 1 addition & 1 deletion src/uu/printf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ path = "src/printf.rs"

[dependencies]
clap = { workspace = true }
uucore = { workspace = true, features = ["memo", "quoting-style"] }
uucore = { workspace = true, features = ["format", "quoting-style"] }

[[bin]]
name = "printf"
Expand Down
27 changes: 23 additions & 4 deletions src/uu/printf/src/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
// spell-checker:ignore (change!) each's
// spell-checker:ignore (ToDO) LONGHELP FORMATSTRING templating parameterizing formatstr

use std::io::stdout;
use std::ops::ControlFlow;

use clap::{crate_version, Arg, ArgAction, Command};
use uucore::error::{UResult, UUsageError};
use uucore::memo::printf;
use uucore::format::{parse_spec_and_escape, FormatArgument};
use uucore::{format_usage, help_about, help_section, help_usage};

const VERSION: &str = "version";
Expand All @@ -30,12 +33,28 @@
let format_string = matches
.get_one::<String>(options::FORMATSTRING)
.ok_or_else(|| UUsageError::new(1, "missing operand"))?;
let values: Vec<String> = match matches.get_many::<String>(options::ARGUMENT) {
Some(s) => s.map(|s| s.to_string()).collect(),

let values: Vec<_> = match matches.get_many::<String>(options::ARGUMENT) {
Some(s) => s.map(|s| FormatArgument::Unparsed(s.to_string())).collect(),
None => vec![],
};

printf(format_string, &values[..])?;
let mut args = values.iter().peekable();
for item in parse_spec_and_escape(format_string.as_ref()) {
match item?.write(stdout(), &mut args)? {
cakebaker marked this conversation as resolved.
Show resolved Hide resolved
ControlFlow::Continue(()) => {}
ControlFlow::Break(()) => return Ok(()),
};
}

while args.peek().is_some() {
for item in parse_spec_and_escape(format_string.as_ref()) {
match item?.write(stdout(), &mut args)? {
ControlFlow::Continue(()) => {}
ControlFlow::Break(()) => return Ok(()),

Check warning on line 54 in src/uu/printf/src/printf.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/printf/src/printf.rs#L54

Added line #L54 was not covered by tests
};
}
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/uu/seq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bigdecimal = { workspace = true }
clap = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
uucore = { workspace = true, features = ["memo", "quoting-style"] }
uucore = { workspace = true, features = ["format", "quoting-style"] }

[[bin]]
name = "seq"
Expand Down
54 changes: 5 additions & 49 deletions src/uu/seq/src/extendedbigdecimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ use std::fmt::Display;
use std::ops::Add;

use bigdecimal::BigDecimal;
use num_bigint::BigInt;
use num_bigint::ToBigInt;
use num_traits::One;
use num_traits::Zero;

use crate::extendedbigint::ExtendedBigInt;

#[derive(Debug, Clone)]
pub enum ExtendedBigDecimal {
/// Arbitrary precision floating point number.
Expand Down Expand Up @@ -72,53 +67,14 @@ pub enum ExtendedBigDecimal {
Nan,
}

/// The smallest integer greater than or equal to this number.
fn ceil(x: BigDecimal) -> BigInt {
if x.is_integer() {
// Unwrapping the Option because it always returns Some
x.to_bigint().unwrap()
} else {
(x + BigDecimal::one().half()).round(0).to_bigint().unwrap()
}
}

/// The largest integer less than or equal to this number.
fn floor(x: BigDecimal) -> BigInt {
if x.is_integer() {
// Unwrapping the Option because it always returns Some
x.to_bigint().unwrap()
} else {
(x - BigDecimal::one().half()).round(0).to_bigint().unwrap()
}
}

impl ExtendedBigDecimal {
/// The smallest integer greater than or equal to this number.
pub fn ceil(self) -> ExtendedBigInt {
match self {
Self::BigDecimal(x) => ExtendedBigInt::BigInt(ceil(x)),
other => From::from(other),
}
#[cfg(test)]
pub fn zero() -> Self {
Self::BigDecimal(0.into())
}

/// The largest integer less than or equal to this number.
pub fn floor(self) -> ExtendedBigInt {
match self {
Self::BigDecimal(x) => ExtendedBigInt::BigInt(floor(x)),
other => From::from(other),
}
}
}

impl From<ExtendedBigInt> for ExtendedBigDecimal {
fn from(big_int: ExtendedBigInt) -> Self {
match big_int {
ExtendedBigInt::BigInt(n) => Self::BigDecimal(BigDecimal::from(n)),
ExtendedBigInt::Infinity => Self::Infinity,
ExtendedBigInt::MinusInfinity => Self::MinusInfinity,
ExtendedBigInt::MinusZero => Self::MinusZero,
ExtendedBigInt::Nan => Self::Nan,
}
pub fn one() -> Self {
Self::BigDecimal(1.into())
}
}

Expand Down
214 changes: 0 additions & 214 deletions src/uu/seq/src/extendedbigint.rs

This file was deleted.

Loading
Loading