diff --git a/src/parser/org_source.rs b/src/parser/org_source.rs index 3562944..9789c8e 100644 --- a/src/parser/org_source.rs +++ b/src/parser/org_source.rs @@ -50,6 +50,12 @@ impl<'s> OrgSource<'s> { pub fn is_at_start_of_line(&self) -> bool { self.start == self.start_of_line } + + pub fn get_until(&self, other: OrgSource<'s>) -> OrgSource<'s> { + assert!(other.start >= self.start); + assert!(other.end <= self.end); + self.slice(..(other.start - self.start)) + } } impl<'s> InputTake for OrgSource<'s> { @@ -118,7 +124,6 @@ where .map(|idx| self.start + idx + 1) .unwrap_or(self.start_of_line); - // TODO: calculate updated values for WrappedInput OrgSource { full_source: self.full_source, start: new_start, diff --git a/src/parser/util.rs b/src/parser/util.rs index 2306cd5..f70685a 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -49,26 +49,9 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name: false } -/// Check if the child string slice is a slice of the parent string slice. -fn is_slice_of(parent: &str, child: &str) -> bool { - let parent_start = parent.as_ptr() as usize; - let parent_end = parent_start + parent.len(); - let child_start = child.as_ptr() as usize; - let child_end = child_start + child.len(); - child_start >= parent_start && child_end <= parent_end -} - /// Get a slice of the string that was consumed in a parser using the original input to the parser and the remaining input after the parser. pub fn get_consumed<'s>(input: OrgSource<'s>, remaining: OrgSource<'s>) -> OrgSource<'s> { - // TODO: This should be replaced with new logic now that we are wrapping the input type. - let input = Into::<&str>::into(&input); - let remaining = Into::<&str>::into(&remaining); - assert!(is_slice_of(input, remaining)); - let source = { - let offset = remaining.as_ptr() as usize - input.as_ptr() as usize; - &input[..offset] - }; - source.into() + input.get_until(remaining) } /// A line containing only whitespace and then a line break