diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/construct/attention.rs | 13 | ||||
-rw-r--r-- | src/construct/list_item.rs | 7 | ||||
-rw-r--r-- | src/to_html.rs | 9 | ||||
-rw-r--r-- | src/to_mdast.rs | 9 | ||||
-rw-r--r-- | src/util/slice.rs | 64 |
5 files changed, 19 insertions, 83 deletions
diff --git a/src/construct/attention.rs b/src/construct/attention.rs index 7ac41de..dd87e05 100644 --- a/src/construct/attention.rs +++ b/src/construct/attention.rs @@ -81,12 +81,9 @@ use crate::resolve::Name as ResolveName; use crate::state::{Name as StateName, State}; use crate::subtokenize::Subresult; use crate::tokenizer::Tokenizer; -use crate::util::{ - char::{ - after_index as char_after_index, before_index as char_before_index, classify_opt, - Kind as CharacterKind, - }, - slice::Slice, +use crate::util::char::{ + after_index as char_after_index, before_index as char_before_index, classify_opt, + Kind as CharacterKind, }; use alloc::{vec, vec::Vec}; @@ -242,9 +239,7 @@ fn get_sequences(tokenizer: &mut Tokenizer) -> Vec<Sequence> { let end = index + 1; let exit = &tokenizer.events[end]; - let marker = Slice::from_point(tokenizer.parse_state.bytes, &enter.point) - .head() - .unwrap(); + let marker = tokenizer.parse_state.bytes[enter.point.index]; let before = classify_opt(char_before_index( tokenizer.parse_state.bytes, enter.point.index, diff --git a/src/construct/list_item.rs b/src/construct/list_item.rs index 87cf0b1..ff22d3b 100644 --- a/src/construct/list_item.rs +++ b/src/construct/list_item.rs @@ -386,12 +386,7 @@ pub fn resolve(tokenizer: &mut Tokenizer) -> Option<Subresult> { let end = skip::opt(&tokenizer.events, index, &[Name::ListItem]) - 1; let marker = skip::to(&tokenizer.events, index, &[Name::ListItemMarker]); // Guaranteed to be a valid ASCII byte. - let marker = Slice::from_index( - tokenizer.parse_state.bytes, - tokenizer.events[marker].point.index, - ) - .head() - .unwrap(); + let marker = tokenizer.parse_state.bytes[tokenizer.events[marker].point.index]; let current = (marker, balance, index, end); let mut list_index = lists_wip.len(); diff --git a/src/to_html.rs b/src/to_html.rs index ab55376..6811350 100644 --- a/src/to_html.rs +++ b/src/to_html.rs @@ -1271,12 +1271,9 @@ fn on_exit_heading_setext_underline_sequence(context: &mut CompileContext) { .heading_setext_buffer .take() .expect("`heading_atx_rank` must be set in headings"); - let head = Slice::from_position( - context.bytes, - &Position::from_exit_event(context.events, context.index), - ) - .head(); - let rank = if head == Some(b'-') { "2" } else { "1" }; + let position = Position::from_exit_event(context.events, context.index); + let head = context.bytes[position.start.index]; + let rank = if head == b'-' { "2" } else { "1" }; context.line_ending_if_needed(); context.push("<h"); diff --git a/src/to_mdast.rs b/src/to_mdast.rs index 1d25beb..1ce5826 100644 --- a/src/to_mdast.rs +++ b/src/to_mdast.rs @@ -1278,12 +1278,9 @@ fn on_exit_heading_setext_text(context: &mut CompileContext) { /// Handle [`Exit`][Kind::Exit]:[`HeadingSetextUnderlineSequence`][Name::HeadingSetextUnderlineSequence]. fn on_exit_heading_setext_underline_sequence(context: &mut CompileContext) { - let head = Slice::from_position( - context.bytes, - &SlicePosition::from_exit_event(context.events, context.index), - ) - .head(); - let depth = if head == Some(b'-') { 2 } else { 1 }; + let position = SlicePosition::from_exit_event(context.events, context.index); + let head = context.bytes[position.start.index]; + let depth = if head == b'-' { 2 } else { 1 }; if let Node::Heading(node) = context.tail_mut() { node.depth = depth; diff --git a/src/util/slice.rs b/src/util/slice.rs index 13a22dd..4c942b9 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -26,20 +26,17 @@ impl<'a> Position<'a> { /// When `markdown-rs` is used, this function never panics. pub fn from_exit_event(events: &'a [Event], index: usize) -> Position<'a> { let exit = &events[index]; - debug_assert_eq!( - exit.kind, - Kind::Exit, - "expected `from_exit_event` to be called on `exit` event" - ); + debug_assert_eq!(exit.kind, Kind::Exit, "expected `exit` event"); let mut enter_index = index - 1; loop { let enter = &events[enter_index]; if enter.kind == Kind::Enter && enter.name == exit.name { - return Position { + let position = Position { start: &enter.point, end: &exit.point, }; + return position; } enter_index -= 1; @@ -70,37 +67,6 @@ pub struct Slice<'a> { } impl<'a> Slice<'a> { - /// Get a slice for a single point. - pub fn from_point(bytes: &'a [u8], point: &Point) -> Slice<'a> { - let mut before = point.vs; - let mut start = point.index; - let end = if start < bytes.len() { - start + 1 - } else { - start - }; - - // If we have virtual spaces before, it means we are past the actual - // character at that index, and those virtual spaces. - if before > 0 { - before = TAB_SIZE - before; - start += 1; - }; - - Slice { - bytes: if start < end { &bytes[start..end] } else { &[] }, - before, - after: 0, - } - } - - /// Get a slice for a single index. - /// - /// > π **Note**: indices cannot represent virtual spaces. - pub fn from_index(bytes: &'a [u8], index: usize) -> Slice<'a> { - Slice::from_indices(bytes, index, index + 1) - } - /// Get a slice for a position. pub fn from_position(bytes: &'a [u8], position: &Position) -> Slice<'a> { let mut before = position.start.vs; @@ -145,18 +111,6 @@ impl<'a> Slice<'a> { self.bytes.len() + self.before + self.after } - /// Get the first byte in this slice, representing a virtual space as a - /// space. - pub fn head(&self) -> Option<u8> { - if self.before > 0 { - Some(b' ') - } else if self.bytes.is_empty() { - None - } else { - Some(self.bytes[0]) - } - } - /// Turn the slice into a `&str`. /// /// > π **Note**: cannot represent virtual spaces. @@ -166,7 +120,7 @@ impl<'a> Slice<'a> { /// Turn the slice into a `String`. /// - /// Support virtual spaces. + /// Supports virtual spaces. pub fn serialize(&self) -> String { let mut string = String::with_capacity(self.len()); let mut index = self.before; @@ -175,12 +129,10 @@ impl<'a> Slice<'a> { index -= 1; } string.push_str(self.as_str()); - index = self.after; - while index > 0 { - string.push(' '); - index -= 1; - } - + debug_assert_eq!(self.after, 0, "expected no trailing vs"); + // If the above ever starts erroring, handle the same as `self.before` + // above but with `self.after`. + // Itβd currently be unused code. string } } |