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

proposal: strings: add First/Last function #63197

Open
xrfang opened this issue Sep 25, 2023 · 6 comments
Open

proposal: strings: add First/Last function #63197

xrfang opened this issue Sep 25, 2023 · 6 comments
Labels
Milestone

Comments

@xrfang
Copy link

xrfang commented Sep 25, 2023

I encountered many times the following scenario:

func First(s string, n int) string {
    if len(s) <= n {
        return s
    }
    return s[:n]
}

This simple function try to get at most N-bytes from a string. Likewise, for at most N-bytes "tail" from a string:

func Last(s string, n int) string {
    if len(s) <= n {
        return s
    }
    return s[len(s)-n:]
}

These functions will many some programs easier to read, like the TrimPrefix/TrimSuffix functions.

@gopherbot gopherbot added this to the Proposal milestone Sep 25, 2023
@it512
Copy link

it512 commented Sep 25, 2023

Left , Right ?

@xrfang
Copy link
Author

xrfang commented Sep 25, 2023

Name of the function is not important, it could be Prefix(), Suffix(), or LeftMost(), RightMost()

@dsnet
Copy link
Member

dsnet commented Sep 25, 2023

For semi-related prior discussion, see #53510

@xrfang
Copy link
Author

xrfang commented Sep 26, 2023

@dsnet I don't think my proposal has same intention as #53510. The key difference is, in #53510, @ianlancetaylor asked:

Should First and Last panic for an empty slice?

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.

@gophun
Copy link

gophun commented Sep 26, 2023

I want to get the first (last) N bytes

Why bytes? Bytes are efficient as index positions into strings, usually determined via Index* functions, but they are not suitable for counting. The concept of 'count' doesn't have an obvious meaning in Unicode strings because there are multiple layers to consider.

@earthboundkid
Copy link
Contributor

earthboundkid commented Sep 26, 2023

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 min to make a one liner: s := mystr[:min(N, len(mystr))].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants