Skip to content

Commit a374c3c

Browse files
committed
little cleanups.
1 parent ebbf7c8 commit a374c3c

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

src/arena_tree.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ impl<'a, T> Node<'a, T> {
205205
self.last_child.set(Some(new_child));
206206
}
207207

208+
/// Append multiple new children to this node, after existing children.
209+
pub fn extend(&'a self, new_children: impl IntoIterator<Item = &'a Node<'a, T>>) {
210+
for child in new_children.into_iter() {
211+
self.append(child);
212+
}
213+
}
214+
208215
/// Prepend a new child to this node, before existing children.
209216
pub fn prepend(&'a self, new_child: &'a Node<'a, T>) {
210217
new_child.detach();

src/parser/mod.rs

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,16 @@ where
348348
self.first_nonspace_column = self.column;
349349

350350
loop {
351-
if self.first_nonspace >= line.len() {
352-
break;
353-
}
354-
match bytes[self.first_nonspace] {
355-
32 => {
351+
match bytes.get(self.first_nonspace) {
352+
Some(b' ') => {
356353
self.first_nonspace += 1;
357354
self.first_nonspace_column += 1;
358355
chars_to_tab -= 1;
359356
if chars_to_tab == 0 {
360357
chars_to_tab = TAB_STOP;
361358
}
362359
}
363-
9 => {
360+
Some(b'\t') => {
364361
self.first_nonspace += 1;
365362
self.first_nonspace_column += chars_to_tab;
366363
chars_to_tab = TAB_STOP;
@@ -473,9 +470,10 @@ where
473470
};
474471

475472
if matched >= fence_length {
476-
if let NodeValue::CodeBlock(ref mut ncb) = ast.value {
477-
ncb.closed = true;
478-
}
473+
let NodeValue::CodeBlock(ref mut ncb) = ast.value else {
474+
unreachable!();
475+
};
476+
ncb.closed = true;
479477
self.advance_offset(line, matched, false);
480478
self.current = self.finalize_borrowed(container, ast).unwrap();
481479
return None;
@@ -597,8 +595,8 @@ where
597595
}
598596
}
599597
// fallback to start position (better than column 0)
600-
let sp = node.data().sourcepos.start;
601-
node.data_mut().sourcepos.end = sp;
598+
let mut ast = node.data_mut();
599+
ast.sourcepos.end = ast.sourcepos.start;
602600
}
603601
}
604602
}
@@ -857,7 +855,7 @@ where
857855
fence_char: line.as_bytes()[first_nonspace],
858856
fence_length: matched,
859857
fence_offset: first_nonspace - offset,
860-
info: String::with_capacity(10),
858+
info: String::new(),
861859
literal: String::new(),
862860
closed: false,
863861
};
@@ -1091,13 +1089,12 @@ where
10911089
return false;
10921090
}
10931091

1094-
let parent = container.parent();
1095-
if parent.is_none() {
1092+
let Some(parent) = container.parent() else {
10961093
return false;
1097-
}
1094+
};
10981095

10991096
tight = true;
1100-
*container = parent.unwrap();
1097+
*container = parent;
11011098
container.last_child().unwrap()
11021099
}
11031100
};
@@ -1335,13 +1332,15 @@ where
13351332
let bytes = line.as_bytes();
13361333
while count > 0 {
13371334
match bytes[self.offset] {
1338-
9 => {
1335+
b'\t' => {
13391336
let chars_to_tab = TAB_STOP - (self.column % TAB_STOP);
13401337
if columns {
13411338
self.partially_consumed_tab = chars_to_tab > count;
13421339
let chars_to_advance = min(count, chars_to_tab);
13431340
self.column += chars_to_advance;
1344-
self.offset += if self.partially_consumed_tab { 0 } else { 1 };
1341+
if !self.partially_consumed_tab {
1342+
self.offset += 1;
1343+
};
13451344
count -= chars_to_advance;
13461345
} else {
13471346
self.partially_consumed_tab = false;
@@ -1467,18 +1466,19 @@ where
14671466
};
14681467
let count = self.first_nonspace - self.offset;
14691468

1470-
// In a rare case the above `chop` operation can leave
1471-
// the line shorter than the recorded `first_nonspace`
1472-
// This happens with ATX headers containing no header
1473-
// text, multiple spaces and trailing hashes, e.g
1469+
// In some cases the `chop_trailing_hashes` above
1470+
// can leave the line shorter than the recorded
1471+
// `first_nonspace` This happens with ATX headers
1472+
// containing no header text, multiple spaces and
1473+
// trailing hashes, e.g
14741474
//
14751475
// ### ###
14761476
//
1477-
// In this case `first_nonspace` indexes into the second
1478-
// set of hashes, while `chop_trailing_hashtags` truncates
1479-
// `line` to just `###` (the first three hashes).
1480-
// In this case there's no text to add, and no further
1481-
// processing to be done.
1477+
// In this case `first_nonspace` indexes into the
1478+
// second set of hashes, while `chop_trailing_hashtags`
1479+
// truncates `line` to just `###` (the first three
1480+
// hashes). In this case there's no text to add, and no
1481+
// further processing to be done.
14821482
let have_line_text = self.first_nonspace <= line.len();
14831483

14841484
if have_line_text {
@@ -1514,9 +1514,9 @@ where
15141514
}
15151515
}
15161516
if self.offset < line.len() {
1517-
// since whitespace is stripped off the beginning of lines, we need to keep
1518-
// track of how much was stripped off. This allows us to properly calculate
1519-
// inline sourcepos during inline processing.
1517+
// Since whitespace is stripped off the beginning of lines, we need
1518+
// to keep track of how much was stripped off. This allows us to
1519+
// properly calculate inline sourcepos during inline processing.
15201520
ast.line_offsets.push(self.offset);
15211521

15221522
ast.content.push_str(&line[self.offset..]);
@@ -1537,10 +1537,7 @@ where
15371537
if self.options.extension.footnotes {
15381538
// Append auto-generated inline footnote definitions
15391539
if self.options.extension.inline_footnotes {
1540-
let inline_defs = self.footnote_defs.take();
1541-
for def in inline_defs.into_iter() {
1542-
self.root.append(def);
1543-
}
1540+
self.root.extend(self.footnote_defs.take());
15441541
}
15451542

15461543
self.process_footnotes();
@@ -1553,21 +1550,19 @@ where
15531550

15541551
fn resolve_reference_link_definitions(&mut self, content: &mut String) -> bool {
15551552
let mut seeked = 0;
1556-
let mut rrs_to_add = vec![];
1553+
let mut rrs = vec![];
15571554

1558-
{
1559-
let bytes = content.as_bytes();
1560-
while seeked < content.len() && bytes[seeked] == b'[' {
1561-
if let Some((offset, rr)) = self.parse_reference_inline(&content[seeked..]) {
1562-
seeked += offset;
1563-
rrs_to_add.extend(rr);
1564-
} else {
1565-
break;
1566-
}
1555+
let bytes = content.as_bytes();
1556+
while seeked < content.len() && bytes[seeked] == b'[' {
1557+
if let Some((offset, rr)) = self.parse_reference_inline(&content[seeked..]) {
1558+
seeked += offset;
1559+
rrs.extend(rr);
1560+
} else {
1561+
break;
15671562
}
15681563
}
15691564

1570-
for (lab, rr) in rrs_to_add {
1565+
for (lab, rr) in rrs {
15711566
self.refmap.map.entry(lab).or_insert(rr);
15721567
}
15731568

@@ -1585,9 +1580,7 @@ where
15851580
let content = &mut ast.content;
15861581
let parent = node.parent();
15871582

1588-
if matches!(ast.value, NodeValue::Table(..)) {
1589-
// handles its own sourcepos.end.
1590-
} else if self.curline_len == 0 {
1583+
if self.curline_len == 0 {
15911584
ast.sourcepos.end = (self.line_number, self.last_line_length).into();
15921585
} else if match ast.value {
15931586
NodeValue::Document => true,
@@ -1598,7 +1591,7 @@ where
15981591
ast.sourcepos.end = (self.line_number, self.curline_end_col).into();
15991592
} else if matches!(
16001593
ast.value,
1601-
NodeValue::ThematicBreak | NodeValue::TableRow(..)
1594+
NodeValue::ThematicBreak | NodeValue::TableRow(..) | NodeValue::Table(..)
16021595
) {
16031596
// sourcepos.end set by itself or managed below.
16041597
} else {
@@ -1631,8 +1624,9 @@ where
16311624
pos += 1;
16321625
}
16331626

1634-
let mut info = entity::unescape_html(&content[..pos]).into();
1635-
strings::trim(&mut info);
1627+
let mut info = entity::unescape_html(&content[..pos]);
1628+
strings::trim_cow(&mut info);
1629+
let mut info = info.into_owned();
16361630
strings::unescape(&mut info);
16371631
if info.is_empty() {
16381632
ncb.info = self

0 commit comments

Comments
 (0)