Doug Hoyte on Nostr: Is there an easy way to truncate to a max byte-length using the Go standard library? ...
Is there an easy way to truncate to a max byte-length using the Go standard library? This is the best answer I could find (after only 1 minute of searching though):
https://stackoverflow.com/questions/46415894/golang-truncate-strings-with-special-characters-without-corrupting-data/76502408#76502408
Note that as the answer says, this doesn't understand grapheme clusters, meaning that café can become cafe (depending on normalisation form). Also it involves another pass over the data which, if already known to be UTF-8, is redundant.
This 3rd party package looks to be the rough equivalent of my Perl module: https://github.com/rivo/uniseg
https://stackoverflow.com/questions/46415894/golang-truncate-strings-with-special-characters-without-corrupting-data/76502408#76502408
Note that as the answer says, this doesn't understand grapheme clusters, meaning that café can become cafe (depending on normalisation form). Also it involves another pass over the data which, if already known to be UTF-8, is redundant.
This 3rd party package looks to be the rough equivalent of my Perl module: https://github.com/rivo/uniseg