Skip to content

Commit

Permalink
move trim_whitespace() outside of CStr
Browse files Browse the repository at this point in the history
Signed-off-by: qidu <[email protected]>
  • Loading branch information
qidu committed Oct 15, 2022
1 parent 4f6559f commit 904b063
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
44 changes: 22 additions & 22 deletions rust/kernel/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,6 @@ impl CStr {
pub unsafe fn as_str_unchecked(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
}

#[inline]
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];
}
data
}
}

impl fmt::Display for CStr {
Expand Down Expand Up @@ -400,9 +381,9 @@ mod tests {

#[test]
fn test_trim_whitespace() {
assert_eq!(CStr::trim_whitespace(b"foo "), b"foo");
assert_eq!(CStr::trim_whitespace(b" foo"), b"foo");
assert_eq!(CStr::trim_whitespace(b" foo "), b"foo");
assert_eq!(trim_whitespace(b"foo "), b"foo");
assert_eq!(trim_whitespace(b" foo"), b"foo");
assert_eq!(trim_whitespace(b" foo "), b"foo");
}
}

Expand Down Expand Up @@ -621,3 +602,22 @@ impl Deref for CString {
macro_rules! fmt {
($($f:tt)*) => ( core::format_args!($($f)*) )
}

/// 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];
}
data
}
2 changes: 1 addition & 1 deletion rust/kernel/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
bindings,
error::code::*,
io_buffer::IoBufferWriter,
str::CStr,
str::*,
types,
user_ptr::{UserSlicePtr, UserSlicePtrWriter},
Result,
Expand Down

0 comments on commit 904b063

Please sign in to comment.