Skip to content

proposal: bytes, strings: add Truncate #56885

Closed

Description

@icholy

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
}

Activity

  1. added this to the Proposal milestone on Nov 21, 2022
  2. bcmills commented on Nov 21, 2022

    @bcmills
    Contributor

    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?

  3. icholy commented on Nov 22, 2022

    @icholy
    ContributorAuthor

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

  4. moved this to Incoming in Proposalson Nov 22, 2022
  5. moved this from Incoming to Active in Proposalson Nov 30, 2022
  6. rsc commented on Nov 30, 2022

    @rsc
    Contributor

    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

  7. rogpeppe commented on Dec 2, 2022

    @rogpeppe
    Contributor

    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).

  8. icholy commented on Dec 3, 2022

    @icholy
    ContributorAuthor

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

  9. rsc commented on Dec 7, 2022

    @rsc
    Contributor

    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.

  10. rsc commented on Dec 14, 2022

    @rsc
    Contributor

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

  11. moved this from Active to Likely Decline in Proposalson Dec 14, 2022
  12. rsc commented on Dec 21, 2022

    @rsc
    Contributor

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

  13. moved this from Likely Decline to Declined in Proposalson Dec 21, 2022
  14. locked and limited conversation to collaborators on Dec 21, 2023
  15. removed this from Proposalson Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions