Source file
src/bytes/iter.go
1
2
3
4
5 package bytes
6
7 import (
8 "iter"
9 "unicode"
10 "unicode/utf8"
11 )
12
13
14
15
16
17
18 func Lines(s []byte) iter.Seq[[]byte] {
19 return func(yield func([]byte) bool) {
20 for len(s) > 0 {
21 var line []byte
22 if i := IndexByte(s, '\n'); i >= 0 {
23 line, s = s[:i+1], s[i+1:]
24 } else {
25 line, s = s, nil
26 }
27 if !yield(line[:len(line):len(line)]) {
28 return
29 }
30 }
31 }
32 }
33
34
35
36 func splitSeq(s, sep []byte, sepSave int) iter.Seq[[]byte] {
37 return func(yield func([]byte) bool) {
38 if len(sep) == 0 {
39 for len(s) > 0 {
40 _, size := utf8.DecodeRune(s)
41 if !yield(s[:size:size]) {
42 return
43 }
44 s = s[size:]
45 }
46 return
47 }
48 for {
49 i := Index(s, sep)
50 if i < 0 {
51 break
52 }
53 frag := s[:i+sepSave]
54 if !yield(frag[:len(frag):len(frag)]) {
55 return
56 }
57 s = s[i+len(sep):]
58 }
59 yield(s[:len(s):len(s)])
60 }
61 }
62
63
64
65
66
67 func SplitSeq(s, sep []byte) iter.Seq[[]byte] {
68 return splitSeq(s, sep, 0)
69 }
70
71
72
73
74
75 func SplitAfterSeq(s, sep []byte) iter.Seq[[]byte] {
76 return splitSeq(s, sep, len(sep))
77 }
78
79
80
81
82
83 func FieldsSeq(s []byte) iter.Seq[[]byte] {
84 return func(yield func([]byte) bool) {
85 start := -1
86 for i := 0; i < len(s); {
87 size := 1
88 r := rune(s[i])
89 isSpace := asciiSpace[s[i]] != 0
90 if r >= utf8.RuneSelf {
91 r, size = utf8.DecodeRune(s[i:])
92 isSpace = unicode.IsSpace(r)
93 }
94 if isSpace {
95 if start >= 0 {
96 if !yield(s[start:i:i]) {
97 return
98 }
99 start = -1
100 }
101 } else if start < 0 {
102 start = i
103 }
104 i += size
105 }
106 if start >= 0 {
107 yield(s[start:len(s):len(s)])
108 }
109 }
110 }
111
112
113
114
115
116 func FieldsFuncSeq(s []byte, f func(rune) bool) iter.Seq[[]byte] {
117 return func(yield func([]byte) bool) {
118 start := -1
119 for i := 0; i < len(s); {
120 size := 1
121 r := rune(s[i])
122 if r >= utf8.RuneSelf {
123 r, size = utf8.DecodeRune(s[i:])
124 }
125 if f(r) {
126 if start >= 0 {
127 if !yield(s[start:i:i]) {
128 return
129 }
130 start = -1
131 }
132 } else if start < 0 {
133 start = i
134 }
135 i += size
136 }
137 if start >= 0 {
138 yield(s[start:len(s):len(s)])
139 }
140 }
141 }
142
View as plain text