mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 20:03:54 +01:00
allow into int
to convert octal numbers and 0 padded strings (#6053)
* allow `into int` to convert octal numbers and 0 padded strings * added some tests in examples
This commit is contained in:
parent
58ee2bf06a
commit
1f01677b7b
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -3168,7 +3168,7 @@ checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "papergrid"
|
name = "papergrid"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
source = "git+https://github.com/zhiburt/tabled?rev=cca285b1fc0eac48b8a386c8884092d894d0e7ae#cca285b1fc0eac48b8a386c8884092d894d0e7ae"
|
source = "git+https://github.com/zhiburt/tabled?rev=9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278#9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi-str 0.1.1",
|
"ansi-str 0.1.1",
|
||||||
"bytecount",
|
"bytecount",
|
||||||
@ -4786,7 +4786,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "tabled"
|
name = "tabled"
|
||||||
version = "0.7.0"
|
version = "0.7.0"
|
||||||
source = "git+https://github.com/zhiburt/tabled?rev=cca285b1fc0eac48b8a386c8884092d894d0e7ae#cca285b1fc0eac48b8a386c8884092d894d0e7ae"
|
source = "git+https://github.com/zhiburt/tabled?rev=9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278#9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi-str 0.2.0",
|
"ansi-str 0.2.0",
|
||||||
"papergrid",
|
"papergrid",
|
||||||
@ -4797,7 +4797,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "tabled_derive"
|
name = "tabled_derive"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
source = "git+https://github.com/zhiburt/tabled?rev=cca285b1fc0eac48b8a386c8884092d894d0e7ae#cca285b1fc0eac48b8a386c8884092d894d0e7ae"
|
source = "git+https://github.com/zhiburt/tabled?rev=9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278#9c831d5bc5bcd5a7b7a349ce63f746a64bf1c278"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck 0.4.0",
|
"heck 0.4.0",
|
||||||
"proc-macro-error",
|
"proc-macro-error",
|
||||||
|
@ -102,6 +102,21 @@ impl Command for SubCommand {
|
|||||||
example: "'FF' | into int -r 16",
|
example: "'FF' | into int -r 16",
|
||||||
result: Some(Value::test_int(255)),
|
result: Some(Value::test_int(255)),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert octal string to integer",
|
||||||
|
example: "'0o10132' | into int",
|
||||||
|
result: Some(Value::test_int(4186)),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert 0 padded string to integer",
|
||||||
|
example: "'0010132' | into int",
|
||||||
|
result: Some(Value::test_int(10132)),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert 0 padded string to integer with radix",
|
||||||
|
example: "'0010132' | into int -r 8",
|
||||||
|
result: Some(Value::test_int(4186)),
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -258,11 +273,30 @@ fn convert_int(input: &Value, head: Span, radix: u32) -> Value {
|
|||||||
let i = match input {
|
let i = match input {
|
||||||
Value::Int { val, .. } => val.to_string(),
|
Value::Int { val, .. } => val.to_string(),
|
||||||
Value::String { val, .. } => {
|
Value::String { val, .. } => {
|
||||||
if val.starts_with("0x") || val.starts_with("0b") {
|
if val.starts_with("0x") // hex
|
||||||
|
|| val.starts_with("0b") // binary
|
||||||
|
|| val.starts_with("0o")
|
||||||
|
// octal
|
||||||
|
{
|
||||||
match int_from_string(val, head) {
|
match int_from_string(val, head) {
|
||||||
Ok(x) => return Value::Int { val: x, span: head },
|
Ok(x) => return Value::Int { val: x, span: head },
|
||||||
Err(e) => return Value::Error { error: e },
|
Err(e) => return Value::Error { error: e },
|
||||||
}
|
}
|
||||||
|
} else if val.starts_with("00") {
|
||||||
|
// It's a padded string
|
||||||
|
match i64::from_str_radix(val, radix) {
|
||||||
|
Ok(n) => return Value::Int { val: n, span: head },
|
||||||
|
Err(e) => {
|
||||||
|
return Value::Error {
|
||||||
|
error: ShellError::CantConvert(
|
||||||
|
"string".to_string(),
|
||||||
|
"int".to_string(),
|
||||||
|
head,
|
||||||
|
Some(e.to_string()),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val.to_string()
|
val.to_string()
|
||||||
}
|
}
|
||||||
@ -316,6 +350,20 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
|||||||
};
|
};
|
||||||
Ok(num)
|
Ok(num)
|
||||||
}
|
}
|
||||||
|
o if o.starts_with("0o") => {
|
||||||
|
let num = match i64::from_str_radix(o.trim_start_matches("0o"), 8) {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(_reason) => {
|
||||||
|
return Err(ShellError::CantConvert(
|
||||||
|
"int".to_string(),
|
||||||
|
"string".to_string(),
|
||||||
|
span,
|
||||||
|
Some(r#"octal digits following "0o" should be in 0-7"#.to_string()),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(num)
|
||||||
|
}
|
||||||
_ => match trimmed.parse::<i64>() {
|
_ => match trimmed.parse::<i64>() {
|
||||||
Ok(n) => Ok(n),
|
Ok(n) => Ok(n),
|
||||||
Err(_) => match a_string.parse::<f64>() {
|
Err(_) => match a_string.parse::<f64>() {
|
||||||
|
Loading…
Reference in New Issue
Block a user