-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
lib.strings: add trim
#315411
lib.strings: add trim
#315411
Conversation
Should this be added to the top-level ( |
I'll try to review this a bit more closely soon, but yeah for now it should still be exported under |
Would nixpkgs be open to also having If so, would you want them in this PR or separately? |
What is the prevalence of In the sense of: "all these lines of code in Nixpkgs are mere applications of trim". |
Line 1060 in 4eb44db
toInt and friends all try to implement trim
Line 1174 in 4eb44db
Also, I'd want to trim option descriptions in some cases (especially for |
7561b86
to
98cb779
Compare
As suggested (#315411 (comment)) I've implemented a configurable @AndersonTorres does this achieve what you wanted? @eclairevoyant would you mind re-reviewing since the implementation has changed? |
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.
Just a minor comment, otherwise I think this is good!
`strings.trim` returns a copy of the string with all leading and trailing whitespace removed. `strings.trimWith` does the same thing, but calling code can decide whether to trim the start and/or end of the string.
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.
Looking good to me, nice work!
Small follow-up: #330034 |
Successfully created backport PR for |
strings.trim
returns a copy of the string with all leading and trailing whitespace removed.strings.trimWith
does the same thing, but calling code can decide whether to trim the start and/or end of the string.The obvious regex
[[:string:]]*(.*)[[:string:]]*
wouldn't work, because the(.*)
is greedy and matches trailing whitespace. Since ERE doesn't support lazy/reluctant matches (.*?
) introduced in perl-style regexes, we need to use a regex that fails to match blank/empty strings. This case is handled usingoptionalString
.Description of changes
trim
is a fairly core function included in most standard libraries. I've personally come across a couple scenarios where I would've liked to have been able to trim strings.I've added a few basic tests.
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Pinging recent contributors to lib.string: @infinisil @AndersonTorres @FireyFly @eclairevoyant @h7x4
Add a 👍 reaction to pull requests you find important.