Skip to content

proposal: strings: add ReplaceBetween function #45003

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

Closed
pjebs opened this issue Mar 14, 2021 · 12 comments
Closed

proposal: strings: add ReplaceBetween function #45003

pjebs opened this issue Mar 14, 2021 · 12 comments

Comments

@pjebs
Copy link
Contributor

pjebs commented Mar 14, 2021

ReplaceBetween(s, start, end, replace string, n int) string

ReplaceBetween will find all (non-overlapping) substrings beginning with start (token) and ending with end and replace all characters in-between (inclusive of start and end) with replace string.

@mdlayher
Copy link
Member

Why does this belong in the standard library? See https://golang.org/doc/faq#x_in_std.

@mdlayher mdlayher changed the title strings: ReplaceBetween proposal: strings: add ReplaceBetween function Mar 14, 2021
@gopherbot gopherbot added this to the Proposal milestone Mar 14, 2021
@pjebs
Copy link
Contributor Author

pjebs commented Mar 14, 2021

The criteria is super vague and it seems to really depend on what side of the bed 3 key Go people wake up on in the morning, so I don't know how to answer it.

  1. It's useful.
  2. And it's not as simple to implement properly for the average programmer, so code in the wild is likely to be subpar/unreliable even if people think they are well implemented.
  3. Implementing it well would make use of some currently unexported functions in the strings package already used in the implementation of similar exported functions.
  4. There are already some functions in the strings package that you can make the same argument for, but were included.

@ianlancetaylor
Copy link
Contributor

A key argument for something like this is not "it's useful"--everything that is proposed is useful, nobody proposes anything useless--but rather "here are several examples of existing real code that would benefit from this." Thanks.

@DeedleFake
Copy link

Why can this not just be done with regular expressions?

@pjebs
Copy link
Contributor Author

pjebs commented Mar 15, 2021

regex:

  • too slow
  • too powerful
  • harder to setup and use for quick small things
  • and less accessible to people - especially newbies.

@DeedleFake
Copy link

I think regular expressions makes way more sense as a general case for this. If that's not fast enough for your specific use case, implement it manually. This seems extremely specific for something to be included in the standard library.

@pjebs
Copy link
Contributor Author

pjebs commented Mar 15, 2021

In terms of existing examples, quickly and efficiently converting all html tags come to mind:

<i>some-text</i> => <strong>some-other-text</strong>

My actual use-case is merely to remove all content between a start and different end token.

This proposal is more general to be more useful.

@atdiar

This comment has been minimized.

@atdiar
Copy link

atdiar commented Mar 19, 2021

Actually I think I misunderstood.

The OP's request should be easily doable using just strings.Replace.

This does not even really fit my use-case for string interpolation.

@rsc
Copy link
Contributor

rsc commented Aug 18, 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
Copy link
Contributor

rsc commented Aug 25, 2021

@pjebs for an example of how to justify a new routine, see #40135.
If there are no examples of this functionality needed in the standard library or some other large body of code, that's a bad sign.
In the absence of evidence like that, this is headed for a likely decline.

@rsc
Copy link
Contributor

rsc commented Sep 1, 2021

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

@rsc rsc closed this as completed Sep 8, 2021
@rsc rsc moved this to Declined in Proposals Aug 10, 2022
@rsc rsc added this to Proposals Aug 10, 2022
@golang golang locked and limited conversation to collaborators Sep 8, 2022
@rsc rsc removed this from Proposals Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants