-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: strings: add First/Last function #63197
Comments
Left , Right ? |
Name of the function is not important, it could be Prefix(), Suffix(), or LeftMost(), RightMost() |
For semi-related prior discussion, see #53510 |
@dsnet I don't think my proposal has same intention as #53510. The key difference is, in #53510, @ianlancetaylor asked:
The answer is "yes, in the same way that s[0] panics for an empty slice.". But for this proposal, the key point is that I want to get the first (last) N bytes, if there is that much, or more, if the string is shorter, just give me all -- do NOT panic. This has exactly the same rational as "strings.TrimPrefix()". So, comparing to #53510, this is not merely a syntactic sugar, it is a shorthand for, what I shink, a common logic. |
Why bytes? Bytes are efficient as index positions into strings, usually determined via |
I'm afraid @gophun's objection is fatal. Anything you do (count bytes, count runes, count grapheme clusters) will be appropriate for some users and inappropriate for others. It does occur to me though that for counting bytes, you can now use |
I encountered many times the following scenario:
This simple function try to get at most N-bytes from a string. Likewise, for at most N-bytes "tail" from a string:
These functions will many some programs easier to read, like the TrimPrefix/TrimSuffix functions.
The text was updated successfully, but these errors were encountered: