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
Unified · Split
| @@ -257,7 +257,7 @@ fn config_value<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResul · 256 unmodified lines | ||
| 257 | 257 | /// line values as well as values that are continuations. |
| 258 | 258 | fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult<&'i [u8], usize> { |
| 259 | 259 | 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; | |
| 261 | 261 | let mut value_start: usize = 0; |
| 262 | 262 | let mut newlines = 0; |
| 263 | 263 | |
| @@ -266,14 +266,17 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 2 unmodified lines | ||
| 266 | 266 | let mut is_in_quotes = false; |
| 267 | 267 | // Used to determine if we return a Value or Value{Not,}Done |
| 268 | 268 | let mut partial_value_found = false; |
| 269 | − let mut last_value_index: usize = 0; | |
| 269 | + let mut current_index: usize = 0; | |
| 270 | 270 | |
| 271 | 271 | let mut bytes = i.iter(); |
| 272 | 272 | while let Some(mut c) = bytes.next() { |
| 273 | 273 | if prev_char_was_backslash { |
| 274 | 274 | prev_char_was_backslash = false; |
| 275 | + let escape_index = current_index - 1; | |
| 276 | + let escaped_index = current_index; | |
| 275 | 277 | let mut consumed = 1; |
| 276 | 278 | if *c == b'\r' { |
| 279 | + current_index += 1; | |
| 277 | 280 | c = bytes.next().ok_or_else(|| new_err(ErrorKind::Token))?; |
| 278 | 281 | if *c != b'\n' { |
| 279 | 282 | 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 | ||
| 284 | 287 | match c { |
| 285 | 288 | b'\n' => { |
| 286 | 289 | partial_value_found = true; |
| 287 | − let backslash = 1; | |
| 288 | 290 | dispatch(Event::ValueNotDone(Cow::Borrowed( |
| 289 | − i[value_start..last_value_index - backslash].as_bstr(), | |
| 291 | + i[value_start..escape_index].as_bstr(), | |
| 290 | 292 | ))); |
| 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()))); | |
| 293 | 295 | value_start = nl_end; |
| 294 | 296 | value_end = None; |
| 295 | 297 | newlines += 1; |
| 296 | − | |
| 297 | − last_value_index += consumed; | |
| 298 | − } | |
| 299 | − b'n' | b't' | b'\\' | b'b' | b'"' => { | |
| 300 | − last_value_index += 1; | |
| 301 | 298 | } |
| 299 | + b'n' | b't' | b'\\' | b'b' | b'"' => {} | |
| 302 | 300 | _ => { |
| 303 | 301 | return Err(new_err(ErrorKind::Token)); |
| 304 | 302 | } |
| @@ -306,19 +304,19 @@ fn value_impl<'i>(i: &'i [u8], dispatch: &mut impl FnMut(Event<'i>)) -> IResult< · 1 unmodified line | ||
| 306 | 304 | } else { |
| 307 | 305 | match c { |
| 308 | 306 | b'\n' => { |
| 309 | − value_end = last_value_index.into(); | |
| 307 | + value_end = Some(current_index); | |
| 310 | 308 | break; |
| 311 | 309 | } |
| 312 | 310 | b';' | b'#' if !is_in_quotes => { |
| 313 | − value_end = last_value_index.into(); | |
| 311 | + value_end = Some(current_index); | |
| 314 | 312 | break; |
| 315 | 313 | } |
| 316 | 314 | b'\\' => prev_char_was_backslash = true, |
| 317 | 315 | b'"' => is_in_quotes = !is_in_quotes, |
| 318 | 316 | _ => {} |
| 319 | 317 | } |
| 320 | − last_value_index += 1; | |
| 321 | 318 | } |
| 319 | + current_index += 1; | |
| 322 | 320 | } |
| 323 | 321 | if prev_char_was_backslash { |
| 324 | 322 | 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 | ||
| 326 | 324 | if is_in_quotes { |
| 327 | 325 | return Err(new_err(ErrorKind::Slice)); |
| 328 | 326 | } |
| 327 | + let last_value_index = current_index; | |
| 329 | 328 | |
| 330 | 329 | let value_end = match value_end { |
| 331 | 330 | None => { |