iterator.go 804 B

12345678910111213141516171819202122232425262728293031
  1. package graphemes
  2. import (
  3. "github.com/clipperhouse/stringish"
  4. "github.com/clipperhouse/uax29/v2/internal/iterators"
  5. )
  6. type Iterator[T stringish.Interface] struct {
  7. *iterators.Iterator[T]
  8. }
  9. var (
  10. splitFuncString = splitFunc[string]
  11. splitFuncBytes = splitFunc[[]byte]
  12. )
  13. // FromString returns an iterator for the grapheme clusters in the input string.
  14. // Iterate while Next() is true, and access the grapheme via Value().
  15. func FromString(s string) Iterator[string] {
  16. return Iterator[string]{
  17. iterators.New(splitFuncString, s),
  18. }
  19. }
  20. // FromBytes returns an iterator for the grapheme clusters in the input bytes.
  21. // Iterate while Next() is true, and access the grapheme via Value().
  22. func FromBytes(b []byte) Iterator[[]byte] {
  23. return Iterator[[]byte]{
  24. iterators.New(splitFuncBytes, b),
  25. }
  26. }