Skip to content

Commit

Permalink
Refactor input traits (#1610)
Browse files Browse the repository at this point in the history
* add defaut implementations for InputTakeAtPosition methods

* use const generics for fixed size array implementations

* BREAKING CHANGE: simplify InputIter: remove iter_indices
it was only used in two combinators for which an alternative
implementation can be used
  • Loading branch information
Geal committed Jan 5, 2023
1 parent 934b80e commit dc5601d
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 211 deletions.
31 changes: 20 additions & 11 deletions src/character/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,10 @@ where
T: InputIter + InputLength + Slice<RangeFrom<usize>>,
<T as InputIter>::Item: AsChar,
{
let mut it = input.iter_indices();
let mut it = input.iter_elements();
match it.next() {
None => Err(Err::Error(E::from_error_kind(input, ErrorKind::Eof))),
Some((_, c)) => match it.next() {
None => Ok((input.slice(input.input_len()..), c.as_char())),
Some((idx, _)) => Ok((input.slice(idx..), c.as_char())),
},
Some(c) => Ok((input.slice(c.len()..), c.as_char())),
}
}

Expand Down Expand Up @@ -744,7 +741,8 @@ macro_rules! ints {

let mut value: $t = 0;
if sign {
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -755,12 +753,16 @@ macro_rules! ints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
} else {
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -771,7 +773,10 @@ macro_rules! ints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_sub(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
Expand Down Expand Up @@ -804,7 +809,8 @@ macro_rules! uints {
}

let mut value: $t = 0;
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -815,7 +821,10 @@ macro_rules! uints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(i, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/character/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,10 @@ where
T: InputIter + InputLength + Slice<RangeFrom<usize>>,
<T as InputIter>::Item: AsChar,
{
let mut it = input.iter_indices();
let mut it = input.iter_elements();
match it.next() {
None => Err(Err::Incomplete(Needed::new(1))),
Some((_, c)) => match it.next() {
None => Ok((input.slice(input.input_len()..), c.as_char())),
Some((idx, _)) => Ok((input.slice(idx..), c.as_char())),
},
Some(c) => Ok((input.slice(c.len()..), c.as_char())),
}
}

Expand Down Expand Up @@ -650,7 +647,8 @@ macro_rules! ints {

let mut value: $t = 0;
if sign {
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -661,12 +659,16 @@ macro_rules! ints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
} else {
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -677,7 +679,10 @@ macro_rules! ints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_sub(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(input, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
Expand Down Expand Up @@ -710,7 +715,8 @@ macro_rules! uints {
}

let mut value: $t = 0;
for (pos, c) in i.iter_indices() {
let mut pos = 0;
for c in i.iter_elements() {
match c.as_char().to_digit(10) {
None => {
if pos == 0 {
Expand All @@ -721,7 +727,10 @@ macro_rules! uints {
},
Some(d) => match value.checked_mul(10).and_then(|v| v.checked_add(d as $t)) {
None => return Err(Err::Error(E::from_error_kind(i, ErrorKind::Digit))),
Some(v) => value = v,
Some(v) => {
pos += c.len();
value = v;
},
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/number/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ where
Err(Err::Error(make_error(input, ErrorKind::Eof)))
} else {
let mut res = Uint::default();
for (index, byte) in input.iter_indices().take(bound) {
for (index, byte) in input.iter_elements().take(bound).enumerate() {
res = res + (Uint::from(byte) << (8 * index as u8));
}

Expand Down Expand Up @@ -1506,7 +1506,7 @@ where
T: Clone + Offset + ParseTo<f32> + Compare<&'static str>,
T: InputIter + InputLength + InputTake,
<T as InputIter>::Item: AsChar + Copy,
<T as InputIter>::IterElem: Clone,
<T as InputIter>::Iter: Clone,
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar,
T: AsBytes,
Expand Down Expand Up @@ -1559,7 +1559,7 @@ where
T: Clone + Offset + ParseTo<f64> + Compare<&'static str>,
T: InputIter + InputLength + InputTake,
<T as InputIter>::Item: AsChar + Copy,
<T as InputIter>::IterElem: Clone,
<T as InputIter>::Iter: Clone,
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar,
T: AsBytes,
Expand Down
6 changes: 3 additions & 3 deletions src/number/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ where
Err(Err::Incomplete(Needed::new(bound - input.input_len())))
} else {
let mut res = Uint::default();
for (index, byte) in input.iter_indices().take(bound) {
for (index, byte) in input.iter_elements().take(bound).enumerate() {
res = res + (Uint::from(byte) << (8 * index as u8));
}

Expand Down Expand Up @@ -1476,7 +1476,7 @@ where
T: Clone + Offset,
T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f32> + Compare<&'static str>,
<T as InputIter>::Item: AsChar,
<T as InputIter>::IterElem: Clone,
<T as InputIter>::Iter: Clone,
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar,
T: AsBytes,
Expand Down Expand Up @@ -1530,7 +1530,7 @@ where
T: Clone + Offset,
T: InputIter + InputLength + InputTake + crate::traits::ParseTo<f64> + Compare<&'static str>,
<T as InputIter>::Item: AsChar,
<T as InputIter>::IterElem: Clone,
<T as InputIter>::Iter: Clone,
T: InputTakeAtPosition,
<T as InputTakeAtPosition>::Item: AsChar,
T: AsBytes,
Expand Down

0 comments on commit dc5601d

Please sign in to comment.