comparing begin and end offsets for top-level sections and headlines.
This commit is contained in:
21
src/compare/util.rs
Normal file
21
src/compare/util.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::parser::Source;
|
||||
|
||||
/// 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 the offset into source that the rust object exists at.
|
||||
///
|
||||
/// These offsets are zero-based unlike the elisp ones.
|
||||
pub fn get_offsets<'s, S: Source<'s>>(source: &'s str, rust_object: &'s S) -> (usize, usize) {
|
||||
let rust_object_source = rust_object.get_source();
|
||||
assert!(is_slice_of(source, rust_object_source));
|
||||
let offset = rust_object_source.as_ptr() as usize - source.as_ptr() as usize;
|
||||
let end = offset + rust_object_source.len();
|
||||
(offset, end)
|
||||
}
|
||||
Reference in New Issue
Block a user