stringish
A tiny Go module that provides a generic type constraint for “string-like”
data, and a utf8 package that works with both strings and byte slices
without conversions.
Install
go get github.com/clipperhouse/stringish/utf8
import (
"github.com/clipperhouse/stringish/utf8"
)
Examples
String:
s := "Hello, 世界"
r, size := utf8.DecodeRune(s) // not DecodeRuneInString 🎉
b := []byte("Hello, 世界")
r, size = utf8.DecodeRune(b) // same API!
Motivation
Sometimes we want APIs to accept string or []byte without having to convert
between them. That conversion usually allocates! By implementing with
stringish.Interface, we can have a single API for both types, i.e. one Foo
func instead of Foo and FooString.
We have converted unicode/utf8 as an example, note the absence of *InString
funcs. Others soon, perhaps from x/text.
Packages
github.com/clipperhouse/stringish: the Interface type constraint.
github.com/clipperhouse/stringish/utf8: UTF-8 helpers parameterized over string or []byte.
Used in
clipperhouse/uax29
clipperhouse/displaywidth