|
| 1 | +package fileutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "io" |
| 6 | + "iter" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// LineOption configures the line iterator returned by Lines / LinesReader. |
| 12 | +type LineOption func(*lineConfig) |
| 13 | + |
| 14 | +type lineConfig struct { |
| 15 | + bufferSize int |
| 16 | + splitSet string |
| 17 | + hasSplit bool |
| 18 | + trimSpace bool |
| 19 | + skipEmpty bool |
| 20 | + filter func(string) bool |
| 21 | +} |
| 22 | + |
| 23 | +// WithBufferSize sets the underlying bufio.Scanner buffer. A non-positive |
| 24 | +// value leaves the scanner default (64 KiB) in place. |
| 25 | +func WithBufferSize(n int) LineOption { |
| 26 | + return func(c *lineConfig) { c.bufferSize = n } |
| 27 | +} |
| 28 | + |
| 29 | +// WithSplit splits each scanned line on any of the given runes |
| 30 | +// (strings.FieldsFunc semantics: runs of separator runes are collapsed and |
| 31 | +// empty pieces are not produced). Each piece becomes its own emitted value. |
| 32 | +func WithSplit(separators ...rune) LineOption { |
| 33 | + return func(c *lineConfig) { |
| 34 | + c.hasSplit = true |
| 35 | + c.splitSet = string(separators) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// WithTrimSpace trims leading/trailing whitespace from each emitted value. |
| 40 | +func WithTrimSpace() LineOption { |
| 41 | + return func(c *lineConfig) { c.trimSpace = true } |
| 42 | +} |
| 43 | + |
| 44 | +// WithSkipEmpty drops empty values, evaluated after WithTrimSpace. |
| 45 | +func WithSkipEmpty() LineOption { |
| 46 | + return func(c *lineConfig) { c.skipEmpty = true } |
| 47 | +} |
| 48 | + |
| 49 | +// WithFilter keeps only values for which keep returns true. The filter runs |
| 50 | +// after split / trim / skip-empty so it sees the final value that would be |
| 51 | +// yielded. |
| 52 | +func WithFilter(keep func(string) bool) LineOption { |
| 53 | + return func(c *lineConfig) { c.filter = keep } |
| 54 | +} |
| 55 | + |
| 56 | +// Lines streams lines from the file at filename, applying any configured |
| 57 | +// transforms. With no options it emits raw scanner lines. |
| 58 | +// |
| 59 | +// The file is opened lazily on first iteration and closed when iteration |
| 60 | +// ends (including via break). Open and scanner errors are surfaced as a |
| 61 | +// final ("", err) pair, after which iteration stops. |
| 62 | +// |
| 63 | +// Typical use: |
| 64 | +// |
| 65 | +// for v, err := range fileutil.Lines(path, |
| 66 | +// fileutil.WithSplit(','), |
| 67 | +// fileutil.WithTrimSpace(), |
| 68 | +// fileutil.WithSkipEmpty(), |
| 69 | +// ) { |
| 70 | +// if err != nil { return err } |
| 71 | +// // use v |
| 72 | +// } |
| 73 | +func Lines(filename string, opts ...LineOption) iter.Seq2[string, error] { |
| 74 | + return func(yield func(string, error) bool) { |
| 75 | + f, err := os.Open(filename) |
| 76 | + if err != nil { |
| 77 | + yield("", err) |
| 78 | + return |
| 79 | + } |
| 80 | + defer func() { _ = f.Close() }() |
| 81 | + scanLines(f, opts, yield) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// LinesReader is the io.Reader variant of Lines. The reader is consumed but |
| 86 | +// not closed; the caller owns its lifecycle. |
| 87 | +func LinesReader(r io.Reader, opts ...LineOption) iter.Seq2[string, error] { |
| 88 | + return func(yield func(string, error) bool) { |
| 89 | + scanLines(r, opts, yield) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func scanLines(r io.Reader, opts []LineOption, yield func(string, error) bool) { |
| 94 | + var cfg lineConfig |
| 95 | + for _, o := range opts { |
| 96 | + o(&cfg) |
| 97 | + } |
| 98 | + scanner := bufio.NewScanner(r) |
| 99 | + if cfg.bufferSize > 0 { |
| 100 | + scanner.Buffer(make([]byte, cfg.bufferSize), cfg.bufferSize) |
| 101 | + } |
| 102 | + for scanner.Scan() { |
| 103 | + line := scanner.Text() |
| 104 | + if !cfg.hasSplit { |
| 105 | + if !emitLine(line, &cfg, yield) { |
| 106 | + return |
| 107 | + } |
| 108 | + continue |
| 109 | + } |
| 110 | + for _, piece := range strings.FieldsFunc(line, func(r rune) bool { |
| 111 | + return strings.ContainsRune(cfg.splitSet, r) |
| 112 | + }) { |
| 113 | + if !emitLine(piece, &cfg, yield) { |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + if err := scanner.Err(); err != nil { |
| 119 | + yield("", err) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func emitLine(v string, cfg *lineConfig, yield func(string, error) bool) bool { |
| 124 | + if cfg.trimSpace { |
| 125 | + v = strings.TrimSpace(v) |
| 126 | + } |
| 127 | + if cfg.skipEmpty && v == "" { |
| 128 | + return true |
| 129 | + } |
| 130 | + if cfg.filter != nil && !cfg.filter(v) { |
| 131 | + return true |
| 132 | + } |
| 133 | + return yield(v, nil) |
| 134 | +} |
0 commit comments