History

Clarify value_impl index handling

This clarifies that in the parsing loop, we are dealing with the current index. This also makes it so we have a more consistent pattern for updating the current index. This isn't interesting on its own but is prep for unrolling some of the state

Ed Page committed 3 years ago

Commit b7909481 parent 5fc10961 file · +13 −14Browse files

Unified · Split

gix-config/src/parse/nom/mod.rs+13−14
@@ -257,7 +257,7 @@ fn config_value<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResul · 256 unmodified lines
257257 /// line values as well as values that are continuations.
258258 fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult<&'i [u8], usize> {
259259 let new_err = |kind| winnow::error::ErrMode::from_error_kind(i, kind);
260− let mut value_end = None::<usize>;
260+ let mut value_end = None;
261261 let mut value_start: usize = 0;
262262 let mut newlines = 0;
263263
@@ -266,14 +266,17 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 2 unmodified lines
266266 let mut is_in_quotes = false;
267267 // Used to determine if we return a Value or Value{Not,}Done
268268 let mut partial_value_found = false;
269− let mut last_value_index: usize = 0;
269+ let mut current_index: usize = 0;
270270
271271 let mut bytes = i.iter();
272272 while let Some(mut c) = bytes.next() {
273273 if prev_char_was_backslash {
274274 prev_char_was_backslash = false;
275+ let escape_index = current_index - 1;
276+ let escaped_index = current_index;
275277 let mut consumed = 1;
276278 if *c == b'\r' {
279+ current_index += 1;
277280 c = bytes.next().ok_or_else(|| new_err(ErrorKind::Token))?;
278281 if *c != b'\n' {
279282 return Err(new_err(ErrorKind::Slice));
@@ -284,21 +287,16 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 4 unmodified lines
284287 match c {
285288 b'\n' => {
286289 partial_value_found = true;
287− let backslash = 1;
288290 dispatch(Event::ValueNotDone(Cow::Borrowed(
289− i[value_start..last_value_index - backslash].as_bstr(),
291+ i[value_start..escape_index].as_bstr(),
290292 )));
291− let nl_end = last_value_index + consumed;
292− dispatch(Event::Newline(Cow::Borrowed(i[last_value_index..nl_end].as_bstr())));
293+ let nl_end = escaped_index + consumed;
294+ dispatch(Event::Newline(Cow::Borrowed(i[escaped_index..nl_end].as_bstr())));
293295 value_start = nl_end;
294296 value_end = None;
295297 newlines += 1;
296−
297− last_value_index += consumed;
298− }
299− b'n' | b't' | b'\\' | b'b' | b'"' => {
300− last_value_index += 1;
301298 }
299+ b'n' | b't' | b'\\' | b'b' | b'"' => {}
302300 _ => {
303301 return Err(new_err(ErrorKind::Token));
304302 }
@@ -306,19 +304,19 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 1 unmodified line
306304 } else {
307305 match c {
308306 b'\n' => {
309− value_end = last_value_index.into();
307+ value_end = Some(current_index);
310308 break;
311309 }
312310 b';' | b'#' if !is_in_quotes => {
313− value_end = last_value_index.into();
311+ value_end = Some(current_index);
314312 break;
315313 }
316314 b'\\' => prev_char_was_backslash = true,
317315 b'"' => is_in_quotes = !is_in_quotes,
318316 _ => {}
319317 }
320− last_value_index += 1;
321318 }
319+ current_index += 1;
322320 }
323321 if prev_char_was_backslash {
324322 return Err(new_err(ErrorKind::Token));
@@ -326,6 +324,7 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 1 unmodified line
326324 if is_in_quotes {
327325 return Err(new_err(ErrorKind::Slice));
328326 }
327+ let last_value_index = current_index;
329328
330329 let value_end = match value_end {
331330 None => {