|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package bytes |
| 6 | + |
| 7 | +import ( |
| 8 | + "iter" |
| 9 | + "unicode" |
| 10 | + "unicode/utf8" |
| 11 | +) |
| 12 | + |
| 13 | +// Lines returns an iterator over the newline-terminated lines in the byte slice s. |
| 14 | +// The lines yielded by the iterator include their terminating newlines. |
| 15 | +// If s is empty, the iterator yields no lines at all. |
| 16 | +// If s does not end in a newline, the final yielded line will not end in a newline. |
| 17 | +// It returns a single-use iterator. |
| 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) { |
| 28 | + return |
| 29 | + } |
| 30 | + } |
| 31 | + return |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// explodeSeq returns an iterator over the runes in s. |
| 36 | +func explodeSeq(s []byte) iter.Seq[[]byte] { |
| 37 | + return func(yield func([]byte) bool) { |
| 38 | + for len(s) > 0 { |
| 39 | + _, size := utf8.DecodeRune(s) |
| 40 | + if !yield(s[:size]) { |
| 41 | + return |
| 42 | + } |
| 43 | + s = s[size:] |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// splitSeq is SplitSeq or SplitAfterSeq, configured by how many |
| 49 | +// bytes of sep to include in the results (none or all). |
| 50 | +func splitSeq(s, sep []byte, sepSave int) iter.Seq[[]byte] { |
| 51 | + if len(sep) == 0 { |
| 52 | + return explodeSeq(s) |
| 53 | + } |
| 54 | + return func(yield func([]byte) bool) { |
| 55 | + for { |
| 56 | + i := Index(s, sep) |
| 57 | + if i < 0 { |
| 58 | + break |
| 59 | + } |
| 60 | + frag := s[:i+sepSave] |
| 61 | + if !yield(frag) { |
| 62 | + return |
| 63 | + } |
| 64 | + s = s[i+len(sep):] |
| 65 | + } |
| 66 | + yield(s) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// SplitSeq returns an iterator over all substrings of s separated by sep. |
| 71 | +// The iterator yields the same strings that would be returned by Split(s, sep), |
| 72 | +// but without constructing the slice. |
| 73 | +// It returns a single-use iterator. |
| 74 | +func SplitSeq(s, sep []byte) iter.Seq[[]byte] { |
| 75 | + return splitSeq(s, sep, 0) |
| 76 | +} |
| 77 | + |
| 78 | +// SplitAfterSeq returns an iterator over substrings of s split after each instance of sep. |
| 79 | +// The iterator yields the same strings that would be returned by SplitAfter(s, sep), |
| 80 | +// but without constructing the slice. |
| 81 | +// It returns a single-use iterator. |
| 82 | +func SplitAfterSeq(s, sep []byte) iter.Seq[[]byte] { |
| 83 | + return splitSeq(s, sep, len(sep)) |
| 84 | +} |
| 85 | + |
| 86 | +// FieldsSeq returns an iterator over substrings of s split around runs of |
| 87 | +// whitespace characters, as defined by unicode.IsSpace. |
| 88 | +// The iterator yields the same strings that would be returned by Fields(s), |
| 89 | +// but without constructing the slice. |
| 90 | +func FieldsSeq(s []byte) iter.Seq[[]byte] { |
| 91 | + return func(yield func([]byte) bool) { |
| 92 | + s := s |
| 93 | + start := -1 |
| 94 | + for i := 0; i < len(s); { |
| 95 | + size := 1 |
| 96 | + r := rune(s[i]) |
| 97 | + isSpace := asciiSpace[s[i]] != 0 |
| 98 | + if r >= utf8.RuneSelf { |
| 99 | + r, size = utf8.DecodeRune(s[i:]) |
| 100 | + isSpace = unicode.IsSpace(r) |
| 101 | + } |
| 102 | + if isSpace { |
| 103 | + if start >= 0 { |
| 104 | + if !yield(s[start:i]) { |
| 105 | + return |
| 106 | + } |
| 107 | + start = -1 |
| 108 | + } |
| 109 | + } else if start < 0 { |
| 110 | + start = i |
| 111 | + } |
| 112 | + i += size |
| 113 | + } |
| 114 | + if start >= 0 { |
| 115 | + yield(s[start:]) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// FieldsFuncSeq returns an iterator over substrings of s split around runs of |
| 121 | +// Unicode code points satisfying f(c). |
| 122 | +// The iterator yields the same strings that would be returned by FieldsFunc(s), |
| 123 | +// but without constructing the slice. |
| 124 | +func FieldsFuncSeq(s []byte, f func(rune) bool) iter.Seq[[]byte] { |
| 125 | + return func(yield func([]byte) bool) { |
| 126 | + s := s |
| 127 | + start := -1 |
| 128 | + for i := 0; i < len(s); { |
| 129 | + size := 1 |
| 130 | + r := rune(s[i]) |
| 131 | + if r >= utf8.RuneSelf { |
| 132 | + r, size = utf8.DecodeRune(s[i:]) |
| 133 | + } |
| 134 | + if f(r) { |
| 135 | + if start >= 0 { |
| 136 | + if !yield(s[start:i]) { |
| 137 | + return |
| 138 | + } |
| 139 | + start = -1 |
| 140 | + } |
| 141 | + } else if start < 0 { |
| 142 | + start = i |
| 143 | + } |
| 144 | + i += size |
| 145 | + } |
| 146 | + if start >= 0 { |
| 147 | + yield(s[start:]) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments