You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
proposal: strings: provide ContainsOnly function #46385
strings package has ContainsAny. I have seen many package implement thier ContainsOnly. Although it takes just a few line of code, it would be nice to have it in strings package.
This proposal lacks detail. What is a ContainsOnly? Can you share links to those "many packages" that implement their own? How does it compare to ContainsAny?
I think this could be written as strings.Trim(s, chars) == "".
Does this really come up often enough to deserve its own function? Can you point to some existing code that would be simpler if this function were available? Thanks.
Reacted by Oleg Kovalov, Ingo Oeser, James Lucktaylor and andig
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
The downside of using ContainsOnly for testing a character set is that the character set is very large. Passing "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789" and making the function figure out how to do that seems much worse (and less efficient) than just writing 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9'. It's also much harder to read. Did you notice the missing I in the string?
It's even worse if Unicode is involved.
It doesn't seem like we have a compelling use case here.
Activity
mvdan commentedon May 26, 2021
This proposal lacks detail. What is a ContainsOnly? Can you share links to those "many packages" that implement their own? How does it compare to ContainsAny?
[-]Proposal: strings: provide ContainsOnly function[/-][+]proposal: strings: provide ContainsOnly function[/+]ianlancetaylor commentedon May 26, 2021
I think this could be written as
strings.Trim(s, chars) == "".Does this really come up often enough to deserve its own function? Can you point to some existing code that would be simpler if this function were available? Thanks.
rsc commentedon Jun 9, 2021
This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group
earthboundkid commentedon Jun 9, 2021
This comes up, e.g., to test that all characters in an email address are valid, but I think that doing an O(n m) loop is wrong when you can use a lookup map instead.
icholy commentedon Jun 9, 2021
@carlmjohnson for small
charstrings, the O(n m) version might be faster.rsc commentedon Jun 16, 2021
The downside of using ContainsOnly for testing a character set is that the character set is very large. Passing "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789" and making the function figure out how to do that seems much worse (and less efficient) than just writing
'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9'. It's also much harder to read. Did you notice the missing I in the string?It's even worse if Unicode is involved.
It doesn't seem like we have a compelling use case here.
rsc commentedon Jul 14, 2021
Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group
rsc commentedon Jul 21, 2021
No change in consensus, so declined.
— rsc for the proposal review group