Skip to content

Commit

Permalink
Merge line_col method to use position::line_col.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Dec 29, 2022
1 parent 6b8e95a commit 370b938
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 52 deletions.
49 changes: 8 additions & 41 deletions pest/src/iterators/pairs.rs
Expand Up @@ -23,7 +23,7 @@ use super::flat_pairs::{self, FlatPairs};
use super::pair::{self, Pair};
use super::queueable_token::QueueableToken;
use super::tokens::{self, Tokens};
use crate::RuleType;
use crate::{position, RuleType};

#[derive(Clone)]
pub struct Cursor {
Expand All @@ -49,56 +49,23 @@ pub trait CursorPairs {

/// Move the (line, col) with string part
fn move_cursor(&mut self, part: &str) -> (usize, usize) {
let (l, c, has_new_line) = self.line_col(part);
let (l, c) = position::line_col(part, part.len());

// because original_line_col line, col start from 1
let l = l - 1;
let c = c - 1;

let (prev_line, prev_col) = self.cursor().get();

self.cursor_mut().line += l;
if has_new_line {
// Has new line
if l > 0 {
self.cursor_mut().col = c;
} else {
self.cursor_mut().col += c;
}
(prev_line, prev_col)
}

/// Calculate line and col number of a string part
/// Fork from Pest for just count the part.
///
/// https://github.com/pest-parser/pest/blob/85b18aae23cc7b266c0b5252f9f74b7ab0000795/pest/src/position.rs#L135
fn line_col(&self, part: &str) -> (usize, usize, bool) {
let mut chars = part.chars().peekable();

let mut line_col = (0, 0);
let mut has_new_line = false;

loop {
match chars.next() {
Some('\r') => {
if let Some(&'\n') = chars.peek() {
chars.next();

line_col = (line_col.0 + 1, 1);
has_new_line = true;
} else {
line_col = (line_col.0, line_col.1 + 1);
}
}
Some('\n') => {
line_col = (line_col.0 + 1, 1);
has_new_line = true;
}
Some(_c) => {
line_col = (line_col.0, line_col.1 + 1);
}
None => {
break;
}
}
}

(line_col.0, line_col.1, has_new_line)
}
}

/// An iterator over [`Pair`]s. It is created by [`pest::state`] and [`Pair::into_inner`].
Expand Down
26 changes: 15 additions & 11 deletions pest/src/position.rs
Expand Up @@ -116,9 +116,8 @@ impl<'i> Position<'i> {

/// Returns the line and column number of this `Position`.
///
/// This is an O(n) operation, where n is the number of lines in the input.
/// You better use `pair.line_col()` instead.
///
/// This is an O(n) operation, where n is the number of chars in the input.
/// You better use [`pair.line_col()`](struct.Pair.html#method.line_col) instead.
///
/// # Examples
///
Expand All @@ -139,14 +138,8 @@ impl<'i> Position<'i> {
if self.pos > self.input.len() {
panic!("position out of bounds");
}
#[cfg(feature = "fast-line-col")]
{
fast_line_col(self.input, self.pos)
}
#[cfg(not(feature = "fast-line-col"))]
{
original_line_col(self.input, self.pos)
}

line_col(self.input, self.pos)
}

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

pub(crate) fn line_col(input: &str, pos: usize) -> (usize, usize) {
#[cfg(feature = "fast-line-col")]
{
fast_line_col(input, pos)
}
#[cfg(not(feature = "fast-line-col"))]
{
original_line_col(input, pos)
}
}

#[inline]
#[cfg(not(feature = "fast-line-col"))]
fn original_line_col(input: &str, mut pos: usize) -> (usize, usize) {
Expand Down

0 comments on commit 370b938

Please sign in to comment.