Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions core/src/abs_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ impl AbsPath {
// The string doesn't contain the path separator.
return Err(AbsPathNotAbsoluteError);
};
if offset != 0 {
// The string doesn't start with the path separator.

if offset != (if cfg!(windows) { 2 } else { 0 }) {
return Err(AbsPathNotAbsoluteError);
}

let separator_len = MAIN_SEPARATOR_STR.len();
let mut valid_up_to = separator_len;
let mut valid_up_to = separator_len + (if cfg!(windows) { 2 } else { 0 });

while let Some(offset) = separator_offsets.next() {
let component = r#const::str_slice(str, valid_up_to..offset);
Expand Down Expand Up @@ -306,15 +306,31 @@ impl<'a> NormalizeState<'a> {

#[inline]
fn new(original_str: &'a str) -> Result<Self, NormalizeError> {
if original_str.starts_with(MAIN_SEPARATOR_STR) {
let cursor = MAIN_SEPARATOR_STR.len();
Ok(Self {
cursor,
normalized_path: NormalizedPath::Slice(0..cursor),
original_str,
})
} else {
Err(NormalizeError::NotAbsolute)
let cursor = MAIN_SEPARATOR_STR.len() + if cfg!(windows) { 2 } else { 0 };
#[cfg(windows)]
{
if matches!(original_str.chars().next(), Some('a'..='z' | 'A'..='Z')) && original_str.get(1..cursor) == Some(":\\") {
Ok(Self {
cursor,
normalized_path: NormalizedPath::Slice(0..cursor),
original_str,
})
} else {
Err(NormalizeError::NotAbsolute)
}
}

#[cfg(not(windows))]
{
if original_str.starts_with(MAIN_SEPARATOR_STR) {
Ok(Self {
cursor,
normalized_path: NormalizedPath::Slice(0..cursor),
original_str,
})
} else {
Err(NormalizeError::NotAbsolute)
}
}
}

Expand Down Expand Up @@ -656,3 +672,13 @@ mod serde_impls {
}
}
}

#[cfg(test)]
mod tests {
use super::AbsPath;
#[cfg(windows)]
#[test]
fn test_windows_abs_path() {
assert!(AbsPath::from_str("C:\\Users\\User1").is_ok());
}
}