Move some functions into util.
This commit is contained in:
parent
d582c8603a
commit
3c26933e7f
@ -27,6 +27,8 @@ use super::error::Res;
|
|||||||
use super::object::Object;
|
use super::object::Object;
|
||||||
use super::parser_with_context::parser_with_context;
|
use super::parser_with_context::parser_with_context;
|
||||||
use super::source::Source;
|
use super::source::Source;
|
||||||
|
use super::util::get_consumed;
|
||||||
|
use super::util::get_one_before;
|
||||||
use super::Context;
|
use super::Context;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -177,48 +179,3 @@ fn start_of_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st
|
|||||||
};
|
};
|
||||||
Ok((input, ()))
|
Ok((input, ()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get one character from before the current position.
|
|
||||||
fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Option<&'s str> {
|
|
||||||
assert!(is_slice_of(document, current_position));
|
|
||||||
if document.as_ptr() as usize == current_position.as_ptr() as usize {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let offset = current_position.as_ptr() as usize - document.as_ptr() as usize;
|
|
||||||
let previous_character_offset = document.floor_char_boundary(offset - 1);
|
|
||||||
Some(&document[previous_character_offset..offset])
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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: &'s str, remaining: &'s str) -> &'s str {
|
|
||||||
assert!(is_slice_of(input, remaining));
|
|
||||||
let source = {
|
|
||||||
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
|
||||||
&input[..offset]
|
|
||||||
};
|
|
||||||
source
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn get_one_before_unicode() {
|
|
||||||
let input = "🧡💛💚💙💜";
|
|
||||||
let (green_heart_index, _) = input.char_indices().skip(2).next().unwrap();
|
|
||||||
let starting_with_green_heart = &input[green_heart_index..];
|
|
||||||
let yellow_heart = get_one_before(input, starting_with_green_heart).unwrap();
|
|
||||||
assert!(is_slice_of(input, yellow_heart));
|
|
||||||
assert_eq!(yellow_heart, "💛");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,6 @@ use nom::combinator::recognize;
|
|||||||
use nom::multi::many1;
|
use nom::multi::many1;
|
||||||
use nom::sequence::tuple;
|
use nom::sequence::tuple;
|
||||||
|
|
||||||
use crate::parser::document::get_consumed;
|
|
||||||
use crate::parser::object::standard_set_object;
|
use crate::parser::object::standard_set_object;
|
||||||
use crate::parser::parser_context::ChainBehavior;
|
use crate::parser::parser_context::ChainBehavior;
|
||||||
use crate::parser::parser_context::ContextElement;
|
use crate::parser::parser_context::ContextElement;
|
||||||
@ -19,6 +18,7 @@ use super::error::Res;
|
|||||||
use super::greater_element::PlainList;
|
use super::greater_element::PlainList;
|
||||||
use super::lesser_element::Paragraph;
|
use super::lesser_element::Paragraph;
|
||||||
use super::source::Source;
|
use super::source::Source;
|
||||||
|
use super::util::get_consumed;
|
||||||
use super::Context;
|
use super::Context;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -23,3 +23,48 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name:
|
|||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get one character from before the current position.
|
||||||
|
pub fn get_one_before<'s>(document: &'s str, current_position: &'s str) -> Option<&'s str> {
|
||||||
|
assert!(is_slice_of(document, current_position));
|
||||||
|
if document.as_ptr() as usize == current_position.as_ptr() as usize {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let offset = current_position.as_ptr() as usize - document.as_ptr() as usize;
|
||||||
|
let previous_character_offset = document.floor_char_boundary(offset - 1);
|
||||||
|
Some(&document[previous_character_offset..offset])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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: &'s str, remaining: &'s str) -> &'s str {
|
||||||
|
assert!(is_slice_of(input, remaining));
|
||||||
|
let source = {
|
||||||
|
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
||||||
|
&input[..offset]
|
||||||
|
};
|
||||||
|
source
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn get_one_before_unicode() {
|
||||||
|
let input = "🧡💛💚💙💜";
|
||||||
|
let (green_heart_index, _) = input.char_indices().skip(2).next().unwrap();
|
||||||
|
let starting_with_green_heart = &input[green_heart_index..];
|
||||||
|
let yellow_heart = get_one_before(input, starting_with_green_heart).unwrap();
|
||||||
|
assert!(is_slice_of(input, yellow_heart));
|
||||||
|
assert_eq!(yellow_heart, "💛");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user