From 1a6aedce72f5913b8e2d7e8c600bb43a92377669 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 5 Oct 2019 13:53:55 +0200 Subject: Move functions to util module Signed-off-by: Matthias Beyer --- src/util.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/util.rs (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..5376fa6 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,45 @@ +pub fn is_boundary(line: &str, ix: Option) -> bool { + ix.and_then(|v| line.chars().nth(v)) + .map(|c| c.is_whitespace() || c == '"' || c == '(' || c == ')' || c == '<' || c == '>') + .unwrap_or(true) +} + +pub fn find_from(line: &str, ix_start: usize, key: &str) -> Option { + line[ix_start..].find(key).map(|v| ix_start + v) +} + +pub fn find_from_u8(line: &[u8], ix_start: usize, key: &[u8]) -> Option { + assert!(!key.is_empty()); + assert!(ix_start < line.len()); + if line.len() < key.len() { + return None; + } + let ix_end = line.len() - key.len(); + if ix_start <= ix_end { + for i in ix_start..ix_end { + if line[i] == key[0] { + let mut success = true; + for j in 1..key.len() { + if line[i + j] != key[j] { + success = false; + break; + } + } + if success { + return Some(i); + } + } + } + } + None +} + +#[test] +fn test_find_from_u8() { + assert_eq!(find_from_u8(b"hello world", 0, b"hell"), Some(0)); + assert_eq!(find_from_u8(b"hello world", 0, b"o"), Some(4)); + assert_eq!(find_from_u8(b"hello world", 4, b"o"), Some(4)); + assert_eq!(find_from_u8(b"hello world", 5, b"o"), Some(7)); + assert_eq!(find_from_u8(b"hello world", 8, b"o"), None); + assert_eq!(find_from_u8(b"hello world", 10, b"d"), None); +} -- cgit v1.2.3