-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
53 lines (45 loc) · 788 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package enu
import (
"bufio"
"os"
)
func FromFile(path string) *Enumerable[string] {
return New[string](NewFileReader(path))
}
type FileReader struct {
path string
f *os.File
err error
scanner *bufio.Scanner
}
func NewFileReader(path string) *FileReader {
return &FileReader{path: path}
}
func (r *FileReader) Err() error {
return r.err
}
func (r *FileReader) Dispose() {
if r.f != nil {
err := r.f.Close()
if err != nil {
r.err = err
}
}
r.scanner = nil
}
func (r *FileReader) Next() (string, bool) {
if r.scanner == nil {
f, err := os.Open(r.path)
if err != nil {
r.err = err
return "", false
}
r.f = f
r.scanner = bufio.NewScanner(f)
}
ok := r.scanner.Scan()
if !ok {
return "", false
}
return r.scanner.Text(), true
}