Move some functions into util.
This commit is contained in:
@@ -23,3 +23,48 @@ pub fn immediate_in_section<'r, 's, 'x>(context: Context<'r, 's>, section_name:
|
||||
}
|
||||
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, "💛");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user