Skip to content

Commit ff8171d

Browse files
committed
Refactor slice algorithms some more
1 parent e694077 commit ff8171d

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/util/slice.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::event::{Event, Kind, Point};
44
use crate::util::constant::TAB_SIZE;
5-
use alloc::string::String;
5+
use alloc::{format, string::String, vec};
66
use core::str;
77

88
/// A range between two points.
@@ -24,20 +24,19 @@ impl<'a> Position<'a> {
2424
///
2525
/// This function panics if an enter event is given.
2626
/// When `markdown-rs` is used, this function never panics.
27-
#[cfg_attr(tarpaulin, ignore)]
2827
pub fn from_exit_event(events: &'a [Event], index: usize) -> Position<'a> {
29-
let exit = &events[index];
30-
debug_assert_eq!(exit.kind, Kind::Exit, "expected `exit` event");
31-
let mut enter_index = index - 1;
28+
debug_assert_eq!(events[index].kind, Kind::Exit, "expected `exit` event");
29+
let end = &events[index].point;
30+
let name = &events[index].name;
31+
let mut index = index - 1;
3232

33-
while events[enter_index].kind != Kind::Enter || events[enter_index].name != exit.name {
34-
enter_index -= 1;
33+
while !(events[index].kind == Kind::Enter && events[index].name == *name) {
34+
index -= 1;
3535
}
3636

37-
Position {
38-
start: &events[enter_index].point,
39-
end: &exit.point,
40-
}
37+
let start = &events[index].point;
38+
39+
Position { start, end }
4140
}
4241

4342
/// Turn a position into indices.
@@ -118,19 +117,9 @@ impl<'a> Slice<'a> {
118117
/// Turn the slice into a `String`.
119118
///
120119
/// Supports virtual spaces.
121-
#[cfg_attr(tarpaulin, ignore)]
122120
pub fn serialize(&self) -> String {
123-
debug_assert_eq!(self.after, 0, "expected no trailing vs");
124-
// If the above ever starts erroring, handle the same as `self.before`
125-
// above but with `self.after`.
126-
// It’d currently be unused code.
127-
let mut string = String::with_capacity(self.len());
128-
let mut index = self.before;
129-
while index > 0 {
130-
string.push(' ');
131-
index -= 1;
132-
}
133-
string.push_str(self.as_str());
134-
string
121+
let prefix = String::from_utf8(vec![b' '; self.before]).unwrap();
122+
let suffix = String::from_utf8(vec![b' '; self.after]).unwrap();
123+
format!("{}{}{}", prefix, self.as_str(), suffix)
135124
}
136125
}

0 commit comments

Comments
 (0)