-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes_iter.go
109 lines (89 loc) · 1.54 KB
/
bytes_iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package g
import (
"unicode"
"unicode/utf8"
)
func explodeBytes(bs Bytes) SeqSlice[Bytes] {
return func(yield func(Bytes) bool) {
for len(bs) > 0 {
_, size := utf8.DecodeRune(bs)
if !yield(bs[:size]) {
return
}
bs = bs[size:]
}
}
}
func splitBytes(bs, sep Bytes, sepSave Int) SeqSlice[Bytes] {
if len(sep) == 0 {
return explodeBytes(bs)
}
return func(yield func(Bytes) bool) {
for {
i := bs.Index(sep)
if i < 0 {
break
}
frag := bs[:i+sepSave]
if !yield(frag) {
return
}
bs = bs[i+sep.Len():]
}
yield(bs)
}
}
func fieldsBytes(bs Bytes) SeqSlice[Bytes] {
return func(yield func(Bytes) bool) {
start := -1
for i := 0; i < len(bs); {
size := 1
r := rune(bs[i])
isSpace := asciiSpace[bs[i]] != 0
if r >= utf8.RuneSelf {
r, size = utf8.DecodeRune(bs[i:])
isSpace = unicode.IsSpace(r)
}
if isSpace {
if start >= 0 {
if !yield(bs[start:i]) {
return
}
start = -1
}
} else if start < 0 {
start = i
}
i += size
}
if start >= 0 {
yield(bs[start:])
}
}
}
func fieldsbyBytes(bs Bytes, f func(rune) bool) SeqSlice[Bytes] {
return func(yield func(Bytes) bool) {
start := -1
for i := 0; i < len(bs); {
size := 1
r := rune(bs[i])
if r >= utf8.RuneSelf {
r, size = utf8.DecodeRune(bs[i:])
}
if f(r) {
if start >= 0 {
if !yield(bs[start:i]) {
return
}
start = -1
}
} else if start < 0 {
start = i
}
i += size
}
if start >= 0 {
yield(bs[start:])
}
}
}