Skip to content

Commit

Permalink
[#96] Simplify algorithms by removing loops
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jan 30, 2024
1 parent e0f7fbb commit 215f79b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 67 deletions.
4 changes: 3 additions & 1 deletion iceoryx2-bb/container/src/semantic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub trait SemanticString<const CAPACITY: usize>:
///
/// # Safety
///
/// * The user must ensure that the bytes contain only valid characters.
/// * The slice must contain only valid characters.
/// * The slice must have a length that is less or equal CAPACITY
///
unsafe fn new_unchecked(bytes: &[u8]) -> Self;

Expand Down Expand Up @@ -169,6 +170,7 @@ pub trait SemanticString<const CAPACITY: usize>:
///
/// * The user must ensure that the bytes contain only valid characters.
/// * The user must ensure that the result, after the bytes were added, is valid.
/// * The slice must have a length that is less or equal CAPACITY
///
unsafe fn insert_bytes_unchecked(&mut self, idx: usize, bytes: &[u8]);

Expand Down
42 changes: 17 additions & 25 deletions iceoryx2-bb/system-types/src/file_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,35 @@ impl FilePath {
Ok(new_self)
}

fn rfind(&self, byte: u8) -> Option<usize> {
let bytes = self.as_bytes();
for i in 0..self.len() {
let pos = self.len() - 1 - i;
if bytes[pos] == byte {
return Some(pos);
}
}

None
}

/// Returns the last file part ([`FileName`]) of the path.
pub fn file_name(&self) -> FileName {
let file_name = self
.as_bytes()
.rsplitn(2, |c| *c == PATH_SEPARATOR)
.next()
.unwrap();
// SAFETY
// * the file path ensures that is a valid path to a file, therefore the last part
// must be a valid FileName
unsafe {
FileName::new_unchecked(match self.rfind(PATH_SEPARATOR) {
None => self.as_bytes(),
Some(pos) => self.as_bytes().get(pos + 1..self.len()).unwrap(),
})
}
unsafe { FileName::new_unchecked(file_name) }
}

/// 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,
Some(_) => &[PATH_SEPARATOR],
None => &[],
};
// SAFETY
// * the file path ensures that is a valid path to a file, therefore the first part
// must be a valid path
unsafe {
Path::new_unchecked(match self.rfind(PATH_SEPARATOR) {
None => core::slice::from_raw_parts(self.as_ptr(), 0),
Some(0) => self.as_bytes().get(0..1).unwrap(),
Some(pos) => self.as_bytes().get(0..pos).unwrap(),
})
}
unsafe { Path::new_unchecked(path) }
}
}

Expand Down
47 changes: 6 additions & 41 deletions iceoryx2-bb/system-types/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,50 +127,15 @@ impl Path {
}

pub fn new_normalized(value: &[u8]) -> Result<Path, SemanticStringError> {
let mut raw_path = [0u8; PATH_LENGTH];

let mut previous_char_is_path_separator = false;
let mut n = 0;
for i in 0..value.len() {
if i + 1 == value.len() && value[i] == PATH_SEPARATOR {
break;
}

if !(previous_char_is_path_separator && value[i] == PATH_SEPARATOR) {
raw_path[n] = value[i];
n += 1;
}

previous_char_is_path_separator = value[i] == PATH_SEPARATOR
}

Path::new(&raw_path[0..n])
Ok(Path::new(value)?.normalize())
}

pub fn entries(&self) -> Vec<FixedSizeByteString<FILENAME_LENGTH>> {
let mut entry_vec = vec![];
let mut start_pos = 0;
let raw_path = self.as_bytes();
for i in 0..raw_path.len() {
if raw_path[i] == PATH_SEPARATOR {
if i - start_pos == 0 {
start_pos = i + 1;
continue;
}

entry_vec
.push(unsafe { FixedSizeByteString::new_unchecked(&raw_path[start_pos..i]) });
start_pos = i + 1;
}
}

if start_pos < raw_path.len() {
entry_vec.push(unsafe {
FixedSizeByteString::new_unchecked(&raw_path[start_pos..raw_path.len()])
});
}

entry_vec
self.as_bytes()
.split(|c| *c == PATH_SEPARATOR)
.filter(|entry| entry.len() > 0)
.map(|entry| unsafe { FixedSizeByteString::new_unchecked(entry) })
.collect()
}
}

Expand Down

0 comments on commit 215f79b

Please sign in to comment.