Skip to content

Commit

Permalink
Merge pull request uutils#5357 from zhitkoff/issue5334-uucore-parse-r-q
Browse files Browse the repository at this point in the history
Implement SI prefixes R and Q
  • Loading branch information
sylvestre committed Oct 18, 2023
2 parents adde9ab + abc9536 commit 4573eb6
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ fn parse_bytes_no_x(full: &str, s: &str) -> Result<u64, ParseError> {
..Default::default()
};
let (num, multiplier) = match (s.find('c'), s.rfind('w'), s.rfind('b')) {
(None, None, None) => match parser.parse(s) {
(None, None, None) => match parser.parse_u64(s) {
Ok(n) => (n, 1),
Err(ParseSizeError::InvalidSuffix(_) | ParseSizeError::ParseFailure(_)) => {
return Err(ParseError::InvalidNumber(full.to_string()))
Expand Down
6 changes: 3 additions & 3 deletions src/uu/df/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{env, fmt};

use uucore::{
display::Quotable,
parse_size::{parse_size, ParseSizeError},
parse_size::{parse_size_u64, ParseSizeError},
};

/// The first ten powers of 1024.
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Default for BlockSize {
pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSizeError> {
if matches.contains_id(OPT_BLOCKSIZE) {
let s = matches.get_one::<String>(OPT_BLOCKSIZE).unwrap();
let bytes = parse_size(s)?;
let bytes = parse_size_u64(s)?;

if bytes > 0 {
Ok(BlockSize::Bytes(bytes))
Expand All @@ -184,7 +184,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
fn block_size_from_env() -> Option<u64> {
for env_var in ["DF_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
if let Ok(env_size) = env::var(env_var) {
if let Ok(size) = parse_size(&env_size) {
if let Ok(size) = parse_size_u64(&env_size) {
return Some(size);
} else {
return None;
Expand Down
8 changes: 4 additions & 4 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use uucore::error::FromIo;
use uucore::error::{set_exit_code, UError, UResult};
use uucore::line_ending::LineEnding;
use uucore::parse_glob;
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::{
crash, format_usage, help_about, help_section, help_usage, show, show_error, show_warning,
};
Expand Down Expand Up @@ -256,12 +256,12 @@ fn get_file_info(path: &Path) -> Option<FileInfo> {

fn read_block_size(s: Option<&str>) -> u64 {
if let Some(s) = s {
parse_size(s)
parse_size_u64(s)
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(&e, s, options::BLOCK_SIZE)))
} else {
for env_var in ["DU_BLOCK_SIZE", "BLOCK_SIZE", "BLOCKSIZE"] {
if let Ok(env_size) = env::var(env_var) {
if let Ok(v) = parse_size(&env_size) {
if let Ok(v) = parse_size_u64(&env_size) {
return v;
}
}
Expand Down Expand Up @@ -946,7 +946,7 @@ impl FromStr for Threshold {
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let offset = usize::from(s.starts_with(&['-', '+'][..]));

let size = parse_size(&s[offset..])?;
let size = parse_size_u64(&s[offset..])?;

if s.starts_with('-') {
// Threshold of '-0' excludes everything besides 0 sized entries
Expand Down
4 changes: 2 additions & 2 deletions src/uu/head/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// file that was distributed with this source code.

use std::ffi::OsString;
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};

#[derive(PartialEq, Eq, Debug)]
pub enum ParseError {
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn parse_num(src: &str) -> Result<(u64, bool), ParseSizeError> {
if trimmed_string.is_empty() {
Ok((0, all_but_last))
} else {
parse_size(trimmed_string).map(|n| (n, all_but_last))
parse_size_u64(trimmed_string).map(|n| (n, all_but_last))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use uucore::{
error::{set_exit_code, UError, UResult},
format_usage,
fs::display_permissions,
parse_size::parse_size,
parse_size::parse_size_u64,
version_cmp::version_cmp,
};
use uucore::{help_about, help_section, help_usage, parse_glob, show, show_error, show_warning};
Expand Down Expand Up @@ -781,7 +781,7 @@ impl Config {
};

let block_size: Option<u64> = if !opt_si && !opt_hr && !raw_bs.is_empty() {
match parse_size(&raw_bs.to_string_lossy()) {
match parse_size_u64(&raw_bs.to_string_lossy()) {
Ok(size) => Some(size),
Err(_) => {
show!(LsError::BlockSizeParseError(
Expand Down
4 changes: 2 additions & 2 deletions src/uu/od/src/parse_nrofbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};

pub fn parse_number_of_bytes(s: &str) -> Result<u64, ParseSizeError> {
let mut start = 0;
Expand All @@ -15,7 +15,7 @@ pub fn parse_number_of_bytes(s: &str) -> Result<u64, ParseSizeError> {
} else if s.starts_with('0') {
radix = 8;
} else {
return parse_size(&s[start..]);
return parse_size_u64(&s[start..]);
}

let mut ends_with = s.chars().rev();
Expand Down
4 changes: 2 additions & 2 deletions src/uu/shred/src/shred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::os::unix::prelude::PermissionsExt;
use std::path::{Path, PathBuf};
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size;
use uucore::parse_size::parse_size_u64;
use uucore::{format_usage, help_about, help_section, help_usage, show, show_error, show_if_err};

const ABOUT: &str = help_about!("shred.md");
Expand Down Expand Up @@ -319,7 +319,7 @@ pub fn uu_app() -> Command {
fn get_size(size_str_opt: Option<String>) -> Option<u64> {
size_str_opt
.as_ref()
.and_then(|size| parse_size(size.as_str()).ok())
.and_then(|size| parse_size_u64(size.as_str()).ok())
.or_else(|| {
if let Some(size) = size_str_opt {
show_error!("invalid file size: {}", size.quote());
Expand Down
24 changes: 12 additions & 12 deletions src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::path::Path;
use std::u64;
use uucore::display::Quotable;
use uucore::error::{FromIo, UIoError, UResult, USimpleError, UUsageError};
use uucore::parse_size::{parse_size, parse_size_max, ParseSizeError};
use uucore::parse_size::{parse_size_u64, parse_size_u64_max, ParseSizeError};
use uucore::uio_error;
use uucore::{format_usage, help_about, help_section, help_usage};

Expand Down Expand Up @@ -503,7 +503,7 @@ impl NumberType {
let parts: Vec<&str> = s.split('/').collect();
match &parts[..] {
[n_str] => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
if num_chunks > 0 {
Ok(Self::Bytes(num_chunks))
Expand All @@ -512,39 +512,39 @@ impl NumberType {
}
}
[k_str, n_str] if !k_str.starts_with('l') && !k_str.starts_with('r') => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
let chunk_number = parse_size(k_str)
let chunk_number = parse_size_u64(k_str)
.map_err(|_| NumberTypeError::ChunkNumber(k_str.to_string()))?;
if is_invalid_chunk(chunk_number, num_chunks) {
return Err(NumberTypeError::ChunkNumber(k_str.to_string()));
}
Ok(Self::KthBytes(chunk_number, num_chunks))
}
["l", n_str] => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
Ok(Self::Lines(num_chunks))
}
["l", k_str, n_str] => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
let chunk_number = parse_size(k_str)
let chunk_number = parse_size_u64(k_str)
.map_err(|_| NumberTypeError::ChunkNumber(k_str.to_string()))?;
if is_invalid_chunk(chunk_number, num_chunks) {
return Err(NumberTypeError::ChunkNumber(k_str.to_string()));
}
Ok(Self::KthLines(chunk_number, num_chunks))
}
["r", n_str] => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
Ok(Self::RoundRobin(num_chunks))
}
["r", k_str, n_str] => {
let num_chunks = parse_size(n_str)
let num_chunks = parse_size_u64(n_str)
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
let chunk_number = parse_size(k_str)
let chunk_number = parse_size_u64(k_str)
.map_err(|_| NumberTypeError::ChunkNumber(k_str.to_string()))?;
if is_invalid_chunk(chunk_number, num_chunks) {
return Err(NumberTypeError::ChunkNumber(k_str.to_string()));
Expand Down Expand Up @@ -616,7 +616,7 @@ impl Strategy {
error: fn(ParseSizeError) -> StrategyError,
) -> Result<Strategy, StrategyError> {
let s = matches.get_one::<String>(option).unwrap();
let n = parse_size_max(s).map_err(error)?;
let n = parse_size_u64_max(s).map_err(error)?;
if n > 0 {
Ok(strategy(n))
} else {
Expand All @@ -635,7 +635,7 @@ impl Strategy {
matches.value_source(OPT_NUMBER) == Some(ValueSource::CommandLine),
) {
(Some(v), false, false, false, false) => {
let v = parse_size_max(v).map_err(|_| {
let v = parse_size_u64_max(v).map_err(|_| {
StrategyError::Lines(ParseSizeError::ParseFailure(v.to_string()))
})?;
if v > 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/uu/stdbuf/src/stdbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::process;
use tempfile::tempdir;
use tempfile::TempDir;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::parse_size;
use uucore::parse_size::parse_size_u64;
use uucore::{crash, format_usage, help_about, help_section, help_usage};

const ABOUT: &str = help_about!("stdbuf.md");
Expand Down Expand Up @@ -101,7 +101,7 @@ fn check_option(matches: &ArgMatches, name: &str) -> Result<BufferType, ProgramO
Ok(BufferType::Line)
}
}
x => parse_size(x).map_or_else(
x => parse_size_u64(x).map_or_else(
|e| crash!(125, "invalid mode {}", e),
|m| {
Ok(BufferType::Size(m.try_into().map_err(|_| {
Expand Down
4 changes: 2 additions & 2 deletions src/uu/tail/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::ffi::OsString;
use std::io::IsTerminal;
use std::time::Duration;
use uucore::error::{UResult, USimpleError, UUsageError};
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::{format_usage, help_about, help_usage, show_warning};

const ABOUT: &str = help_about!("tail.md");
Expand Down Expand Up @@ -414,7 +414,7 @@ fn parse_num(src: &str) -> Result<Signum, ParseSizeError> {
}
}

match parse_size(size_string) {
match parse_size_u64(size_string) {
Ok(n) => match (n, starting_with) {
(0, true) => Ok(Signum::PlusZero),
(0, false) => Ok(Signum::MinusZero),
Expand Down
6 changes: 3 additions & 3 deletions src/uu/truncate/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::os::unix::fs::FileTypeExt;
use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::parse_size::{parse_size_u64, ParseSizeError};
use uucore::{format_usage, help_about, help_section, help_usage};

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -380,7 +380,7 @@ fn is_modifier(c: char) -> bool {

/// Parse a size string with optional modifier symbol as its first character.
///
/// A size string is as described in [`parse_size`]. The first character
/// A size string is as described in [`parse_size_u64`]. The first character
/// of `size_string` might be a modifier symbol, like `'+'` or
/// `'<'`. The first element of the pair returned by this function
/// indicates which modifier symbol was present, or
Expand All @@ -406,7 +406,7 @@ fn parse_mode_and_size(size_string: &str) -> Result<TruncateMode, ParseSizeError
if is_modifier(c) {
size_string = &size_string[1..];
}
parse_size(size_string).map(match c {
parse_size_u64(size_string).map(match c {
'+' => TruncateMode::Extend,
'-' => TruncateMode::Reduce,
'<' => TruncateMode::AtMost,
Expand Down
Loading

0 comments on commit 4573eb6

Please sign in to comment.