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: bytes, strings: add Truncate #56885

Closed
icholy opened this issue Nov 21, 2022 · 8 comments
Closed

proposal: bytes, strings: add Truncate #56885

icholy opened this issue Nov 21, 2022 · 8 comments

Comments

@icholy
Copy link

icholy commented Nov 21, 2022

I find myself implementing this function pretty often and I thought it would be decent addition to the standard library.

// Truncate returns the first n runes of s.
func Truncate(s string, n int) string {
	if len(s) <= n {
		return s
	}
	for i := range s {
		if n == 0 {
			return s[:i]
		}
		n--
	}
	return s
}
@gopherbot gopherbot added this to the Proposal milestone Nov 21, 2022
@bcmills
Copy link
Contributor

bcmills commented Nov 21, 2022

Why runes?

  • If you're dealing with ASCII, truncating to bytes is trivial.

  • If you're trying to limit columns of output to display to a user, you probably need to take into account character widths and also truncate to a grapheme cluster boundary, not just a rune boundary.

So this seems like too much complexity for the ASCII case, and not nearly enough for the full Unicode case... what problems does that leave for it to solve?

@icholy
Copy link
Author

icholy commented Nov 22, 2022

That is a valid criticism, I'm mainly using it for ASCII text.

@rsc
Copy link
Contributor

rsc commented Nov 30, 2022

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

@rogpeppe
Copy link
Contributor

rogpeppe commented Dec 2, 2022

If you're trying to limit columns of output to display to a user, you probably need to take into account character widths and also truncate to a grapheme cluster boundary, not just a rune boundary.

FWIW when I want this functionality, it's usually because I want to limit the overall size in memory of something without producing malformed UTF-8. It's true that I might produce a malformed grapheme cluster, but I'd still submit that it's better to do that than splitting at an arbitrary byte.

I'm not even sure that it's possible to split on grapheme clusters without going outside the standard library

I have a strong recollection of having this discussion before, but I can't for the life of me find the discussion (ISTR that @rsc was involved).

@icholy
Copy link
Author

icholy commented Dec 3, 2022

@rogpeppe I believe you're thinking about the discussion here #53120

@rsc
Copy link
Contributor

rsc commented Dec 7, 2022

Similar to the discussion in #53120, this API seems to promise something but really doesn't promise much of anything. It is easy enough to write in a third-party library. It doesn't seem like we should have it here.

@rsc
Copy link
Contributor

rsc commented Dec 14, 2022

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

@rsc
Copy link
Contributor

rsc commented Dec 21, 2022

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

@rsc rsc closed this as completed Dec 21, 2022
@golang golang locked and limited conversation to collaborators Dec 21, 2023
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

5 participants