iterators.go
package testmark
// HunkIter provides safe iteration over document hunks following Go conventions.
// Use the Next() method which returns (value, bool) like other Go iterators.
type HunkIter struct {
doc *Document
index int
}
// Next advances to the next hunk and returns the hunk and true if successful.
// Returns nil, false when iteration is complete.
// This follows the Go convention of returning (value, bool).
func (it *HunkIter) Next() (*Hunk, bool) {
if it.index >= len(it.doc.hunks) {
return nil, false
}
hunk := it.doc.hunks[it.index]
it.index++
return hunk.hunk, true
}
// All returns all remaining hunks as a slice.
func (it *HunkIter) All() []*Hunk {
var result []*Hunk
for hunk, ok := it.Next(); ok; hunk, ok = it.Next() {
result = append(result, hunk)
}
return result
}
// DirIter provides safe iteration over directory entries following Go conventions.
// Use the Next() method which returns (value, bool) like other Go iterators.
type DirIter struct {
entries []*DirEntry
index int
}
// Next advances to the next entry and returns the entry and true if successful.
// Returns nil, false when iteration is complete.
// This follows the Go convention of returning (value, bool).
func (it *DirIter) Next() (*DirEntry, bool) {
if it.index >= len(it.entries) {
return nil, false
}
entry := it.entries[it.index]
it.index++
return entry, true
}
// All returns all remaining entries as a slice.
func (it *DirIter) All() []*DirEntry {
var result []*DirEntry
for entry, ok := it.Next(); ok; entry, ok = it.Next() {
result = append(result, entry)
}
return result
}
v2/iterators.go · 1.6 KiB · 93c0df2a6e