-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshort.rs
91 lines (80 loc) · 2.67 KB
/
short.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#![allow(dead_code)]
use crate::safe_unchecked::SliceGetter;
use crate::AtoiSimdError;
macro_rules! overflow {
($curr:ident * 10 + $more:ident, $max:expr) => {
$curr >= $max / 10 && ($curr > $max / 10 || $more > $max % 10)
};
}
macro_rules! overflow_neg {
($curr:ident * 10 - $more:ident, $max:expr) => {
$curr <= $max / 10 && ($curr < $max / 10 || $more > -($max % 10))
};
}
#[inline]
pub(crate) fn parse_short_pos<const MAX: u64>(s: &[u8]) -> Result<(u64, usize), AtoiSimdError> {
let mut i = 0;
if s.len() == i {
return Err(AtoiSimdError::Empty);
}
match s.get_safe_unchecked(i) {
c @ b'0'..=b'9' => {
let mut res = (c & 0xF) as u64;
i += 1;
while s.len() > i {
match s.get_safe_unchecked(i) {
c @ b'0'..=b'9' => {
let digit = (c & 0xF) as u64;
if MAX <= u32::MAX as u64 && overflow!(res * 10 + digit, MAX) {
return Err(AtoiSimdError::Overflow(s));
}
res = res * 10 + digit;
i += 1;
}
_ => return Ok((res, i)),
}
}
Ok((res, i))
}
_ => Err(AtoiSimdError::Empty),
}
}
#[inline]
pub(crate) fn parse_short_neg<const MIN: i64>(s: &[u8]) -> Result<(i64, usize), AtoiSimdError> {
debug_assert!(MIN < 0);
let mut i = 0;
if s.len() == i {
return Err(AtoiSimdError::Empty);
}
match s.get_safe_unchecked(i) {
c @ b'0'..=b'9' => {
let mut res = -((c & 0xF) as i64);
i += 1;
while s.len() > i {
match s.get_safe_unchecked(i) {
c @ b'0'..=b'9' => {
let digit = (c & 0xF) as i64;
if MIN >= i32::MIN as i64 && overflow_neg!(res * 10 - digit, MIN) {
// can't overflow, because MIN is bigger than i64::MIN
return Err(AtoiSimdError::Overflow(s));
}
res = res * 10 - digit;
i += 1;
}
_ => return Ok((res, i)),
}
}
Ok((res, i))
}
_ => Err(AtoiSimdError::Empty),
}
}
/* #[inline(always)]
pub(crate) fn parse_short_checked_neg<const MIN: i64>(s: &[u8]) -> Result<i64, AtoiSimdError> {
debug_assert!(MIN < 0);
let (res, len) = parse_short_neg::<MIN>(s)?;
if len != s.len() {
return Err(AtoiSimdError::Invalid64(-res as u64, len, s));
}
Ok(res)
} */