generated from PlexSheep/rs-base
Compare commits
No commits in common. "68d9cdaf6928ef4c27f38ef5adf286a1fe629284" and "47894a3f26d0c2fb1c850dde425a815950d6810f" have entirely different histories.
68d9cdaf69
...
47894a3f26
2 changed files with 7 additions and 48 deletions
|
@ -2,7 +2,7 @@
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use clap::{ArgGroup, Parser};
|
use clap::{ArgGroup, Parser};
|
||||||
use libpt::bintols::split;
|
use libpt::bintols::split;
|
||||||
use num::traits::Pow;
|
use num;
|
||||||
|
|
||||||
pub type NumberType = u128;
|
pub type NumberType = u128;
|
||||||
|
|
||||||
|
@ -232,7 +232,7 @@ impl Format {
|
||||||
///
|
///
|
||||||
/// #[derive(Parser)]
|
/// #[derive(Parser)]
|
||||||
/// struct Args {
|
/// struct Args {
|
||||||
/// #[clap(short, long, value_parser=numf_parser::<u128>)]
|
/// #[clap(short, long, value_parser=numf_parser)]
|
||||||
/// address: u128,
|
/// address: u128,
|
||||||
/// }
|
/// }
|
||||||
/// let args = Args::parse_from(&["", "-a", "0x10"]);
|
/// let args = Args::parse_from(&["", "-a", "0x10"]);
|
||||||
|
@ -244,7 +244,6 @@ where
|
||||||
<T as std::str::FromStr>::Err: std::fmt::Display,
|
<T as std::str::FromStr>::Err: std::fmt::Display,
|
||||||
T: num::Num,
|
T: num::Num,
|
||||||
<T as num::Num>::FromStrRadixErr: std::fmt::Display,
|
<T as num::Num>::FromStrRadixErr: std::fmt::Display,
|
||||||
<T as std::str::FromStr>::Err: std::fmt::Debug,
|
|
||||||
{
|
{
|
||||||
if s.starts_with(&Format::Dec.prefix()) || s.parse::<T>().is_ok() {
|
if s.starts_with(&Format::Dec.prefix()) || s.parse::<T>().is_ok() {
|
||||||
let s = match s.strip_prefix(&Format::Dec.prefix()) {
|
let s = match s.strip_prefix(&Format::Dec.prefix()) {
|
||||||
|
@ -295,51 +294,11 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if s.starts_with(&Format::Base64.prefix()) {
|
} else if s.starts_with(&Format::Base64.prefix()) {
|
||||||
let s = match s.strip_prefix(&Format::Base64.prefix()) {
|
let e = "parsing of base64 is not yet implemented".to_string();
|
||||||
Some(sr) => sr,
|
Err(anyhow!(e))
|
||||||
None => s,
|
|
||||||
};
|
|
||||||
match fast32::base64::RFC4648.decode_str(s) {
|
|
||||||
Ok(r) => {
|
|
||||||
if r.len() > 16 {
|
|
||||||
panic!("boom");
|
|
||||||
}
|
|
||||||
let mut ri: u128 = 0;
|
|
||||||
for (i, e) in r.iter().rev().enumerate() {
|
|
||||||
ri += (*e as u128) * 256.pow(i as u32) as u128;
|
|
||||||
}
|
|
||||||
dbg!(ri);
|
|
||||||
dbg!(format!("{ri:#x}"));
|
|
||||||
Ok(ri.to_string().parse().unwrap())
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let e = format!("{e}");
|
|
||||||
Err(anyhow!(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if s.starts_with(&Format::Base32.prefix()) {
|
} else if s.starts_with(&Format::Base32.prefix()) {
|
||||||
let s = match s.strip_prefix(&Format::Base32.prefix()) {
|
let e = "parsing of base32 is not yet implemented".to_string();
|
||||||
Some(sr) => sr,
|
Err(anyhow!(e))
|
||||||
None => s,
|
|
||||||
};
|
|
||||||
match fast32::base32::RFC4648.decode_str(s) {
|
|
||||||
Ok(r) => {
|
|
||||||
if r.len() > 16 {
|
|
||||||
panic!("boom");
|
|
||||||
}
|
|
||||||
let mut ri: u128 = 0;
|
|
||||||
for (i, e) in r.iter().rev().enumerate() {
|
|
||||||
ri += (*e as u128) * 256.pow(i as u32) as u128;
|
|
||||||
}
|
|
||||||
dbg!(ri);
|
|
||||||
dbg!(format!("{ri:#x}"));
|
|
||||||
Ok(ri.to_string().parse().unwrap())
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let e = format!("{e}");
|
|
||||||
Err(anyhow!(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let e = "could not determine the format of the value".to_string();
|
let e = "could not determine the format of the value".to_string();
|
||||||
Err(anyhow!(e))
|
Err(anyhow!(e))
|
||||||
|
|
|
@ -228,7 +228,7 @@ fn parser_b64() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parser_b32() {
|
fn parser_b32() {
|
||||||
assert_eq!(numf_parser::<u32>("032sIFAUEQQ=").unwrap(), 0x41414242);
|
assert_eq!(numf_parser::<u32>("IFAUEQQ=").unwrap(), 0x41414242);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Reference in a new issue