src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

README.md (2054B)


      1 # stringish
      2 
      3 A small Go module that provides a generic type constraint for “string-like”
      4 data, and a utf8 package that works with both strings and byte slices
      5 without conversions.
      6 
      7 ```go
      8 type Interface interface {
      9 	~[]byte | ~string
     10 }
     11 ```
     12 
     13 [![Go Reference](https://pkg.go.dev/badge/github.com/clipperhouse/stringish/utf8.svg)](https://pkg.go.dev/github.com/clipperhouse/stringish/utf8)
     14 [![Test Status](https://github.com/clipperhouse/stringish/actions/workflows/gotest.yml/badge.svg)](https://github.com/clipperhouse/stringish/actions/workflows/gotest.yml)
     15 
     16 ## Install
     17 
     18 ```
     19 go get github.com/clipperhouse/stringish
     20 ```
     21 
     22 ## Examples
     23 
     24 ```go
     25 import (
     26     "github.com/clipperhouse/stringish"
     27     "github.com/clipperhouse/stringish/utf8"
     28 )
     29 
     30 s := "Hello, 世界"
     31 r, size := utf8.DecodeRune(s)   // not DecodeRuneInString 🎉
     32 
     33 b := []byte("Hello, 世界")
     34 r, size = utf8.DecodeRune(b)    // same API!
     35 
     36 func MyFoo[T stringish.Interface](s T) T {
     37     // pass a string or a []byte
     38     // iterate, slice, transform, whatever
     39 }
     40 ```
     41 
     42 ## Motivation
     43 
     44 Sometimes we want APIs to accept `string` or `[]byte` without having to convert
     45 between those types. That conversion usually allocates!
     46 
     47 By implementing with `stringish.Interface`, we can have a single API, and
     48 single implementation for both types: one `Foo` instead of `Foo` and
     49 `FooString`.
     50 
     51 We have converted the
     52 [`unicode/utf8` package](https://github.com/clipperhouse/stringish/blob/main/utf8/utf8.go)
     53 as an example -- note the absence of`*InString` funcs. We might look at `x/text`
     54 next.
     55 
     56 ## Used by
     57 
     58 - clipperhouse/uax29: [stringish trie](https://github.com/clipperhouse/uax29/blob/master/graphemes/trie.go#L27), [stringish iterator](https://github.com/clipperhouse/uax29/blob/master/internal/iterators/iterator.go#L9), [stringish SplitFunc](https://github.com/clipperhouse/uax29/blob/master/graphemes/splitfunc.go#L21)
     59 
     60 - [clipperhouse/displaywidth](https://github.com/clipperhouse/displaywidth)
     61 
     62 ## Prior discussion
     63 
     64 - [Consideration of similar by the Go team](https://github.com/golang/go/issues/48643)