Navigation Menu

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

strings: please add UnwriteByte to Builder #36038

Closed
jech opened this issue Dec 7, 2019 · 5 comments
Closed

strings: please add UnwriteByte to Builder #36038

jech opened this issue Dec 7, 2019 · 5 comments
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@jech
Copy link

jech commented Dec 7, 2019

Go version 1.13.

There does not seem to be a way to remove data written to a strings.Builder.

I'm currently doing the following:

func pathUrl(p []string) string {
	var b []byte
	for _, s := range p {
		t := url.PathEscape(s)
		b = append(b, t...)
		b = append(b, '/')
	}
	return string(b[0 : len(b)-1])
}

The last line removes the trailing slash from the accumulated buffer. I don't see an obvious way of doing the same with a strings.Builder. Perhaps it would be a good idea to add UnwriteByte, UnwriteRune and perhaps UnwriteString functions to strings.Builder?

@ianlancetaylor ianlancetaylor changed the title Please add UnwriteByte to strings.Builder. strings: please add UnwriteByte to Builder Dec 7, 2019
@ianlancetaylor
Copy link
Contributor

It seems to me easy enough to write

func pathUrl(p []string) string {
	var b []byte
	for i, s := range p {
		if i > 0 {
			b = append(b, '/')
		}
		t := url.PathEscape(s)
		b = append(b, t...)
	}
	return string(b)
}

I'm not convinced it's worth the complexity of adding Unwrite methods to strings.Builder.

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 7, 2019
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Dec 7, 2019
@josharian
Copy link
Contributor

If you are unwriting only the final byte, you could use strings.Builder and then slice away the final byte of the string.

@jech
Copy link
Author

jech commented Dec 8, 2019

@ianlancetaylor, I agree that it's a matter of taste. It becomes a little more messy when i is not readily available (when the components are not coming from a slice).

Feel free to close if you feel that this proposal is unlikely to gain consensus.

@renthraysk
Copy link

I'd personally deal with p[0] outside the loop, and range over p[1:], and never have to unwrite.

https://play.golang.org/p/LODpCtD3mjh

@ianlancetaylor
Copy link
Contributor

I think it's always going to be straightforward to not write data, and very few io.Writer implementations implement UnwriteByte. So I do think this is unlikely to gain consensus.

@golang golang locked and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants