On Sun, Oct 22, 2017, at 09:29, Juliusz Chroboczek wrote:
> I'm probably missing something obvious, but I've looked through the
> standard library to no avail.  How do I sanitise a []byte to make sure
> it's a UTF-8 string by replacing all incorrect sequences by the
> replacement character (or whatever)?

The golang.org/x/text/transform package can be used to do very efficient
transformations on byte slices and strings.
A transformer called ReplaceIllFormed
(https://godoc.org/golang.org/x/text/runes#ReplaceIllFormed) exists in
the golang.org/x/text/runes package to do what you want (replace invalid
UTF-8 sequences with utf8.RuneError).
If you have a string already and don't mind a bit of extra allocation
overhead, you can do something simple like this:

    runes.ReplaceIllFormed().String("mystring")

—Sam

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to