-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rust: kernel: move the trim_whitespace() from sysctl to str #911
base: rust
Are you sure you want to change the base?
Conversation
Moving it to |
No, it's not necessary inside CStr. I could move it outside. |
This might be out-of-scope for this PR, which just moves code as is, #[inline]
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
}
#[test]
fn test_trim_whitespace() {
assert_eq!(trim_whitespace(b"" ), b"");
assert_eq!(trim_whitespace(b" "), b"");
assert_eq!(trim_whitespace(b"foo "), b"foo");
assert_eq!(trim_whitespace(b" foo"), b"foo");
assert_eq!(trim_whitespace(b" foo "), b"foo");
} |
Thanks for your advices!
|
@Skgland Yeah, I like the @qidu Please rebase your commits so that one commit does the move as-is and another changes the Also please read And, of course, you will need to add docs if you plan to make it |
By the way, I would give credit to @Skgland for the second part, or even better, he could submit it himself after you perform the move. |
Yeah, I could do that as well. Though I am fine either way.
Ah, I was unable to find the diff view and compared them manually. |
Do I understand
If that the case than that would be:
|
Almost -- that tag should be there, as well as the From: Bennet Bleßmann <[email protected]>
<changelog>
Signed-off-by: Bennet Bleßmann <[email protected]>
Co-developed-by: @qidu real name <email>
Signed-off-by: @qidu real name <email> But if you are happy sending it yourself, then you will both learn how to prepare commits for the kernel :) |
904b063
to
eef2ff5
Compare
We have a patch that'll make |
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]>
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]>
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase the branch to create proper commits -- I would suggest checking other PRs, for instance https://github.com/Rust-for-Linux/linux/pull/910/commits.
Thanks!
Later on (in another PR, likely after #913) we may want to consider trimming |
Please rebase with |
For there are already other common string types in str.rs, and trimming white spaces is a common need for string. So this could help further refactoring or re-difining these string functions. Following changes are made here: - move the function from sysctl.rs into str.rs - make the function public - move the tests into an example Signed-off-by: Qidu Teric <[email protected]>
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]>
Should we add tests for \n and \t too? assert_eq!(trim_whitespace(b"\nfoo\t "), b"foo"); and test for non stripping assert_eq!(trim_whitespace(b"f o\no\tbar "), b"f o\no\tbar"); |
As there is already a str, there put the trim_whitespace() into str.rs would be better.