-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
101 lines (84 loc) · 1.86 KB
/
scanner.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package io
import (
"bufio"
"io"
"log"
"strings"
"text/scanner"
"github.com/pkg/errors"
)
func scanFile(r io.Reader, n int) {
defer CloseReaderIf(r)
// rd := bufio.NewReader(r)
sc := bufio.NewScanner(r)
for sc.Scan() {
_ = sc.Text() // GET the line string
}
if err := sc.Err(); err != nil {
log.Fatalf("scan file error: %v", err)
return
}
}
func readFileLines(r io.Reader) (lines []string, err error) {
defer CloseReaderIf(r)
rd := bufio.NewReader(r)
for {
line, err1 := rd.ReadString('\n')
lines = append(lines, line)
if err1 != nil {
if err1 != io.EOF {
return nil, errors.Wrapf(err1, "read file line error: %v", err1)
}
break
}
}
return lines, nil
}
type head struct {
r io.Reader // source Reader
n int // number of lines
}
const sampleText = `sample text
This stuff is boring.
It reads lines.
It reads more lines.
It puts the lotion on its skin...
last line
`
func sample() string {
sampleReader := strings.NewReader(sampleText)
sampleHead := &head{sampleReader, DefaultHeadLength}
b := make([]byte, 0, DefaultHeadLength*80)
_, err := sampleHead.Read(b)
if err != nil {
return ""
}
return string(b)
}
// Head returns an io.Reader that always
// returns the first n lines of a source reader
func Head(r io.Reader, n int) ([]byte, error) {
return io.ReadAll(&head{r, n})
}
func (h *head) Read(p []byte) (n int, err error) {
pr, pw := io.Pipe()
var s scanner.Scanner
s.Init(h.r)
s.Whitespace ^= 1 << '\n' // don't skip new lines
// s.Whitespace ^= 1<<'\t' | 1<<'\n' // don't skip tabs and new lines
for tok := s.Scan(); tok != scanner.EOF && s.Line < h.n; tok = s.Scan() {
// if tok == '\n' {
// count++
// }
n++
pw.Write([]byte(string(tok)))
pr.Read(p)
}
return n, pr.Close()
}
func ByteHead() {
// lr, err := io.LimitReader(h.r, int64(n)).Read(buf)
// if err != nil {
// return nil, err
// }
}