Skip to content

Commit

Permalink
Fix parse_skipped for parsing zeroes (#10)
Browse files Browse the repository at this point in the history
Also make AtoiSimdError comparable
  • Loading branch information
orlp authored Nov 28, 2023
1 parent 3d5aa8d commit ecf1955
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ::core::fmt;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)]
pub enum AtoiSimdError<'a> {
Empty,
Size(usize, &'a [u8]),
Expand Down
4 changes: 2 additions & 2 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait Parser<T: ParserPos<T>>: Sized {
if *s.first().ok_or(AtoiSimdError::Empty)? == b'+' {
i = 1;
}
while s.len() > i {
while i + 1 < s.len() {
if *s.get_safe_unchecked(i) != b'0' {
break;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ fn atoi_simd_parse_skipped_signed<T: ParserPos<T> + ParserNeg<T>>(
}
_ => {}
};
while s.len() > i {
while i + 1 < s.len() {
if *s.get_safe_unchecked(i) != b'0' {
break;
}
Expand Down
78 changes: 78 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,4 +1102,82 @@ fn test_parse_skipped() {

let tmp = parse_skipped::<i128>(b"-000000000000000000000000012345678901234567890").unwrap();
assert_eq!(tmp, -12345678901234567890_i128);

// Zeroes.
assert_eq!(parse_skipped::<i8>(b"0"), Ok(0));
assert_eq!(parse_skipped::<i8>(b"-0"), Ok(0));
assert_eq!(parse_skipped::<i8>(b"-0000000000000000000000000000"), Ok(0));
assert_eq!(parse_skipped::<i8>(b"+0"), Ok(0));
assert_eq!(parse_skipped::<i8>(b"+0000000000000000000000000000"), Ok(0));
assert_eq!(parse_skipped::<u8>(b"0"), Ok(0));
assert_eq!(parse_skipped::<u8>(b"+0"), Ok(0));
assert_eq!(parse_skipped::<u8>(b"+0000000000000000000000000000"), Ok(0));
assert_eq!(parse_skipped::<i16>(b"0"), Ok(0));
assert_eq!(parse_skipped::<i16>(b"-0"), Ok(0));
assert_eq!(
parse_skipped::<i16>(b"-0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i16>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<i16>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<u16>(b"0"), Ok(0));
assert_eq!(parse_skipped::<u16>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<u16>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i32>(b"0"), Ok(0));
assert_eq!(parse_skipped::<i32>(b"-0"), Ok(0));
assert_eq!(
parse_skipped::<i32>(b"-0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i32>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<i32>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<u32>(b"0"), Ok(0));
assert_eq!(parse_skipped::<u32>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<u32>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i64>(b"0"), Ok(0));
assert_eq!(parse_skipped::<i64>(b"-0"), Ok(0));
assert_eq!(
parse_skipped::<i64>(b"-0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i64>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<i64>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<u64>(b"0"), Ok(0));
assert_eq!(parse_skipped::<u64>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<u64>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i128>(b"0"), Ok(0));
assert_eq!(parse_skipped::<i128>(b"-0"), Ok(0));
assert_eq!(
parse_skipped::<i128>(b"-0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<i128>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<i128>(b"+0000000000000000000000000000"),
Ok(0)
);
assert_eq!(parse_skipped::<u128>(b"0"), Ok(0));
assert_eq!(parse_skipped::<u128>(b"+0"), Ok(0));
assert_eq!(
parse_skipped::<u128>(b"+0000000000000000000000000000"),
Ok(0)
);
}

0 comments on commit ecf1955

Please sign in to comment.