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