Skip to content

Commit

Permalink
feat: starting to work on memchr
Browse files Browse the repository at this point in the history
  • Loading branch information
matzxrr committed Feb 27, 2024
1 parent db64766 commit 96537a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions dotme-path/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 11 additions & 0 deletions dotme-path/src/interpolate_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod error;
#[cfg(test)]
mod tests;

use memchr;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
Expand All @@ -27,5 +28,15 @@ pub fn interpoate_env(path: &Path) -> Result<PathBuf, Error> {

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)
}
4 changes: 2 additions & 2 deletions dotme-path/src/interpolate_env/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 96537a2

Please sign in to comment.