Skip to content

Commit dc96b0e

Browse files
author
Ashe Connor
authored
Merge pull request #83 from brson/cleanup
Cleanup `feed` and a NUL replacement fix
2 parents 1e9823b + 81f9710 commit dc96b0e

File tree

5 files changed

+65
-116
lines changed

5 files changed

+65
-116
lines changed

src/nodes.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,6 @@ pub struct Ast {
315315
/// The line in the input document the node starts at.
316316
pub start_line: u32,
317317

318-
/// The column in the input document the node starts at.
319-
pub start_column: usize,
320-
321-
/// The line in the input document the node ends at.
322-
pub end_line: u32,
323-
324-
/// The column in the input document the node ends at.
325-
pub end_column: usize,
326-
327318
#[doc(hidden)]
328319
pub content: Vec<u8>,
329320
#[doc(hidden)]
@@ -333,14 +324,11 @@ pub struct Ast {
333324
}
334325

335326
#[doc(hidden)]
336-
pub fn make_block(value: NodeValue, start_line: u32, start_column: usize) -> Ast {
327+
pub fn make_block(value: NodeValue, start_line: u32) -> Ast {
337328
Ast {
338329
value: value,
339330
content: vec![],
340331
start_line: start_line,
341-
start_column: start_column,
342-
end_line: start_line,
343-
end_column: 0,
344332
open: true,
345333
last_line_blank: false,
346334
}

src/parser/inlines.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,9 +1125,6 @@ pub fn make_inline<'a>(arena: &'a Arena<AstNode<'a>>, value: NodeValue) -> &'a A
11251125
value: value,
11261126
content: vec![],
11271127
start_line: 0,
1128-
start_column: 0,
1129-
end_line: 0,
1130-
end_column: 0,
11311128
open: false,
11321129
last_line_blank: false,
11331130
};

src/parser/mod.rs

Lines changed: 34 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@ pub fn parse_document<'a>(
3333
value: NodeValue::Document,
3434
content: vec![],
3535
start_line: 0,
36-
start_column: 0,
37-
end_line: 0,
38-
end_column: 0,
3936
open: true,
4037
last_line_blank: false,
4138
})));
4239
let mut parser = Parser::new(arena, root, options);
43-
parser.feed(buffer, true);
40+
parser.feed(buffer);
4441
parser.finish()
4542
}
4643

@@ -58,8 +55,6 @@ pub struct Parser<'a, 'o> {
5855
blank: bool,
5956
partially_consumed_tab: bool,
6057
last_line_length: usize,
61-
linebuf: Vec<u8>,
62-
last_buffer_ended_with_cr: bool,
6358
options: &'o ComrakOptions,
6459
}
6560

@@ -305,7 +300,7 @@ struct FootnoteDefinition<'a> {
305300
}
306301

307302
impl<'a, 'o> Parser<'a, 'o> {
308-
pub fn new(
303+
fn new(
309304
arena: &'a Arena<AstNode<'a>>,
310305
root: &'a AstNode<'a>,
311306
options: &'o ComrakOptions,
@@ -324,69 +319,53 @@ impl<'a, 'o> Parser<'a, 'o> {
324319
blank: false,
325320
partially_consumed_tab: false,
326321
last_line_length: 0,
327-
linebuf: Vec::with_capacity(80),
328-
last_buffer_ended_with_cr: false,
329322
options: options,
330323
}
331324
}
332325

333-
fn feed(&mut self, s: &str, eof: bool) {
326+
fn feed(&mut self, s: &str) {
334327
let s = s.as_bytes();
335328
let mut i = 0;
336-
let buffer = s;
337-
let sz = buffer.len();
338-
339-
if self.last_buffer_ended_with_cr && buffer[i] == b'\n' {
340-
i += 1;
341-
}
342-
self.last_buffer_ended_with_cr = false;
329+
let sz = s.len();
330+
let mut linebuf = vec![];
343331

344332
while i < sz {
345-
let mut process = false;
333+
let mut process = true;
346334
let mut eol = i;
347335
while eol < sz {
348-
if strings::is_line_end_char(buffer[eol]) {
349-
process = true;
336+
if strings::is_line_end_char(s[eol]) {
350337
break;
351338
}
352-
if buffer[eol] == 0 {
339+
if s[eol] == 0 {
340+
process = false;
353341
break;
354342
}
355343
eol += 1;
356344
}
357345

358-
if eol >= sz && eof {
359-
process = true;
360-
}
361-
362346
if process {
363-
if !self.linebuf.is_empty() {
364-
self.linebuf.extend_from_slice(&s[i..eol]);
365-
let linebuf = mem::replace(&mut self.linebuf, Vec::with_capacity(80));
347+
if !linebuf.is_empty() {
348+
linebuf.extend_from_slice(&s[i..eol]);
366349
self.process_line(&linebuf);
367-
} else if sz > eol && buffer[eol] == b'\n' {
350+
linebuf.truncate(0);
351+
} else if sz > eol && s[eol] == b'\n' {
368352
self.process_line(&s[i..eol + 1]);
369353
} else {
370354
self.process_line(&s[i..eol]);
371355
}
372-
} else if eol < sz && buffer[eol] == b'\0' {
373-
self.linebuf.extend_from_slice(&s[i..eol]);
374-
self.linebuf
375-
.extend_from_slice(&"\u{fffd}".to_string().into_bytes());
376-
eol += 1;
377-
} else {
378-
self.linebuf.extend_from_slice(&s[i..eol]);
379-
}
380356

381-
i = eol;
382-
if i < sz && buffer[i] == b'\r' {
383-
i += 1;
384-
if i == sz {
385-
self.last_buffer_ended_with_cr = true;
357+
i = eol;
358+
if i < sz && s[i] == b'\r' {
359+
i += 1;
386360
}
387-
}
388-
if i < sz && buffer[i] == b'\n' {
389-
i += 1;
361+
if i < sz && s[i] == b'\n' {
362+
i += 1;
363+
}
364+
} else {
365+
debug_assert!(eol < sz && s[eol] == b'\0');
366+
linebuf.extend_from_slice(&s[i..eol]);
367+
linebuf.extend_from_slice(&"\u{fffd}".to_string().into_bytes());
368+
i = eol + 1;
390369
}
391370
}
392371
}
@@ -558,14 +537,13 @@ impl<'a, 'o> Parser<'a, 'o> {
558537
let indented = self.indent >= CODE_INDENT;
559538

560539
if !indented && line[self.first_nonspace] == b'>' {
561-
let blockquote_startpos = self.first_nonspace;
562540
let offset = self.first_nonspace + 1 - self.offset;
563541
self.advance_offset(line, offset, false);
564542
if strings::is_space_or_tab(line[self.offset]) {
565543
self.advance_offset(line, 1, true);
566544
}
567545
*container =
568-
self.add_child(*container, NodeValue::BlockQuote, blockquote_startpos + 1);
546+
self.add_child(*container, NodeValue::BlockQuote);
569547
} else if !indented
570548
&& unwrap_into(
571549
scanners::atx_heading_start(&line[self.first_nonspace..]),
@@ -577,7 +555,6 @@ impl<'a, 'o> Parser<'a, 'o> {
577555
*container = self.add_child(
578556
*container,
579557
NodeValue::Heading(NodeHeading::default()),
580-
heading_startpos + 1,
581558
);
582559

583560
let mut hashpos = line[self.first_nonspace..]
@@ -610,7 +587,7 @@ impl<'a, 'o> Parser<'a, 'o> {
610587
literal: Vec::new(),
611588
};
612589
*container =
613-
self.add_child(*container, NodeValue::CodeBlock(ncb), first_nonspace + 1);
590+
self.add_child(*container, NodeValue::CodeBlock(ncb));
614591
self.advance_offset(line, first_nonspace + matched - offset, false);
615592
} else if !indented
616593
&& (unwrap_into(
@@ -623,13 +600,12 @@ impl<'a, 'o> Parser<'a, 'o> {
623600
&mut matched,
624601
),
625602
}) {
626-
let offset = self.first_nonspace + 1;
627603
let nhb = NodeHtmlBlock {
628604
block_type: matched as u8,
629605
literal: Vec::new(),
630606
};
631607

632-
*container = self.add_child(*container, NodeValue::HtmlBlock(nhb), offset);
608+
*container = self.add_child(*container, NodeValue::HtmlBlock(nhb));
633609
} else if !indented && match container.data.borrow().value {
634610
NodeValue::Paragraph => unwrap_into(
635611
scanners::setext_heading_line(&line[self.first_nonspace..]),
@@ -653,8 +629,7 @@ impl<'a, 'o> Parser<'a, 'o> {
653629
&mut matched,
654630
),
655631
} {
656-
let offset = self.first_nonspace + 1;
657-
*container = self.add_child(*container, NodeValue::ThematicBreak, offset);
632+
*container = self.add_child(*container, NodeValue::ThematicBreak);
658633
let adv = line.len() - 1 - self.offset;
659634
self.advance_offset(line, adv, false);
660635
} else if !indented && self.options.ext_footnotes
@@ -666,11 +641,9 @@ impl<'a, 'o> Parser<'a, 'o> {
666641
c = c.split(|&e| e == b']').next().unwrap();
667642
let offset = self.first_nonspace + matched - self.offset;
668643
self.advance_offset(line, offset, false);
669-
let offset = self.first_nonspace + matched + 1;
670644
*container = self.add_child(
671645
*container,
672646
NodeValue::FootnoteDefinition(c.to_vec()),
673-
offset,
674647
);
675648
} else if (!indented || match container.data.borrow().value {
676649
NodeValue::List(..) => true,
@@ -714,16 +687,14 @@ impl<'a, 'o> Parser<'a, 'o> {
714687

715688
nl.marker_offset = self.indent;
716689

717-
let offset = self.first_nonspace + 1;
718690
if match container.data.borrow().value {
719691
NodeValue::List(ref mnl) => !lists_match(&nl, mnl),
720692
_ => true,
721693
} {
722-
*container = self.add_child(*container, NodeValue::List(nl), offset);
694+
*container = self.add_child(*container, NodeValue::List(nl));
723695
}
724696

725-
let offset = self.first_nonspace + 1;
726-
*container = self.add_child(*container, NodeValue::Item(nl), offset);
697+
*container = self.add_child(*container, NodeValue::Item(nl));
727698
} else if indented && !maybe_lazy && !self.blank {
728699
self.advance_offset(line, CODE_INDENT, true);
729700
let ncb = NodeCodeBlock {
@@ -734,8 +705,7 @@ impl<'a, 'o> Parser<'a, 'o> {
734705
info: vec![],
735706
literal: Vec::new(),
736707
};
737-
let offset = self.offset + 1;
738-
*container = self.add_child(*container, NodeValue::CodeBlock(ncb), offset);
708+
*container = self.add_child(*container, NodeValue::CodeBlock(ncb));
739709
} else {
740710
let new_container = if !indented && self.options.ext_table {
741711
table::try_opening_block(self, *container, line)
@@ -898,13 +868,12 @@ impl<'a, 'o> Parser<'a, 'o> {
898868
&mut self,
899869
mut parent: &'a AstNode<'a>,
900870
value: NodeValue,
901-
start_column: usize,
902871
) -> &'a AstNode<'a> {
903872
while !nodes::can_contain_type(parent, &value) {
904873
parent = self.finalize(parent).unwrap();
905874
}
906875

907-
let child = make_block(value, self.line_number, start_column);
876+
let child = make_block(value, self.line_number);
908877
let node = self.arena.alloc(Node::new(RefCell::new(child)));
909878
parent.append(node);
910879
node
@@ -1010,8 +979,7 @@ impl<'a, 'o> Parser<'a, 'o> {
1010979
self.add_line(container, &line);
1011980
}
1012981
} else {
1013-
let start_column = self.first_nonspace + 1;
1014-
container = self.add_child(container, NodeValue::Paragraph, start_column);
982+
container = self.add_child(container, NodeValue::Paragraph);
1015983
let count = self.first_nonspace - self.offset;
1016984
self.advance_offset(line, count, false);
1017985
self.add_line(container, line);
@@ -1038,12 +1006,7 @@ impl<'a, 'o> Parser<'a, 'o> {
10381006
}
10391007
}
10401008

1041-
pub fn finish(&mut self) -> &'a AstNode<'a> {
1042-
if !self.linebuf.is_empty() {
1043-
let linebuf = mem::replace(&mut self.linebuf, vec![]);
1044-
self.process_line(&linebuf);
1045-
}
1046-
1009+
fn finish(&mut self) -> &'a AstNode<'a> {
10471010
self.finalize_document();
10481011
self.postprocess_text_nodes(self.root);
10491012
self.root
@@ -1073,28 +1036,6 @@ impl<'a, 'o> Parser<'a, 'o> {
10731036
assert!(ast.open);
10741037
ast.open = false;
10751038

1076-
if !self.linebuf.is_empty() {
1077-
ast.end_line = self.line_number;
1078-
ast.end_column = self.last_line_length;
1079-
} else if match ast.value {
1080-
NodeValue::Document => true,
1081-
NodeValue::CodeBlock(ref ncb) => ncb.fenced,
1082-
NodeValue::Heading(ref nh) => nh.setext,
1083-
_ => false,
1084-
} {
1085-
ast.end_line = self.line_number;
1086-
ast.end_column = self.linebuf.len();
1087-
if ast.end_column > 0 && self.linebuf[ast.end_column - 1] == b'\n' {
1088-
ast.end_column -= 1;
1089-
}
1090-
if ast.end_column > 0 && self.linebuf[ast.end_column - 1] == b'\r' {
1091-
ast.end_column -= 1;
1092-
}
1093-
} else {
1094-
ast.end_line = self.line_number - 1;
1095-
ast.end_column = self.last_line_length;
1096-
}
1097-
10981039
let content = &mut ast.content;
10991040
let mut pos = 0;
11001041

src/parser/table.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,16 @@ fn try_opening_header<'a, 'o>(
5858
});
5959
}
6060

61-
let start_column = container.data.borrow().start_column;
6261
let child = make_block(
6362
NodeValue::Table(alignments),
6463
parser.line_number,
65-
start_column,
6664
);
6765
let table = parser.arena.alloc(Node::new(RefCell::new(child)));
6866
container.append(table);
6967

70-
let header = parser.add_child(table, NodeValue::TableRow(true), start_column);
68+
let header = parser.add_child(table, NodeValue::TableRow(true));
7169
for header_str in header_row {
72-
let header_cell = parser.add_child(header, NodeValue::TableCell, start_column);
70+
let header_cell = parser.add_child(header, NodeValue::TableCell);
7371
header_cell.data.borrow_mut().content = header_str;
7472
}
7573

@@ -92,15 +90,13 @@ fn try_opening_row<'a, 'o>(
9290
let new_row = parser.add_child(
9391
container,
9492
NodeValue::TableRow(false),
95-
container.data.borrow().start_column,
9693
);
9794

9895
let mut i = 0;
9996
while i < min(alignments.len(), this_row.len()) {
10097
let cell = parser.add_child(
10198
new_row,
10299
NodeValue::TableCell,
103-
container.data.borrow().start_column,
104100
);
105101
cell.data.borrow_mut().content = this_row[i].clone();
106102
i += 1;
@@ -110,7 +106,6 @@ fn try_opening_row<'a, 'o>(
110106
parser.add_child(
111107
new_row,
112108
NodeValue::TableCell,
113-
container.data.borrow().start_column,
114109
);
115110
i += 1;
116111
}

0 commit comments

Comments
 (0)