Skip to content

Commit 340e0ba

Browse files
committed
count newlines correctly.
1 parent d8ad0ed commit 340e0ba

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/parser/table.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ fn try_opening_header<'a>(
101101
container.append(table);
102102

103103
let header = parser.add_child(table, NodeValue::TableRow(true), start.column);
104-
eprintln!("cc {container_content:?}");
105104
{
106105
let header_ast = &mut header.data_mut();
107106
header_ast.sourcepos.start.line = start.line;
@@ -304,7 +303,7 @@ fn try_inserting_table_header_paragraph<'a>(
304303
let start = container_ast.sourcepos.start;
305304

306305
let mut paragraph = Ast::new(NodeValue::Paragraph, start);
307-
paragraph.sourcepos.end.line = start.line + newlines - 1; // This assumes endlines are one byte each.
306+
paragraph.sourcepos.end.line = start.line + newlines - 1;
308307

309308
for n in 0..newlines {
310309
paragraph.line_offsets.push(container_ast.line_offsets[n]);

src/strings.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,25 @@ pub fn trim_start_match<'s>(s: &'s str, pat: &str) -> &'s str {
415415
s.strip_prefix(pat).unwrap_or(s)
416416
}
417417

418-
// XXX: this counts \r\n as two newlines.
419418
pub fn count_newlines(input: &str) -> (usize, usize) {
419+
let bytes = input.as_bytes();
420420
let mut num_lines = 0;
421421
let mut last_line_start = 0;
422-
for (i, &c) in input.as_bytes().iter().enumerate() {
423-
if is_line_end_char(c) {
424-
num_lines += 1;
425-
last_line_start = i + 1;
422+
let mut i = 0;
423+
while i < input.len() {
424+
match bytes[i] {
425+
b'\r' if i + 1 < input.len() && bytes[i + 1] == b'\n' => {
426+
i += 1;
427+
num_lines += 1;
428+
last_line_start = i + 1;
429+
}
430+
b'\r' | b'\n' => {
431+
num_lines += 1;
432+
last_line_start = i + 1;
433+
}
434+
_ => {}
426435
}
436+
i += 1;
427437
}
428438
let last_line_len = input.len() - last_line_start;
429439
(num_lines, last_line_len)

0 commit comments

Comments
 (0)