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: missing mirror functions #46514

Closed
colin-sitehost opened this issue Jun 2, 2021 · 7 comments
Closed

proposal: strings: missing mirror functions #46514

colin-sitehost opened this issue Jun 2, 2021 · 7 comments

Comments

@colin-sitehost
Copy link

background

there does not appear to be a turnkey way to do a SplitN of a string starting at the tail and proceeding toward the head. similarly, the following do not appear to exist:

  • SplitAfter, but before (put the separator on the next element: []string{"f" ",o" ",o"})
  • SplitAfterN, but before
  • Replace but start from the tail

proposal

add the following symbols to the strings package:

func SplitLastN(s, sep string, n int) []string
func SplitBefore(s, sep string) []string
func SplitBeforeN(s, sep string, n int) []string
func ReplaceLast(s, sep string, n int) []string

alternatives

we could say that these are weird and esoteric enough to not justify symbols for, but at least that decision is documented. there are workarounds to get this behaviour if you really need it:

rest, last := s[strings.LastIndex(s, ","):] // SplitLast(s, ",", 1)

I am very flexible on the names; so LastSplitN, ReplaceReverseN, or anything else is fine; I am just interested in the functionality.

@gopherbot gopherbot added this to the Proposal milestone Jun 2, 2021
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jun 2, 2021
@ianlancetaylor
Copy link
Contributor

Can you point to some examples of where these would be used in existing code? Thanks.

@rsc
Copy link
Contributor

rsc commented Jun 2, 2021

Note that if we add CutLast as in #46336 then this would probably serve the need.

@colin-sitehost
Copy link
Author

Note that if we add CutLast as in #46336 then this would probably serve the need.

this would serve the needs for SplitLast(s, sep, 1), but that is already just syntax sugar for s[strings.LastIndex(s, ","):], though the ok is a nice add. needing to chain or loop them together seems like a bodge.

Can you point to some examples of where these would be used in existing code? Thanks.

I can explain my use case, but I have not combed through the examples corpus to see if this would fit nicely into some existing logic, and this seems quite string parse(r/ing) specific.

for SplitLastN, we are tail first parsing some structured data, and want to split of the last N segments. there may be more separators in the head, so there is no way to index from there. after, we process the head separately.

@rsc
Copy link
Contributor

rsc commented Nov 10, 2021

These seem rare enough to have in other libraries to start.

@rsc
Copy link
Contributor

rsc commented Nov 10, 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

@rsc rsc moved this from Incoming to Active in Proposals (old) Nov 10, 2021
@rsc
Copy link
Contributor

rsc commented Dec 1, 2021

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Decline in Proposals (old) Dec 1, 2021
@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Dec 8, 2021
@rsc
Copy link
Contributor

rsc commented Dec 8, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Dec 8, 2021
@golang golang locked and limited conversation to collaborators Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

4 participants