mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Make the default int an i64 (#3428)
* Make the default int an i64 * fmt * Fix random integer * Treat pids as i64 for now
This commit is contained in:
@ -106,6 +106,12 @@ pub fn autoview(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
} => {
|
||||
out!("{}", n);
|
||||
}
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::BigInt(n)),
|
||||
..
|
||||
} => {
|
||||
out!("{}", n);
|
||||
}
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Decimal(n)),
|
||||
..
|
||||
|
@ -102,7 +102,7 @@ pub fn process_row(
|
||||
|
||||
pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value {
|
||||
let mut dict = TaggedDictBuilder::new(item.tag());
|
||||
dict.insert_untagged("index", UntaggedValue::int(index));
|
||||
dict.insert_untagged("index", UntaggedValue::int(index as i64));
|
||||
dict.insert_value("item", item);
|
||||
|
||||
dict.into_value()
|
||||
|
@ -1,5 +1,4 @@
|
||||
use crate::prelude::*;
|
||||
use bigdecimal::Zero;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::hir::Operator;
|
||||
@ -81,7 +80,7 @@ impl RangeIterator {
|
||||
};
|
||||
|
||||
let end = match range.to.0.item {
|
||||
Primitive::Nothing => Primitive::Int(u64::MAX.into()),
|
||||
Primitive::Nothing => Primitive::Int(i64::MAX),
|
||||
x => x,
|
||||
};
|
||||
|
||||
@ -121,11 +120,11 @@ impl Iterator for RangeIterator {
|
||||
(
|
||||
UntaggedValue::Primitive(Primitive::Decimal(x)),
|
||||
UntaggedValue::Primitive(Primitive::Int(y)),
|
||||
) => x.cmp(&(BigDecimal::zero() + y)),
|
||||
) => x.cmp(&(BigDecimal::from(*y))),
|
||||
(
|
||||
UntaggedValue::Primitive(Primitive::Int(x)),
|
||||
UntaggedValue::Primitive(Primitive::Decimal(y)),
|
||||
) => (BigDecimal::zero() + x).cmp(y),
|
||||
) => (BigDecimal::from(*x)).cmp(y),
|
||||
_ => {
|
||||
self.done = true;
|
||||
return Some(
|
||||
|
@ -64,7 +64,7 @@ fn process_row(
|
||||
..
|
||||
} = s
|
||||
{
|
||||
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
||||
let byte_format = InlineShape::format_bytes(fs, Some(&format.item));
|
||||
let byte_value = Value::from(byte_format.1);
|
||||
Ok(input
|
||||
.replace_data_at_column_path(&field, byte_value)
|
||||
|
@ -35,7 +35,7 @@ fn convert_json_value_to_nu_value(v: &nu_json::Value, tag: impl Into<Tag>) -> Va
|
||||
nu_json::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag),
|
||||
nu_json::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag),
|
||||
nu_json::Value::F64(n) => UntaggedValue::decimal_from_float(*n, span).into_value(&tag),
|
||||
nu_json::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag),
|
||||
nu_json::Value::U64(n) => UntaggedValue::big_int(*n).into_value(&tag),
|
||||
nu_json::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag),
|
||||
nu_json::Value::String(s) => {
|
||||
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)
|
||||
|
@ -90,14 +90,14 @@ impl WholeStreamCommand for SubCommand {
|
||||
description: "convert a number to a nushell binary primitive",
|
||||
example: "echo 1 | into binary",
|
||||
result: Some(vec![
|
||||
UntaggedValue::binary(BigInt::from(1).to_bytes_le().1).into()
|
||||
UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "convert a boolean to a nushell binary primitive",
|
||||
example: "echo $true | into binary",
|
||||
result: Some(vec![
|
||||
UntaggedValue::binary(BigInt::from(1).to_bytes_le().1).into()
|
||||
UntaggedValue::binary(i64::from(1).to_le_bytes().to_vec()).into()
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
@ -152,7 +152,17 @@ fn into_binary(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
.to_action_stream())
|
||||
}
|
||||
|
||||
pub fn bigint_to_endian(n: &BigInt) -> Vec<u8> {
|
||||
fn int_to_endian(n: i64) -> Vec<u8> {
|
||||
if cfg!(target_endian = "little") {
|
||||
// eprintln!("Little Endian");
|
||||
n.to_le_bytes().to_vec()
|
||||
} else {
|
||||
// eprintln!("Big Endian");
|
||||
n.to_be_bytes().to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
fn bigint_to_endian(n: &BigInt) -> Vec<u8> {
|
||||
if cfg!(target_endian = "little") {
|
||||
// eprintln!("Little Endian");
|
||||
n.to_bytes_le().1
|
||||
@ -192,7 +202,8 @@ pub fn action(
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
Primitive::Int(n_ref) => bigint_to_endian(n_ref),
|
||||
Primitive::Int(n_ref) => int_to_endian(*n_ref),
|
||||
Primitive::BigInt(n_ref) => bigint_to_endian(n_ref),
|
||||
Primitive::Decimal(dec) => match dec.to_bigint() {
|
||||
Some(n) => bigint_to_endian(&n),
|
||||
None => {
|
||||
@ -229,8 +240,8 @@ pub fn action(
|
||||
}
|
||||
}
|
||||
Primitive::Boolean(a_bool) => match a_bool {
|
||||
false => bigint_to_endian(&BigInt::from(0)),
|
||||
true => bigint_to_endian(&BigInt::from(1)),
|
||||
false => int_to_endian(0),
|
||||
true => int_to_endian(1),
|
||||
},
|
||||
Primitive::Date(a_date) => a_date.format("%c").to_string().as_bytes().to_vec(),
|
||||
Primitive::FilePath(a_filepath) => a_filepath
|
||||
|
@ -4,7 +4,7 @@ use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use num_bigint::{BigInt, ToBigInt};
|
||||
use num_bigint::ToBigInt;
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
@ -118,20 +118,34 @@ pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
}
|
||||
},
|
||||
Primitive::Decimal(dec) => match dec.to_bigint() {
|
||||
Some(n) => n,
|
||||
Some(n) => match n.to_i64() {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return Err(ShellError::unimplemented(
|
||||
"failed to convert decimal to int",
|
||||
));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(ShellError::unimplemented(
|
||||
"failed to convert decimal to int",
|
||||
));
|
||||
}
|
||||
},
|
||||
Primitive::Int(n_ref) => n_ref.to_owned(),
|
||||
Primitive::Int(n_ref) => *n_ref,
|
||||
Primitive::Boolean(a_bool) => match a_bool {
|
||||
false => BigInt::from(0),
|
||||
true => BigInt::from(1),
|
||||
false => 0,
|
||||
true => 1,
|
||||
},
|
||||
Primitive::Filesize(a_filesize) => match a_filesize.to_bigint() {
|
||||
Some(n) => n,
|
||||
Some(n) => match n.to_i64() {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return Err(ShellError::unimplemented(
|
||||
"failed to convert filesize to bigint",
|
||||
));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
return Err(ShellError::unimplemented(
|
||||
"failed to convert filesize to bigint",
|
||||
@ -154,13 +168,17 @@ pub fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
}
|
||||
}
|
||||
|
||||
fn int_from_string(a_string: &str, tag: &Tag) -> Result<BigInt, ShellError> {
|
||||
match a_string.parse::<BigInt>() {
|
||||
fn int_from_string(a_string: &str, tag: &Tag) -> Result<i64, ShellError> {
|
||||
match a_string.parse::<i64>() {
|
||||
Ok(n) => Ok(n),
|
||||
Err(_) => match a_string.parse::<f64>() {
|
||||
Ok(res_float) => match res_float.to_bigint() {
|
||||
Some(n) => Ok(n),
|
||||
None => Err(ShellError::unimplemented("failed to convert f64 to int")),
|
||||
Ok(f) => match f.to_i64() {
|
||||
Some(i) => Ok(i),
|
||||
None => Err(ShellError::labeled_error(
|
||||
"Could not convert string value to int",
|
||||
"original value",
|
||||
tag.clone(),
|
||||
)),
|
||||
},
|
||||
Err(_) => Err(ShellError::labeled_error(
|
||||
"Could not convert string value to int",
|
||||
|
@ -129,6 +129,13 @@ pub fn action(
|
||||
match &input.value {
|
||||
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
|
||||
Primitive::Int(int) => {
|
||||
if group_digits {
|
||||
format_int(*int) // int.to_formatted_string(*locale)
|
||||
} else {
|
||||
int.to_string()
|
||||
}
|
||||
}
|
||||
Primitive::BigInt(int) => {
|
||||
if group_digits {
|
||||
format_bigint(int) // int.to_formatted_string(*locale)
|
||||
} else {
|
||||
@ -141,7 +148,7 @@ pub fn action(
|
||||
Primitive::Date(a_date) => a_date.format("%c").to_string(),
|
||||
Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
|
||||
Primitive::Filesize(a_filesize) => {
|
||||
let byte_string = InlineShape::format_bytes(a_filesize, None);
|
||||
let byte_string = InlineShape::format_bytes(*a_filesize, None);
|
||||
byte_string.1
|
||||
}
|
||||
_ => {
|
||||
@ -162,6 +169,23 @@ pub fn action(
|
||||
}
|
||||
}
|
||||
|
||||
fn format_int(int: i64) -> String {
|
||||
format!("{}", int)
|
||||
|
||||
// TODO once platform-specific dependencies are stable (see Cargo.toml)
|
||||
// #[cfg(windows)]
|
||||
// {
|
||||
// int.to_formatted_string(&Locale::en)
|
||||
// }
|
||||
// #[cfg(not(windows))]
|
||||
// {
|
||||
// match SystemLocale::default() {
|
||||
// Ok(locale) => int.to_formatted_string(&locale),
|
||||
// Err(_) => int.to_formatted_string(&Locale::en),
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
fn format_bigint(int: &BigInt) -> String {
|
||||
format!("{}", int)
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||
|
||||
pub struct Last;
|
||||
|
||||
@ -31,7 +31,7 @@ impl WholeStreamCommand for Last {
|
||||
Example {
|
||||
description: "Get the last row",
|
||||
example: "echo [1 2 3] | last",
|
||||
result: Some(vec![Value::from(UntaggedValue::from(BigInt::from(3)))]),
|
||||
result: Some(vec![UntaggedValue::int(3).into()]),
|
||||
},
|
||||
Example {
|
||||
description: "Get the last three rows",
|
||||
|
@ -94,7 +94,7 @@ impl Iterator for CountIterator {
|
||||
input.count()
|
||||
};
|
||||
|
||||
Some(UntaggedValue::int(length).into_value(self.tag.clone()))
|
||||
Some(UntaggedValue::int(length as i64).into_value(self.tag.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,9 @@ impl WholeStreamCommand for SubCommand {
|
||||
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let mapped = args.input.map(move |val| match val.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(val)) => {
|
||||
UntaggedValue::int(val.magnitude().clone()).into()
|
||||
UntaggedValue::Primitive(Primitive::Int(val)) => UntaggedValue::int(val.abs()).into(),
|
||||
UntaggedValue::Primitive(Primitive::BigInt(val)) => {
|
||||
UntaggedValue::big_int(val.magnitude().clone()).into()
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Decimal(val)) => {
|
||||
UntaggedValue::decimal(val.abs()).into()
|
||||
|
@ -5,10 +5,7 @@ use crate::commands::math::utils::run_with_function;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
hir::{convert_number_to_u64, Number, Operator},
|
||||
Primitive, Signature, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
|
||||
|
||||
use bigdecimal::FromPrimitive;
|
||||
|
||||
@ -47,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
fn to_byte(value: &Value) -> Option<Value> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(num)) => {
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@ -79,7 +76,7 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => UntaggedValue::int(num.clone()).into_untagged_value(),
|
||||
} => UntaggedValue::int(*num as i64).into_untagged_value(),
|
||||
other => other.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
@ -100,15 +97,18 @@ pub fn average(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => {
|
||||
let left = UntaggedValue::from(Primitive::Int(num));
|
||||
let left = UntaggedValue::from(Primitive::Int(num as i64));
|
||||
let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
|
||||
|
||||
match result {
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => {
|
||||
let number = Number::Decimal(result);
|
||||
let number = convert_number_to_u64(&number);
|
||||
Ok(UntaggedValue::filesize(number).into_value(name))
|
||||
}
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => match result.to_u64() {
|
||||
Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)),
|
||||
None => Err(ShellError::labeled_error(
|
||||
"could not calculate average of non-integer or unrelated types",
|
||||
"source",
|
||||
name,
|
||||
)),
|
||||
},
|
||||
Ok(_) => Err(ShellError::labeled_error(
|
||||
"could not calculate average of non-integer or unrelated types",
|
||||
"source",
|
||||
|
@ -23,7 +23,13 @@ impl WholeStreamCommand for SubCommand {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let input = args.input;
|
||||
|
||||
run_with_numerical_functions_on_stream(input, ceil_big_int, ceil_big_decimal, ceil_default)
|
||||
run_with_numerical_functions_on_stream(
|
||||
input,
|
||||
ceil_int,
|
||||
ceil_big_int,
|
||||
ceil_big_decimal,
|
||||
ceil_default,
|
||||
)
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
@ -39,17 +45,28 @@ impl WholeStreamCommand for SubCommand {
|
||||
}
|
||||
}
|
||||
|
||||
fn ceil_big_int(val: BigInt) -> Value {
|
||||
fn ceil_int(val: i64) -> Value {
|
||||
UntaggedValue::int(val).into()
|
||||
}
|
||||
|
||||
fn ceil_big_int(val: BigInt) -> Value {
|
||||
UntaggedValue::big_int(val).into()
|
||||
}
|
||||
|
||||
fn ceil_big_decimal(val: BigDecimal) -> Value {
|
||||
let mut maybe_ceiled = val.round(0);
|
||||
if maybe_ceiled < val {
|
||||
maybe_ceiled += BigDecimal::one();
|
||||
}
|
||||
let (ceiled, _) = maybe_ceiled.into_bigint_and_exponent();
|
||||
UntaggedValue::int(ceiled).into()
|
||||
let ceiling = maybe_ceiled.to_i64();
|
||||
|
||||
match ceiling {
|
||||
Some(x) => UntaggedValue::int(x).into(),
|
||||
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
|
||||
"Value too big to ceiling to an 64-bit integer",
|
||||
))
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ceil_default(_: UntaggedValue) -> Value {
|
||||
|
@ -25,6 +25,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
|
||||
run_with_numerical_functions_on_stream(
|
||||
input,
|
||||
floor_int,
|
||||
floor_big_int,
|
||||
floor_big_decimal,
|
||||
floor_default,
|
||||
@ -36,25 +37,29 @@ impl WholeStreamCommand for SubCommand {
|
||||
description: "Apply the floor function to a list of numbers",
|
||||
example: "echo [1.5 2.3 -3.1] | math floor",
|
||||
result: Some(vec![
|
||||
UntaggedValue::int(1).into(),
|
||||
UntaggedValue::int(2).into(),
|
||||
UntaggedValue::int(-4).into(),
|
||||
UntaggedValue::big_int(1).into(),
|
||||
UntaggedValue::big_int(2).into(),
|
||||
UntaggedValue::big_int(-4).into(),
|
||||
]),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn floor_big_int(val: BigInt) -> Value {
|
||||
fn floor_int(val: i64) -> Value {
|
||||
UntaggedValue::int(val).into()
|
||||
}
|
||||
|
||||
fn floor_big_int(val: BigInt) -> Value {
|
||||
UntaggedValue::big_int(val).into()
|
||||
}
|
||||
|
||||
fn floor_big_decimal(val: BigDecimal) -> Value {
|
||||
let mut maybe_floored = val.round(0);
|
||||
if maybe_floored > val {
|
||||
maybe_floored -= BigDecimal::one();
|
||||
}
|
||||
let (floored, _) = maybe_floored.into_bigint_and_exponent();
|
||||
UntaggedValue::int(floored).into()
|
||||
UntaggedValue::big_int(floored).into()
|
||||
}
|
||||
|
||||
fn floor_default(_: UntaggedValue) -> Value {
|
||||
|
@ -4,10 +4,7 @@ use crate::prelude::*;
|
||||
use bigdecimal::FromPrimitive;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
hir::{convert_number_to_u64, Number, Operator},
|
||||
Primitive, Signature, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{hir::Operator, Primitive, Signature, UntaggedValue, Value};
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
@ -124,14 +121,21 @@ fn compute_average(values: &[Value], name: impl Into<Tag>) -> Result<Value, Shel
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => {
|
||||
let left = UntaggedValue::from(Primitive::Int(num));
|
||||
let left = UntaggedValue::from(Primitive::Int(num as i64));
|
||||
let result = nu_data::value::compute_values(Operator::Divide, &left, &total_rows);
|
||||
|
||||
match result {
|
||||
Ok(UntaggedValue::Primitive(Primitive::Decimal(result))) => {
|
||||
let number = Number::Decimal(result);
|
||||
let number = convert_number_to_u64(&number);
|
||||
Ok(UntaggedValue::filesize(number).into_value(name))
|
||||
let (bi, _) = result.as_bigint_and_exponent();
|
||||
let number = bi.to_u64();
|
||||
match number {
|
||||
Some(number) => Ok(UntaggedValue::filesize(number).into_value(name)),
|
||||
None => Err(ShellError::labeled_error(
|
||||
"Can't convert to filesize",
|
||||
"can't convert to filesize",
|
||||
name,
|
||||
)),
|
||||
}
|
||||
}
|
||||
Ok(_) => Err(ShellError::labeled_error(
|
||||
"could not calculate median of non-numeric or unrelated types",
|
||||
|
@ -36,7 +36,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
fn to_byte(value: &Value) -> Option<Value> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(num)) => {
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@ -59,7 +59,7 @@ pub fn product(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => UntaggedValue::int(num.clone()).into_untagged_value(),
|
||||
} => UntaggedValue::int(*num as i64).into_untagged_value(),
|
||||
other => other.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
|
@ -62,7 +62,7 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
0
|
||||
};
|
||||
let mapped = input.map(move |val| match val.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(val)) => round_big_int(val),
|
||||
UntaggedValue::Primitive(Primitive::BigInt(val)) => round_big_int(val),
|
||||
UntaggedValue::Primitive(Primitive::Decimal(val)) => {
|
||||
round_big_decimal(val, precision.into())
|
||||
}
|
||||
@ -72,18 +72,22 @@ fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
}
|
||||
|
||||
fn round_big_int(val: BigInt) -> Value {
|
||||
UntaggedValue::int(val).into()
|
||||
UntaggedValue::big_int(val).into()
|
||||
}
|
||||
|
||||
fn round_big_decimal(val: BigDecimal, precision: i64) -> Value {
|
||||
if precision > 0 {
|
||||
UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into()
|
||||
} else {
|
||||
let (rounded, _) = val
|
||||
.with_scale(precision + 1)
|
||||
.round(precision)
|
||||
.as_bigint_and_exponent();
|
||||
UntaggedValue::int(rounded).into()
|
||||
let rounded = val.with_scale(precision + 1).round(precision).to_i64();
|
||||
|
||||
match rounded {
|
||||
Some(x) => UntaggedValue::int(x).into(),
|
||||
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
|
||||
"Number too larger to round to 64-bit int",
|
||||
))
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,10 +46,18 @@ fn operate(args: CommandArgs) -> OutputStream {
|
||||
fn sqrt_big_decimal(val: BigDecimal) -> Value {
|
||||
let squared = val.sqrt();
|
||||
match squared {
|
||||
None => UntaggedValue::Error(ShellError::unexpected("Cant square root a negative number"))
|
||||
.into(),
|
||||
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
|
||||
"Can't square root a negative number",
|
||||
))
|
||||
.into(),
|
||||
Some(val) if !val.is_integer() => UntaggedValue::decimal(val.normalized()).into(),
|
||||
Some(val) => UntaggedValue::int(val.with_scale(0).as_bigint_and_exponent().0).into(),
|
||||
Some(val) => match val.to_i64() {
|
||||
Some(x) => UntaggedValue::int(x).into(),
|
||||
None => UntaggedValue::Error(ShellError::untagged_runtime_error(
|
||||
"Value too large to convert to 64-bit integer",
|
||||
))
|
||||
.into(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
fn to_byte(value: &Value) -> Option<Value> {
|
||||
match &value.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(num)) => {
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(num.clone())).into_untagged_value())
|
||||
Some(UntaggedValue::Primitive(Primitive::Filesize(*num as u64)).into_untagged_value())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
@ -71,7 +71,7 @@ pub fn summation(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => UntaggedValue::int(num.clone()).into_untagged_value(),
|
||||
} => UntaggedValue::int(*num as i64).into_untagged_value(),
|
||||
other => other.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
|
@ -34,7 +34,9 @@ pub fn run_with_function(
|
||||
}
|
||||
}
|
||||
|
||||
pub type IntFunction = fn(val: BigInt) -> Value;
|
||||
pub type BigIntFunction = fn(val: BigInt) -> Value;
|
||||
|
||||
pub type IntFunction = fn(val: i64) -> Value;
|
||||
|
||||
pub type DecimalFunction = fn(val: BigDecimal) -> Value;
|
||||
|
||||
@ -43,11 +45,13 @@ pub type DefaultFunction = fn(val: UntaggedValue) -> Value;
|
||||
pub fn run_with_numerical_functions_on_stream(
|
||||
input: InputStream,
|
||||
int_function: IntFunction,
|
||||
big_int_function: BigIntFunction,
|
||||
decimal_function: DecimalFunction,
|
||||
default_function: DefaultFunction,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let mapped = input.map(move |val| match val.value {
|
||||
UntaggedValue::Primitive(Primitive::Int(val)) => int_function(val),
|
||||
UntaggedValue::Primitive(Primitive::BigInt(val)) => big_int_function(val),
|
||||
UntaggedValue::Primitive(Primitive::Decimal(val)) => decimal_function(val),
|
||||
other => default_function(other),
|
||||
});
|
||||
|
@ -120,7 +120,7 @@ fn sum_of_squares(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
|
||||
value: UntaggedValue::Primitive(Primitive::Filesize(num)),
|
||||
..
|
||||
} => {
|
||||
UntaggedValue::from(Primitive::Int(num.clone()))
|
||||
UntaggedValue::from(Primitive::Int(*num as i64))
|
||||
},
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(num),
|
||||
|
@ -62,9 +62,9 @@ pub fn integer(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
};
|
||||
|
||||
let (min, max) = if let Some(range) = &cmd_args.range {
|
||||
(range.min_u64()?, range.max_u64()?)
|
||||
(range.min_i64()?, range.max_i64()?)
|
||||
} else {
|
||||
(0, u64::MAX)
|
||||
(0, i64::MAX)
|
||||
};
|
||||
|
||||
match min.cmp(&max) {
|
||||
@ -83,7 +83,7 @@ pub fn integer(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let mut thread_rng = thread_rng();
|
||||
// add 1 to max, because gen_range is right-exclusive
|
||||
let max = max.saturating_add(1);
|
||||
let result: u64 = thread_rng.gen_range(min, max);
|
||||
let result: i64 = thread_rng.gen_range(min, max);
|
||||
|
||||
Ok(OutputStream::one(
|
||||
UntaggedValue::int(result).into_value(Tag::unknown()),
|
||||
|
@ -109,6 +109,14 @@ pub fn action(
|
||||
match &input.value {
|
||||
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
|
||||
Primitive::Int(int) => {
|
||||
if group_digits {
|
||||
format_int(*int) // int.to_formatted_string(*locale)
|
||||
} else {
|
||||
int.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
Primitive::BigInt(int) => {
|
||||
if group_digits {
|
||||
format_bigint(int) // int.to_formatted_string(*locale)
|
||||
} else {
|
||||
@ -121,7 +129,7 @@ pub fn action(
|
||||
Primitive::Date(a_date) => a_date.format("%c").to_string(),
|
||||
Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
|
||||
Primitive::Filesize(a_filesize) => {
|
||||
let byte_string = InlineShape::format_bytes(a_filesize, None);
|
||||
let byte_string = InlineShape::format_bytes(*a_filesize, None);
|
||||
byte_string.1
|
||||
}
|
||||
_ => {
|
||||
@ -142,6 +150,23 @@ pub fn action(
|
||||
}
|
||||
}
|
||||
|
||||
fn format_int(int: i64) -> String {
|
||||
format!("{}", int)
|
||||
|
||||
// TODO once platform-specific dependencies are stable (see Cargo.toml)
|
||||
// #[cfg(windows)]
|
||||
// {
|
||||
// int.to_formatted_string(&Locale::en)
|
||||
// }
|
||||
// #[cfg(not(windows))]
|
||||
// {
|
||||
// match SystemLocale::default() {
|
||||
// Ok(locale) => int.to_formatted_string(&locale),
|
||||
// Err(_) => int.to_formatted_string(&Locale::en),
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
fn format_bigint(int: &BigInt) -> String {
|
||||
format!("{}", int)
|
||||
|
||||
|
@ -147,12 +147,12 @@ fn action(
|
||||
|
||||
if *end {
|
||||
if let Some(result) = s[start_index..end_index].rfind(&**pattern) {
|
||||
Ok(UntaggedValue::int(result + start_index).into_value(tag))
|
||||
Ok(UntaggedValue::int(result as i64 + start_index as i64).into_value(tag))
|
||||
} else {
|
||||
Ok(UntaggedValue::int(-1).into_value(tag))
|
||||
}
|
||||
} else if let Some(result) = s[start_index..end_index].find(&**pattern) {
|
||||
Ok(UntaggedValue::int(result + start_index).into_value(tag))
|
||||
Ok(UntaggedValue::int(result as i64 + start_index as i64).into_value(tag))
|
||||
} else {
|
||||
Ok(UntaggedValue::int(-1).into_value(tag))
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {
|
||||
match &input.value {
|
||||
UntaggedValue::Primitive(Primitive::String(s)) => {
|
||||
Ok(UntaggedValue::int(s.len()).into_value(tag))
|
||||
Ok(UntaggedValue::int(s.len() as i64).into_value(tag))
|
||||
}
|
||||
other => {
|
||||
let got = format!("got {}", other.type_name());
|
||||
|
@ -9,9 +9,6 @@ use nu_protocol::{
|
||||
use nu_source::{Tag, Tagged};
|
||||
use nu_value_ext::ValueExt;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::Num;
|
||||
|
||||
struct Arguments {
|
||||
radix: Option<Tagged<u32>>,
|
||||
column_paths: Vec<ColumnPath>,
|
||||
@ -106,7 +103,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
|
||||
|
||||
let out = match trimmed {
|
||||
b if b.starts_with("0b") => {
|
||||
let num = match BigInt::from_str_radix(b.trim_start_matches("0b"), 2) {
|
||||
let num = match i64::from_str_radix(b.trim_start_matches("0b"), 2) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
@ -119,7 +116,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
|
||||
UntaggedValue::int(num)
|
||||
}
|
||||
h if h.starts_with("0x") => {
|
||||
let num = match BigInt::from_str_radix(h.trim_start_matches("0x"), 16) {
|
||||
let num = match i64::from_str_radix(h.trim_start_matches("0x"), 16) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
@ -132,7 +129,7 @@ fn action(input: &Value, tag: impl Into<Tag>, radix: u32) -> Result<Value, Shell
|
||||
UntaggedValue::int(num)
|
||||
}
|
||||
_ => {
|
||||
let num = match BigInt::from_str_radix(trimmed, radix) {
|
||||
let num = match i64::from_str_radix(trimmed, radix) {
|
||||
Ok(n) => n,
|
||||
Err(reason) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
|
@ -35,13 +35,23 @@ impl WholeStreamCommand for TermSize {
|
||||
match size {
|
||||
Some((w, h)) => {
|
||||
if wide && !tall {
|
||||
Ok(ActionStream::one(UntaggedValue::int(w).into_value(tag)))
|
||||
Ok(ActionStream::one(
|
||||
UntaggedValue::int(w as i64).into_value(tag),
|
||||
))
|
||||
} else if !wide && tall {
|
||||
Ok(ActionStream::one(UntaggedValue::int(h).into_value(tag)))
|
||||
Ok(ActionStream::one(
|
||||
UntaggedValue::int(h as i64).into_value(tag),
|
||||
))
|
||||
} else {
|
||||
let mut indexmap = IndexMap::with_capacity(2);
|
||||
indexmap.insert("width".to_string(), UntaggedValue::int(w).into_value(&tag));
|
||||
indexmap.insert("height".to_string(), UntaggedValue::int(h).into_value(&tag));
|
||||
indexmap.insert(
|
||||
"width".to_string(),
|
||||
UntaggedValue::int(w as i64).into_value(&tag),
|
||||
);
|
||||
indexmap.insert(
|
||||
"height".to_string(),
|
||||
UntaggedValue::int(h as i64).into_value(&tag),
|
||||
);
|
||||
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
|
||||
Ok(ActionStream::one(value))
|
||||
}
|
||||
|
@ -109,14 +109,12 @@ pub fn clone_tagged_value(v: &Value) -> Value {
|
||||
UntaggedValue::Primitive(Primitive::Decimal(f)) => {
|
||||
UntaggedValue::Primitive(Primitive::Decimal(f.clone()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
UntaggedValue::Primitive(Primitive::Int(i.clone()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => UntaggedValue::Primitive(Primitive::Int(*i)),
|
||||
UntaggedValue::Primitive(Primitive::FilePath(x)) => {
|
||||
UntaggedValue::Primitive(Primitive::FilePath(x.clone()))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Filesize(b)) => {
|
||||
UntaggedValue::Primitive(Primitive::Filesize(b.clone()))
|
||||
UntaggedValue::Primitive(Primitive::Filesize(*b))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Date(d)) => {
|
||||
UntaggedValue::Primitive(Primitive::Date(*d))
|
||||
|
@ -82,6 +82,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
|
||||
}
|
||||
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
serde_json::Value::Number(serde_json::Number::from(*i))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::BigInt(i)) => {
|
||||
serde_json::Value::Number(serde_json::Number::from(CoerceInto::<i64>::coerce_into(
|
||||
i.tagged(&v.tag),
|
||||
"converting to JSON number",
|
||||
@ -96,12 +99,9 @@ pub fn value_to_json_value(v: &Value) -> Result<serde_json::Value, ShellError> {
|
||||
UnspannedPathMember::String(string) => {
|
||||
Ok(serde_json::Value::String(string.clone()))
|
||||
}
|
||||
UnspannedPathMember::Int(int) => Ok(serde_json::Value::Number(
|
||||
serde_json::Number::from(CoerceInto::<i64>::coerce_into(
|
||||
int.tagged(&v.tag),
|
||||
"converting to JSON number",
|
||||
)?),
|
||||
)),
|
||||
UnspannedPathMember::Int(int) => {
|
||||
Ok(serde_json::Value::Number(serde_json::Number::from(*int)))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<serde_json::Value>, ShellError>>()?,
|
||||
),
|
||||
|
@ -50,7 +50,8 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
|
||||
UntaggedValue::Primitive(Primitive::Decimal(f)) => {
|
||||
toml::Value::Float(f.tagged(&v.tag).coerce_into("converting to TOML float")?)
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => toml::Value::Integer(*i),
|
||||
UntaggedValue::Primitive(Primitive::BigInt(i)) => {
|
||||
toml::Value::Integer(i.tagged(&v.tag).coerce_into("converting to TOML integer")?)
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Nothing) => {
|
||||
@ -65,10 +66,7 @@ fn helper(v: &Value) -> Result<toml::Value, ShellError> {
|
||||
path.iter()
|
||||
.map(|x| match &x.unspanned {
|
||||
UnspannedPathMember::String(string) => Ok(toml::Value::String(string.clone())),
|
||||
UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(
|
||||
int.tagged(&v.tag)
|
||||
.coerce_into("converting to TOML integer")?,
|
||||
)),
|
||||
UnspannedPathMember::Int(int) => Ok(toml::Value::Integer(*int)),
|
||||
})
|
||||
.collect::<Result<Vec<toml::Value>, ShellError>>()?,
|
||||
),
|
||||
|
@ -51,6 +51,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
})?))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::Int(i)) => {
|
||||
serde_yaml::Value::Number(serde_yaml::Number::from(*i))
|
||||
}
|
||||
UntaggedValue::Primitive(Primitive::BigInt(i)) => {
|
||||
serde_yaml::Value::Number(serde_yaml::Number::from(CoerceInto::<i64>::coerce_into(
|
||||
i.tagged(&v.tag),
|
||||
"converting to YAML number",
|
||||
@ -67,12 +70,9 @@ pub fn value_to_yaml_value(v: &Value) -> Result<serde_yaml::Value, ShellError> {
|
||||
UnspannedPathMember::String(string) => {
|
||||
out.push(serde_yaml::Value::String(string.clone()))
|
||||
}
|
||||
UnspannedPathMember::Int(int) => out.push(serde_yaml::Value::Number(
|
||||
serde_yaml::Number::from(CoerceInto::<i64>::coerce_into(
|
||||
int.tagged(&member.span),
|
||||
"converting to YAML number",
|
||||
)?),
|
||||
)),
|
||||
UnspannedPathMember::Int(int) => {
|
||||
out.push(serde_yaml::Value::Number(serde_yaml::Number::from(*int)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ fn uniq(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
UntaggedValue::Row(mut row) => {
|
||||
row.entries.insert(
|
||||
"count".to_string(),
|
||||
UntaggedValue::int(item.1).into_untagged_value(),
|
||||
UntaggedValue::int(item.1 as i64).into_untagged_value(),
|
||||
);
|
||||
Value {
|
||||
value: UntaggedValue::Row(row),
|
||||
@ -141,7 +141,7 @@ fn uniq(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
);
|
||||
map.insert(
|
||||
"count".to_string(),
|
||||
UntaggedValue::int(item.1).into_untagged_value(),
|
||||
UntaggedValue::int(item.1 as i64).into_untagged_value(),
|
||||
);
|
||||
Value {
|
||||
value: UntaggedValue::row(map),
|
||||
|
Reference in New Issue
Block a user