Skip to content

Commit

Permalink
CRLF count modification for location data
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd committed Feb 5, 2025
1 parent 9ce749b commit 4ecd122
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/lazy/text/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,16 @@ impl<'top> TextBuffer<'top> {

/// Updates the location metadata based on the matched whitespace bytes in the consumed buffer
fn update_location_metadata(&mut self, result: TextBuffer<'top>) {
let newline_match_length = result.data.iter().filter(|b| NEWLINE_BYTES.contains(b)).count();
// If the bytes contain '\r\n' in this order then this must be coming from windows line ending pattern and hence should be counted as 1.
let crlf_count = result.data.windows(2).filter(|window| window == b"\r\n").count();

// Subtract the crlf_count from total count of all newline characters to get the correct number of newline match count.
let newline_match_count = result.data.iter().filter(|b| NEWLINE_BYTES.contains(b)).count() - crlf_count;

// Gets index for the last occurrence of the newline byte and subtracts from the result length to get non newline bytes length
let last_index_newline_byte = result.data.iter().rposition(|b| NEWLINE_BYTES.contains(b)).unwrap_or(0);
let non_newline_match_length = result.data.len() - last_index_newline_byte - 1;
self.row += newline_match_length;
self.row += newline_match_count;

// Stores this newline offset as previous newline offset for calculating column position since this has already been matched/parsed
self.prev_newline_offset = self.offset - non_newline_match_length;
Expand Down Expand Up @@ -2892,7 +2896,9 @@ mod tests {
}

#[rstest]
#[case::newlines("\r\n\n", (4,1))]
#[case::newlines("\n\r", (3,1))]
#[case::crlf("\r\n\r\n", (3,1))]
#[case::mixed("\r\n\n\r\n", (4,1))]
#[case::tabs("\n\t\t\t", (2,4))]
#[case::mix_tabs_and_newlines("\n\t\n", (3,1))]
fn expect_whitespace(#[case] input: &str, #[case] expected_location: (usize, usize)) {
Expand Down
4 changes: 3 additions & 1 deletion src/lazy/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ mod tests {
}

#[rstest]
#[case::newlines("{foo: 1, bar: 2}\r\n\n\"hello\"", (4,1))]
#[case::newlines("{foo: 1, bar: 2}\r\n\n\"hello\"", (3,1))]
#[case::crlfs("{foo: 1, bar: 2}\r\n\r\n\"hello\"", (3,1))]
#[case::mixed("{foo: 1, bar: 2}\r\n\n\r\"hello\"", (4,1))]
#[case::tabs("{foo: 1, bar: 2}\n\t\t\t\"hello\"", (2,4))]
#[case::mix_tabs_and_newlines("{foo: 1, bar: 2}\n\t\n\"hello\"", (3,1))]
fn location_test_for_second_tlv(
Expand Down

0 comments on commit 4ecd122

Please sign in to comment.