Skip to content

Commit

Permalink
replace trim_whitespace implementation
Browse files Browse the repository at this point in the history
originally proposed in a comment at Rust-for-Linux#911 (comment)

- using while let allows us to combine all tests and
  the reminder construction  into one pattern
- without slice indexing and as such  the use of the `Index` trait
  we can now also make the function `const`
- a quick comparison on [Compiler Explorer](https://godbolt.org/z/Ex5dT8ffr)
  shows no changes in the resulting instructions,
  at least for the checked constellation

Signed-off-by: Bennet Bleßmann <[email protected]>
  • Loading branch information
Skgland committed Oct 15, 2022
1 parent 99e5c2f commit 68df9bd
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions rust/kernel/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,12 @@ macro_rules! fmt {
}

/// Trim the whitspaces
pub fn trim_whitespace(mut data: &[u8]) -> &[u8] {
while !data.is_empty()
&& (data[0] == b' '
|| data[0] == b'\t'
|| data[0] == b'\n')
{
data = &data[1..];
}
while !data.is_empty()
&& (data[data.len() - 1] == b' '
|| data[data.len() - 1] == b'\t'
|| data[data.len() - 1] == b'\n')
{
data = &data[..data.len() - 1];
pub const fn trim_whitespace(mut data: &[u8]) -> &[u8] {
while let [b' ' | b'\t' | b'\n', remainder @ ..] = data {
data = remainder;
}
while let [remainder @ .., b' ' | b'\t' | b'\n'] = data {
data = remainder;
}
data
}

0 comments on commit 68df9bd

Please sign in to comment.