Skip to content

Commit ff26eaa

Browse files
committed
base: fix stdin parsing
1 parent 0116a4a commit ff26eaa

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/baseapp.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use clap::{arg, Command};
44
use num_bigint::BigUint;
55
use num_traits::Num;
66

7+
use crate::applet::SliceExt;
8+
79
pub struct BaseIntApplet {
810
source_radix: Option<u32>,
911
target_radix: u32,
@@ -50,13 +52,16 @@ impl Applet for BaseIntApplet {
5052
}
5153

5254
fn process(&self, val: Vec<u8>) -> Result<Vec<u8>> {
55+
// Remove whitespace to make conversions work with stdin input
56+
let val = val.trim();
57+
5358
let (srcrad, int) = if let Some(src) = self.source_radix {
5459
(
5560
src,
5661
BigUint::parse_bytes(&val, src).context("Could not convert input")?,
5762
)
5863
} else {
59-
let int_str = String::from_utf8(val).context("Could not convert value to string")?;
64+
let int_str = String::from_utf8_lossy(val);
6065

6166
// Base was not specified, check standard prefixes
6267
if int_str.len() > 2 && &int_str[0..2] == "0x" {
@@ -122,6 +127,17 @@ mod tests {
122127
.success();
123128
}
124129

130+
#[test]
131+
fn test_base_cli_stdin() {
132+
assert_cmd::Command::cargo_bin("rsbkb")
133+
.expect("Could not run binary")
134+
.args(&["base"])
135+
.write_stdin("0xA\n")
136+
.assert()
137+
.stdout("10")
138+
.success();
139+
}
140+
125141
#[test]
126142
fn test_base_cli_arg_to() {
127143
assert_cmd::Command::cargo_bin("rsbkb")

0 commit comments

Comments
 (0)