Skip to content

Commit d4da01e

Browse files
authored
Remove the dependency on parse_int in favour of from_str_radix (#756)
* Remove the dependency on `parse_int` in favour of `from_str_radix` * Add a unit test for the `parse_u32` function
1 parent 6f3c96d commit d4da01e

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

espflash/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ indicatif = { version = "0.17.9", optional = true }
4343
log = "0.4.22"
4444
md-5 = "0.10.6"
4545
miette = "7.4.0"
46-
parse_int = { version = "0.6.0", optional = true }
4746
regex = { version = "1.11.1", optional = true }
4847
serde = { version = "1.0.217", features = ["derive"] }
4948
serialport = { version = "4.7.0", default-features = false, optional = true }
@@ -73,7 +72,6 @@ cli = [
7372
"dep:directories",
7473
"dep:env_logger",
7574
"dep:indicatif",
76-
"dep:parse_int",
7775
"dep:toml",
7876
"dep:update-informer",
7977
"miette/fancy",

espflash/src/cli/mod.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,15 @@ pub struct ChecksumMd5Args {
290290
connect_args: ConnectArgs,
291291
}
292292

293-
/// Parses an integer, in base-10 or hexadecimal format, into a [u32].
293+
/// Parses an integer, in base-10 or hexadecimal format, into a [u32]
294294
pub fn parse_u32(input: &str) -> Result<u32, ParseIntError> {
295-
parse_int::parse(input)
295+
let (s, radix) = if input.len() > 2 && matches!(&input[0..2], "0x" | "0X") {
296+
(&input[2..], 16)
297+
} else {
298+
(input, 10)
299+
};
300+
301+
u32::from_str_radix(s, radix)
296302
}
297303

298304
/// Select a serial port and establish a connection with a target device
@@ -861,7 +867,7 @@ pub fn make_flash_data(
861867
mod test {
862868
use clap::Parser;
863869

864-
use crate::cli::FlashArgs;
870+
use super::*;
865871

866872
#[derive(Parser)]
867873
struct TestParser {
@@ -876,4 +882,21 @@ mod test {
876882
let parser = TestParser::parse_from(iter);
877883
assert_eq!(parser.args.image.partition_table_offset, Some(0x8000));
878884
}
885+
886+
#[test]
887+
fn test_parse_u32() {
888+
// Hex
889+
assert_eq!(parse_u32("0x1"), Ok(0x1));
890+
assert_eq!(parse_u32("0X1234"), Ok(0x1234));
891+
assert_eq!(parse_u32("0xaBcD"), Ok(0xabcd));
892+
// Decimal
893+
assert_eq!(parse_u32("1234"), Ok(1234));
894+
assert_eq!(parse_u32("0"), Ok(0));
895+
// Errors
896+
assert!(parse_u32("").is_err());
897+
assert!(parse_u32("0x").is_err());
898+
assert!(parse_u32("0xg").is_err());
899+
assert!(parse_u32("-123").is_err());
900+
assert!(parse_u32("12.34").is_err());
901+
}
879902
}

espflash/src/cli/monitor/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait InputParser {
1616
}
1717

1818
// Pattern to much a function address in serial output.
19-
static RE_FN_ADDR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"0x[[:xdigit:]]{8}").unwrap());
19+
static RE_FN_ADDR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"0[xX][[:xdigit:]]{8}").unwrap());
2020

2121
fn resolve_addresses(
2222
symbols: &Symbols<'_>,
@@ -30,7 +30,7 @@ fn resolve_addresses(
3030
// Since our regular expression already confirms that this is a correctly
3131
// formatted hex literal, we can (fairly) safely assume that it will parse
3232
// successfully into an integer.
33-
let addr = parse_int::parse::<u64>(matched).unwrap();
33+
let addr = u64::from_str_radix(&matched[2..], 16).unwrap();
3434

3535
let name = symbols.get_name(addr);
3636
let location = symbols.get_location(addr);

0 commit comments

Comments
 (0)