Skip to content

Commit

Permalink
Merge pull request #476 from epage/dec
Browse files Browse the repository at this point in the history
fix(ascii): Accept 0 with dec_uint/dec_int
  • Loading branch information
epage authored Feb 14, 2024
2 parents 49d175e + c9f000a commit 7c88f25
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ where
O: Uint,
{
trace("dec_uint", move |input: &mut I| {
(one_of('1'..='9'), digit0)
alt(((one_of('1'..='9'), digit0).void(), one_of('0').void()))
.recognize()
.verify_map(|s: <I as Stream>::Slice| {
let s = s.as_bstr();
Expand Down Expand Up @@ -994,7 +994,7 @@ where
'-' => empty.value(false),
_ => fail,
});
(sign, one_of('1'..='9'), digit0)
alt(((sign, one_of('1'..='9'), digit0).void(), one_of('0').void()))
.recognize()
.verify_map(|s: <I as Stream>::Slice| {
let s = s.as_bstr();
Expand Down
59 changes: 59 additions & 0 deletions src/ascii/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,65 @@ mod complete {
);
}

#[test]
fn dec_uint_tests() {
fn dec_u32(input: &[u8]) -> IResult<&[u8], u32> {
dec_uint.parse_peek(input)
}

assert_parse!(
dec_u32(&b";"[..]),
Err(ErrMode::Backtrack(error_position!(
&&b";"[..],
ErrorKind::Verify
)))
);
assert_parse!(dec_u32(&b"0;"[..]), Ok((&b";"[..], 0)));
assert_parse!(dec_u32(&b"1;"[..]), Ok((&b";"[..], 1)));
assert_parse!(dec_u32(&b"32;"[..]), Ok((&b";"[..], 32)));
assert_parse!(
dec_u32(&b"1000000000000000000000;"[..]), // overflow
Err(ErrMode::Backtrack(error_position!(
&&b"1000000000000000000000;"[..],
ErrorKind::Verify
)))
);
}

#[test]
fn dec_int_tests() {
fn dec_i32(input: &[u8]) -> IResult<&[u8], i32> {
dec_int.parse_peek(input)
}

assert_parse!(
dec_i32(&b";"[..]),
Err(ErrMode::Backtrack(error_position!(
&&b";"[..],
ErrorKind::Verify
)))
);
assert_parse!(dec_i32(&b"0;"[..]), Ok((&b";"[..], 0)));
assert_parse!(dec_i32(&b"1;"[..]), Ok((&b";"[..], 1)));
assert_parse!(dec_i32(&b"32;"[..]), Ok((&b";"[..], 32)));
assert_parse!(
dec_i32(&b"-0;"[..]),
Err(ErrMode::Backtrack(error_position!(
&&b"-0;"[..],
ErrorKind::Verify
)))
);
assert_parse!(dec_i32(&b"-1;"[..]), Ok((&b";"[..], -1)));
assert_parse!(dec_i32(&b"-32;"[..]), Ok((&b";"[..], -32)));
assert_parse!(
dec_i32(&b"1000000000000000000000;"[..]), // overflow
Err(ErrMode::Backtrack(error_position!(
&&b"1000000000000000000000;"[..],
ErrorKind::Verify
)))
);
}

#[test]
fn hex_uint_tests() {
fn hex_u32(input: &[u8]) -> IResult<&[u8], u32> {
Expand Down

0 comments on commit 7c88f25

Please sign in to comment.