diff --git a/tor-netdoc/src/doc/microdesc.rs b/tor-netdoc/src/doc/microdesc.rs index 8ca13b3ba..1dc2fc1e4 100644 --- a/tor-netdoc/src/doc/microdesc.rs +++ b/tor-netdoc/src/doc/microdesc.rs @@ -223,7 +223,7 @@ impl Microdesc { )); } // Unwrap is safe here because we are parsing these strings from s - util::str_offset(s, first.kwd_str()).unwrap() + util::str::str_offset(s, first.kwd_str()).unwrap() }; // Legacy (tap) onion key diff --git a/tor-netdoc/src/parse/tokenize.rs b/tor-netdoc/src/parse/tokenize.rs index 578331461..ae0b80920 100644 --- a/tor-netdoc/src/parse/tokenize.rs +++ b/tor-netdoc/src/parse/tokenize.rs @@ -433,7 +433,7 @@ impl<'a, K: Keyword> Item<'a, K> { /// /// Returns None if this item doesn't actually belong to the string. pub fn offset_in(&self, s: &str) -> Option { - crate::util::str_offset(s, self.kwd_str) + crate::util::str::str_offset(s, self.kwd_str) } /// Return the position of the n'th argument of this item. /// diff --git a/tor-netdoc/src/util.rs b/tor-netdoc/src/util.rs index f57a8f63d..ea3e91fdd 100644 --- a/tor-netdoc/src/util.rs +++ b/tor-netdoc/src/util.rs @@ -1,5 +1,7 @@ //! Misc helper functions and types for use in parsing network documents +pub(crate) mod str; + use std::iter::Peekable; /// An iterator adaptor that pauses when a given predicate is true. @@ -86,44 +88,6 @@ impl<'a, I: Iterator, F: FnMut(&I::Item) -> bool> Iterator for PauseAt<'a, I, F> } } -/// Return the position of one string slice within another. -/// -/// If `needle` is indeed part of `haystack`, returns some offset -/// `off`, such that `needle` is the same as -/// `&haystack[off..needle.len()]`. -/// -/// Returns None if `needle` is not a part of `haystack`. -/// -/// Remember, offsets are in bytes, not in characters. -/// -/// # Example -/// ```ignore -/// use tor_netdoc::util::str_offset; -/// let quote = "A rose is a rose is a rose."; // -- Gertrude Stein -/// assert_eq!("e[2..6], "rose"); -/// assert_eq!(str_offset(quote, "e[2..6]).unwrap(), 2); -/// assert_eq!("e[12..16], "rose"); -/// assert_eq!(str_offset(quote, "e[12..16]).unwrap(), 12); -/// assert_eq!("e[22..26], "rose"); -/// assert_eq!(str_offset(quote, "e[22..26]).unwrap(), 22); -/// -/// assert_eq!(str_offset(quote, "rose"), None); -/// -/// assert_eq!(str_offset("e[1..], "e[2..6]), Some(1)); -/// assert_eq!(str_offset("e[1..5], "e[2..6]), None); -/// ``` -pub fn str_offset(haystack: &str, needle: &str) -> Option { - let needle_start_u = needle.as_ptr() as usize; - let needle_end_u = needle_start_u + needle.len(); - let haystack_start_u = haystack.as_ptr() as usize; - let haystack_end_u = haystack_start_u + haystack.len(); - if haystack_start_u <= needle_start_u && needle_end_u <= haystack_end_u { - Some(needle_start_u - haystack_start_u) - } else { - None - } -} - #[cfg(test)] mod tests { @@ -174,21 +138,4 @@ mod tests { assert_eq!(iter.peek(), None); assert_eq!(iter.next(), None); } - - #[test] - fn test_str_offset() { - use super::str_offset; - let quote = "A rose is a rose is a rose."; // -- Gertrude Stein - assert_eq!("e[2..6], "rose"); - assert_eq!(str_offset(quote, "e[2..6]).unwrap(), 2); - assert_eq!("e[12..16], "rose"); - assert_eq!(str_offset(quote, "e[12..16]).unwrap(), 12); - assert_eq!("e[22..26], "rose"); - assert_eq!(str_offset(quote, "e[22..26]).unwrap(), 22); - - assert_eq!(str_offset(quote, "rose"), None); - - assert_eq!(str_offset("e[1..], "e[2..6]), Some(1)); - assert_eq!(str_offset("e[1..5], "e[2..6]), None); - } }