From cdbca5c53b2d2f0c3fd1189a2def4cff3c58ea4d Mon Sep 17 00:00:00 2001 From: Niklas Mohrin Date: Fri, 21 May 2021 12:36:32 +0200 Subject: [PATCH] Add `FindFrom` extension trait to `str` to start searching at a byte offset. This also moves the contents of `dedup.rs` into a shared module `extensions`. --- src/{dedup.rs => extensions.rs} | 13 +++++++++++++ src/main.rs | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) rename src/{dedup.rs => extensions.rs} (61%) diff --git a/src/dedup.rs b/src/extensions.rs similarity index 61% rename from src/dedup.rs rename to src/extensions.rs index b06fe0c..3aebfa2 100644 --- a/src/dedup.rs +++ b/src/extensions.rs @@ -18,3 +18,16 @@ impl Dedup for Vec { } } } + +/// Like `str::find`, but starts searching at `start`. +pub(crate) trait FindFrom { + fn find_from(&self, needle: &Self, start: usize) -> Option; +} + +impl FindFrom for str { + fn find_from(&self, needle: &Self, start: usize) -> Option { + self.get(start..) + .and_then(|s| s.find(needle)) + .map(|i| i + start) + } +} diff --git a/src/main.rs b/src/main.rs index 693b793..0d68b06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,16 +31,16 @@ use serde_derive::Deserialize; mod cache; mod config; -mod dedup; mod error; +pub mod extensions; mod formatter; mod tokenizer; mod types; use crate::cache::{Cache, PageLookupResult}; use crate::config::{get_config_dir, get_config_path, make_default_config, Config, MAX_CACHE_AGE}; -use crate::dedup::Dedup; use crate::error::TealdeerError::ConfigError; +use crate::extensions::Dedup; use crate::formatter::print_lines; use crate::tokenizer::Tokenizer; use crate::types::{ColorOptions, OsType};