-
Notifications
You must be signed in to change notification settings - Fork 0
/
strfile.go
89 lines (73 loc) · 1.58 KB
/
strfile.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
// Package strfile provides an implementation of strfile(6), given no arguments.
package strfile
import (
"bufio"
"bytes"
"encoding/binary"
"io"
)
const delim = '%'
// Strfile should perform the same operation as would running strfile(6) over a
// file with the Readers contents, given no options.
func Strfile(r io.Reader, w io.Writer) error {
scanner := bufio.NewScanner(r)
scanner.Split(scanDelim)
var (
longlen uint32
shortlen uint32 = 0xffffffff
numstr uint32
pos uint32
offsets []uint32
)
for scanner.Scan() {
line := scanner.Bytes()
numstr += 1
length := uint32(len(line))
pos += length
if bytes.HasSuffix(line, []byte{'\n', delim, '\n'}) {
length -= 2
}
if longlen < length {
longlen = length
}
if length < shortlen {
shortlen = length
}
offsets = append(offsets, pos)
}
if scanner.Err() != nil {
return scanner.Err()
}
err := binary.Write(w, binary.BigEndian, header{
Version: 2,
Numstr: numstr,
Longlen: longlen,
Shortlen: shortlen,
Flags: 0,
Delim: [8]byte{delim, 0, 0, 0, 0, 0, 0, 0},
})
if err != nil {
return err
}
return binary.Write(w, binary.BigEndian, offsets)
}
type header struct {
Version uint32
Numstr uint32
Longlen uint32
Shortlen uint32
Flags uint32
Delim [8]byte
}
func scanDelim(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte{'\n', delim, '\n'}); i >= 0 {
return i + 3, data[0 : i+3], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}