use std::ffi::OsStr;
pub(crate) fn starts_with(full: &OsStr, start: &OsStr) -> bool {
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
return full
.as_bytes()
.starts_with(start.as_bytes());
}
#[cfg(windows)]
{
use std::os::windows::ffi::OsStrExt;
let mut full_units = full.encode_wide();
for start_unit in start.encode_wide() {
if full_units.next() != Some(start_unit) {
return false;
}
}
true
}
}
pub(crate) fn split_on_dot<'a>(value: &'a OsStr) -> Vec<&'a OsStr> {
value
.as_encoded_bytes()
.split(|byte| *byte == b'.')
.map(|part| {
unsafe { OsStr::from_encoded_bytes_unchecked(part) }
})
.collect()
}
#[cfg(test)]
mod tests {
use std::ffi::OsStr;
use super::{split_on_dot, starts_with};
#[test]
fn starts_with_matches_prefix() {
assert!(starts_with(OsStr::new("build.local"), OsStr::new("build")));
}
#[test]
fn starts_with_rejects_non_prefix() {
assert!(!starts_with(OsStr::new("build.local"), OsStr::new("local")));
}
#[test]
fn split_on_dot_splits_each_segment() {
assert_eq!(
split_on_dot(OsStr::new("build.docker.local")),
vec![OsStr::new("build"), OsStr::new("docker"), OsStr::new("local")]
);
}
#[test]
fn split_on_dot_preserves_empty_segments() {
assert_eq!(
split_on_dot(OsStr::new("build..local")),
vec![OsStr::new("build"), OsStr::new(""), OsStr::new("local")]
);
}
}