| 1 |
package textseg |
| 2 |
|
| 3 |
import "unicode/utf8" |
| 4 |
|
| 5 |
// ScanGraphemeClusters is a split function for bufio.Scanner that splits |
| 6 |
// on UTF8 sequence boundaries. |
| 7 |
// |
| 8 |
// This is included largely for completeness, since this behavior is already |
| 9 |
// built in to Go when ranging over a string. |
| 10 |
func ScanUTF8Sequences(data []byte, atEOF bool) (int, []byte, error) { |
| 11 |
if len(data) == 0 { |
| 12 |
return 0, nil, nil |
| 13 |
} |
| 14 |
r, seqLen := utf8.DecodeRune(data) |
| 15 |
if r == utf8.RuneError && !atEOF { |
| 16 |
return 0, nil, nil |
| 17 |
} |
| 18 |
return seqLen, data[:seqLen], nil |
| 19 |
} |