From 96537a202fb7ed9f38a2d89fa647f45a5886cc88 Mon Sep 17 00:00:00 2001 From: matzxrr Date: Tue, 27 Feb 2024 00:38:44 -0700 Subject: [PATCH] feat: starting to work on memchr --- dotme-path/Cargo.toml | 1 + dotme-path/src/interpolate_env/mod.rs | 11 +++++++++++ dotme-path/src/interpolate_env/tests.rs | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/dotme-path/Cargo.toml b/dotme-path/Cargo.toml index 3ed2770..5df53f8 100644 --- a/dotme-path/Cargo.toml +++ b/dotme-path/Cargo.toml @@ -9,3 +9,4 @@ documentation.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +memchr = "2.7.1" diff --git a/dotme-path/src/interpolate_env/mod.rs b/dotme-path/src/interpolate_env/mod.rs index 32241a1..29cbc52 100644 --- a/dotme-path/src/interpolate_env/mod.rs +++ b/dotme-path/src/interpolate_env/mod.rs @@ -3,6 +3,7 @@ mod error; #[cfg(test)] mod tests; +use memchr; use std::{ ffi::OsStr, path::{Path, PathBuf}, @@ -27,5 +28,15 @@ pub fn interpoate_env(path: &Path) -> Result { fn interpolate_part(part: &OsStr) -> Result<&[u8], Error> { let bytes = part.as_encoded_bytes(); + let range = 0..bytes.len(); + let mut sliding_pointer = range.start; + while sliding_pointer < range.end { + let env_start_pos = match memchr::memchr(b'$', &bytes[sliding_pointer..]) { + Some(start_pos) => sliding_pointer + start_pos, + None => break, + }; + println!("env variable starting at position {}", env_start_pos); + sliding_pointer += 1; + } Ok(bytes) } diff --git a/dotme-path/src/interpolate_env/tests.rs b/dotme-path/src/interpolate_env/tests.rs index 00cab4f..ebc3574 100644 --- a/dotme-path/src/interpolate_env/tests.rs +++ b/dotme-path/src/interpolate_env/tests.rs @@ -2,8 +2,8 @@ use super::*; #[test] fn it_should_parse_basic_env() { - let input = Path::new("$CARGO_PKG_NAME"); + let input = Path::new("someting/test/$CARGO_PKG_NAME"); let result = interpoate_env(input).expect("Should parse env variable"); - let expected = PathBuf::from("dotme-path"); + let expected = PathBuf::from("something/test/dotme-path"); assert_eq!(expected, result); }