Skip to content

Commit

Permalink
Fix incorrect col calculate for pair.line_col method.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 12, 2023
1 parent bcbe0f0 commit 05ff0dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion grammars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "_README.md"
rust-version = "1.56"

[dependencies]
pest = { path = "../pest", version = "2.5.3" }
pest = { path = "../pest", version = "2.5.3", features = ["fast-line-col"] }
pest_derive = { path = "../derive", version = "2.5.3" }

[dev-dependencies]
Expand Down
9 changes: 1 addition & 8 deletions pest/src/iterators/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,7 @@ impl<'i, R: RuleType> Pairs<'i, R> {
let (prev_line, prev_col) = (self.cursor.line, self.cursor.col);

let part = &input[self.cursor.end..end];
let (l, c) = position::line_col(part, part.len());

// Because the `original_line_col` returns (line, col) is start from 1
let l = l - 1;
let mut c = c - 1;
if c < 1 {
c = 1
}
let (l, c) = position::line_col(part, part.len(), (0, 0));

self.cursor.line += l;
// Has new line
Expand Down
23 changes: 14 additions & 9 deletions pest/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'i> Position<'i> {
panic!("position out of bounds");
}

line_col(self.input, self.pos)
line_col(self.input, self.pos, (1, 1))
}

/// Returns the entire line of the input that contains this `Position`.
Expand Down Expand Up @@ -452,25 +452,30 @@ impl<'i> Hash for Position<'i> {
}
}

pub(crate) fn line_col(input: &str, pos: usize) -> (usize, usize) {
/// Returns the line and column of the given `pos` in `input`.
pub(crate) fn line_col(input: &str, pos: usize, start: (usize, usize)) -> (usize, usize) {
#[cfg(feature = "fast-line-col")]
{
fast_line_col(input, pos)
fast_line_col(input, pos, start)
}
#[cfg(not(feature = "fast-line-col"))]
{
original_line_col(input, pos)
original_line_col(input, pos, start)
}
}

#[inline]
#[cfg(not(feature = "fast-line-col"))]
fn original_line_col(input: &str, mut pos: usize) -> (usize, usize) {
pub(crate) fn original_line_col(
input: &str,
mut pos: usize,
start: (usize, usize),
) -> (usize, usize) {
// Position's pos is always a UTF-8 border.
let slice = &input[..pos];
let mut chars = slice.chars().peekable();

let mut line_col = (1, 1);
let mut line_col = start;

while pos != 0 {
match chars.next() {
Expand Down Expand Up @@ -507,16 +512,16 @@ fn original_line_col(input: &str, mut pos: usize) -> (usize, usize) {

#[inline]
#[cfg(feature = "fast-line-col")]
fn fast_line_col(input: &str, pos: usize) -> (usize, usize) {
fn fast_line_col(input: &str, pos: usize, start: (usize, usize)) -> (usize, usize) {
// Position's pos is always a UTF-8 border.
let slice = &input[..pos];

let prec_ln = memchr::memrchr(b'\n', slice.as_bytes());
if let Some(prec_nl_pos) = prec_ln {
let lines = bytecount::count(slice[..=prec_nl_pos].as_bytes(), b'\n') + 1;
let lines = bytecount::count(slice[..=prec_nl_pos].as_bytes(), b'\n') + start.0;
(lines, slice[prec_nl_pos..].chars().count())
} else {
(1, slice.chars().count() + 1)
(start.0, slice.chars().count() + start.1)
}
}

Expand Down

0 comments on commit 05ff0dc

Please sign in to comment.