MCPcopy Create free account
hub / github.com/bobg/hashsplit / Split

Method Split

hashsplit.go:105–150  ·  view source on GitHub ↗

Split consumes the given input stream and hashsplits it, producing an iterator of chunk/level pairs. Each chunk is a subsequence of the input stream. Concatenating the chunks in order produces the original stream. A chunk's "level" is a number that indicates how many trailing zero bits the chunk's

(r io.Reader)

Source from the content-addressed store, hash-verified

103//
104// The Splitter should not be reused for another stream after this call.
105func (s *Splitter) Split(r io.Reader) (iter.Seq2[[]byte, int], *error) {
106 var br io.ByteReader
107 if b, ok := r.(io.ByteReader); ok {
108 br = b
109 } else {
110 br = bufio.NewReader(r)
111 }
112
113 minSize := s.MinSize
114 if minSize <= 0 {
115 minSize = defaultMinSize
116 }
117
118 var err error
119
120 f := func(yield func([]byte, int) bool) {
121 for {
122 var c byte
123 c, err = br.ReadByte()
124 if errors.Is(err, io.EOF) {
125 err = nil
126 if len(s.chunk) > 0 {
127 level, _ := s.checkSplit()
128 yield(s.chunk, level)
129 }
130 return
131 }
132 if err != nil {
133 return
134 }
135 s.chunk = append(s.chunk, c)
136 s.rs.Roll(c)
137 if len(s.chunk) < minSize {
138 continue
139 }
140 if level, shouldSplit := s.checkSplit(); shouldSplit {
141 if !yield(s.chunk, level) {
142 return
143 }
144 s.chunk = nil
145 }
146 }
147 }
148
149 return f, &err
150}
151
152func (s *Splitter) checkSplit() (int, bool) {
153 splitBits := s.SplitBits

Callers 1

SplitFunction · 0.95

Calls 1

checkSplitMethod · 0.95

Tested by

no test coverage detected