Skip to content

Commit f49cee4

Browse files
committed
Fix incorrect col calculate for pair.line_col method.
1 parent bcbe0f0 commit f49cee4

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

pest/src/iterators/pairs.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,7 @@ impl<'i, R: RuleType> Pairs<'i, R> {
249249
let (prev_line, prev_col) = (self.cursor.line, self.cursor.col);
250250

251251
let part = &input[self.cursor.end..end];
252-
let (l, c) = position::line_col(part, part.len());
253-
254-
// Because the `original_line_col` returns (line, col) is start from 1
255-
let l = l - 1;
256-
let mut c = c - 1;
257-
if c < 1 {
258-
c = 1
259-
}
252+
let (l, c) = position::line_col(part, part.len(), (0, 0));
260253

261254
self.cursor.line += l;
262255
// Has new line

pest/src/position.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'i> Position<'i> {
139139
panic!("position out of bounds");
140140
}
141141

142-
line_col(self.input, self.pos)
142+
line_col(self.input, self.pos, (1, 1))
143143
}
144144

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

455-
pub(crate) fn line_col(input: &str, pos: usize) -> (usize, usize) {
455+
/// Returns the line and column of the given `pos` in `input`.
456+
pub(crate) fn line_col(input: &str, pos: usize, start: (usize, usize)) -> (usize, usize) {
456457
#[cfg(feature = "fast-line-col")]
457458
{
458-
fast_line_col(input, pos)
459+
fast_line_col(input, pos, start)
459460
}
460461
#[cfg(not(feature = "fast-line-col"))]
461462
{
462-
original_line_col(input, pos)
463+
original_line_col(input, pos, start)
463464
}
464465
}
465466

466467
#[inline]
467468
#[cfg(not(feature = "fast-line-col"))]
468-
fn original_line_col(input: &str, mut pos: usize) -> (usize, usize) {
469+
pub(crate) fn original_line_col(
470+
input: &str,
471+
mut pos: usize,
472+
start: (usize, usize),
473+
) -> (usize, usize) {
469474
// Position's pos is always a UTF-8 border.
470475
let slice = &input[..pos];
471476
let mut chars = slice.chars().peekable();
472477

473-
let mut line_col = (1, 1);
478+
let mut line_col = start;
474479

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

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

514519
let prec_ln = memchr::memrchr(b'\n', slice.as_bytes());
515520
if let Some(prec_nl_pos) = prec_ln {
516-
let lines = bytecount::count(slice[..=prec_nl_pos].as_bytes(), b'\n') + 1;
521+
let lines = bytecount::count(slice[..=prec_nl_pos].as_bytes(), b'\n') + start.0;
517522
(lines, slice[prec_nl_pos..].chars().count())
518523
} else {
519-
(1, slice.chars().count() + 1)
524+
(start.0, slice.chars().count() + start.1)
520525
}
521526
}
522527

0 commit comments

Comments
 (0)