Skip to content
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

Change trim_whitespace implementation to use while let #913

Open
wants to merge 3 commits into
base: rust
Choose a base branch
from

Conversation

Skgland
Copy link

@Skgland Skgland commented Oct 15, 2022

This depends on #911 and was originally proposed there in a comment #911 (comment)

The first commit adds two additional tests,
one for the empty string and the second for a string consisting only of spaces (maybe there should also be tests testing for other whitespace beside spaces?).

The second commit does the actual change of introducing while let.
Using while let allows us to combine the empty and whitespace character checks as well as the remainder calculation into one patter for each loop.
As a result of no longer uses slice indexing and as such not using the Index trait,
the function can now be made const, which is also done here.

Compiler Explorer shows no significant change in assembly output.

playground

@Skgland
Copy link
Author

Skgland commented Oct 15, 2022

Force Pushed to remove accidental formatting changes in the first of my commits

qidu and others added 3 commits October 19, 2022 22:04
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants