Skip to content

Commit

Permalink
[#96] Fix is_absolute for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jan 30, 2024
1 parent 215f79b commit 4cef9f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
9 changes: 2 additions & 7 deletions iceoryx2-bb/system-types/src/file_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,8 @@ impl FilePath {

/// Returns the [`Path`] part of the [`FilePath`].
pub fn path(&self) -> Path {
let path = match self
.as_bytes()
.rsplitn(2, |c| *c == PATH_SEPARATOR)
.skip(1)
.next()
{
Some(p) if p.len() > 0 => p,
let path = match self.as_bytes().rsplitn(2, |c| *c == PATH_SEPARATOR).nth(1) {
Some(p) if !p.is_empty() => p,
Some(_) => &[PATH_SEPARATOR],
None => &[],
};
Expand Down
22 changes: 18 additions & 4 deletions iceoryx2-bb/system-types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,25 @@ impl Path {
}

pub fn is_absolute(&self) -> bool {
if self.as_bytes().is_empty() {
return false;
#[cfg(not(target_os = "windows"))]
{
if self.as_bytes().is_empty() {
return false;
}

self.as_bytes()[0] == PATH_SEPARATOR
}
#[cfg(target_os = "windows")]
{
if self.len() < 3 {
return false;
}

self.as_bytes()[0] == PATH_SEPARATOR
let has_drive_letter = (b'a' <= self.as_bytes()[0] && self.as_bytes()[0] <= b'z')
|| (b'A' <= self.as_bytes()[0] && self.as_bytes()[0] <= 'Z');

has_drive_letter && self.as_bytes()[1] == b':' && (self.as_bytes()[2] == PATH_SEPARATOR)
}
}

pub fn new_root_path() -> Path {
Expand All @@ -133,7 +147,7 @@ impl Path {
pub fn entries(&self) -> Vec<FixedSizeByteString<FILENAME_LENGTH>> {
self.as_bytes()
.split(|c| *c == PATH_SEPARATOR)
.filter(|entry| entry.len() > 0)
.filter(|entry| !entry.is_empty())
.map(|entry| unsafe { FixedSizeByteString::new_unchecked(entry) })
.collect()
}
Expand Down
18 changes: 18 additions & 0 deletions iceoryx2-bb/system-types/tests/path_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ mod windows {
.unwrap();
assert_that!(sut, eq b"fuu\\blaaaha\\blub.ma");
}

#[test]
fn path_is_absolute_works() {
let sut = Path::new(b"D:\\bla").unwrap();
assert_that!(sut.is_absolute(), eq true);

let sut = Path::new(b"k:\\fuu\\bar").unwrap();
assert_that!(sut.is_absolute(), eq true);

let sut = Path::new(b"0:\\a\\b").unwrap();
assert_that!(sut.is_absolute(), eq false);

let sut = Path::new(b"").unwrap();
assert_that!(sut.is_absolute(), eq false);

let sut = Path::new(b"what/ever/").unwrap();
assert_that!(sut.is_absolute(), eq false);
}
}

#[cfg(not(target_os = "windows"))]
Expand Down

0 comments on commit 4cef9f5

Please sign in to comment.