Skip to content

Commit

Permalink
fix(uint): make from_str parse 0x-prefixed strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Dec 27, 2020
1 parent 203634b commit 10e97d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,11 @@ macro_rules! construct_uint {
type Err = $crate::FromHexError;

fn from_str(value: &str) -> $crate::core_::result::Result<$name, Self::Err> {
let value = if value.starts_with("0x") {
&value[2..]
} else {
value
};
const BYTES_LEN: usize = $n_words * 8;
const MAX_ENCODED_LEN: usize = BYTES_LEN * 2;

Expand Down
2 changes: 2 additions & 0 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ fn uint256_from() {
);

// test initializtion from string
let sa2 = U256::from_str("0x0a").unwrap();
let sa = U256::from_str("0a").unwrap();
assert_eq!(sa2, sa);
assert_eq!(e, sa);
assert_eq!(U256([0, 0, 0, 0]), U256::from_str("").unwrap());
assert_eq!(U256([0x1, 0, 0, 0]), U256::from_str("1").unwrap());
Expand Down

0 comments on commit 10e97d2

Please sign in to comment.