-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice.go
211 lines (183 loc) · 4.09 KB
/
slice.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"github.com/jimmyfrasche/invert"
)
type record struct {
Fields []string
Line string
}
func (r *record) F(n int) string {
ln := len(r.Fields)
if n < 0 {
n = ln + n
}
if n < 0 || n >= ln {
return ""
}
return r.Fields[n]
}
func (r *record) String() string {
return r.Line
}
func SubmatchSplit(header []string, RS, LinePattern string, Stdin io.Reader) (ret interface{}, err error) {
rs, err := cmpl(RS) //might as well add these to the cache
if err != nil {
return
}
lp, err := cmpl(LinePattern)
if err != nil {
return
}
//code loosely based on but entirely inspired by rsc's reply to
//https://groups.google.com/forum/#!topic/golang-nuts/4LpRZDfNXIc
if lp.NumSubexp() < 1 {
return nil, errors.New("submatch splitting requires a regexp with submatches")
}
names := hdr2map(header, true)
if len(names) == 0 {
for i, name := range lp.SubexpNames() {
if name != "" {
names[name] = i
}
}
}
stdin, err := ioutil.ReadAll(Stdin)
if err != nil {
return
}
pairs := rs.FindAllIndex(stdin, -1)
records := invert.Indicies(pairs, len(stdin))
if len(names) > 0 {
//if there are named submatches build the row as a map with just the named entries.
//multiple names are by construction the value of the last name.
out := make([]map[string]string, 0, len(records))
for _, p := range records {
if sms := lp.FindSubmatch(stdin[p[0]:p[1]]); sms != nil {
row := make(map[string]string, len(names))
for name, i := range names {
if i < len(sms) {
row[name] = string(sms[i])
} else {
row[name] = ""
}
}
out = append(out, row)
}
}
ret = out
} else {
out := make([]*record, 0, len(records))
for _, p := range records {
line := stdin[p[0]:p[1]]
if sms := lp.FindSubmatch(line); sms != nil {
row := make([]string, 0, len(sms)-1)
for _, sm := range sms[1:] {
row = append(row, string(sm))
}
out = append(out, &record{
Fields: row,
Line: string(line),
})
}
}
ret = out
}
return
}
func Split(header []string, RS, FS string, Stdin io.Reader) (ret interface{}, err error) {
rs, err := cmpl(RS)
if err != nil {
return
}
fs, err := cmpl(FS)
if err != nil {
return
}
stdin, err := ioutil.ReadAll(Stdin)
if err != nil {
return
}
names := hdr2map(header, false)
pairs := rs.FindAllIndex(stdin, -1)
records := invert.Indicies(pairs, len(stdin))
if len(names) > 0 {
out := make([]map[string]string, 0, len(records))
for _, p := range records {
s := stdin[p[0]:p[1]]
is := fs.FindAllIndex(s, -1)
is = invert.Indicies(is, len(s))
row := make(map[string]string, len(names))
for name, i := range names {
if i < len(is) {
ix := is[i]
row[name] = string(s[ix[0]:ix[1]])
} else {
row[name] = ""
}
}
out = append(out, row)
}
ret = out
} else {
out := make([]*record, 0, len(records))
for _, p := range records {
s := stdin[p[0]:p[1]]
is := fs.FindAllIndex(s, -1)
is = invert.Indicies(is, len(s))
row := make([]string, 0, len(is))
for _, ix := range is {
row = append(row, string(s[ix[0]:ix[1]]))
}
out = append(out, &record{
Fields: row,
Line: string(s),
})
}
ret = out
}
return
}
func CSV(header []string, Stdin io.Reader) (interface{}, error) {
r := csv.NewReader(Stdin)
r.LazyQuotes = true
r.TrimLeadingSpace = true
if ln := len(header); ln > 0 {
r.FieldsPerRecord = ln
}
recs, err := r.ReadAll()
if err != nil {
return nil, err
}
if len(recs) == 0 {
return nil, nil
}
if header == nil {
header, recs = recs[0], recs[1:]
}
rows := []map[string]string{}
for rn, rec := range recs {
if h, r := len(header), len(rec); h != r {
return nil, fmt.Errorf("%d: row len %d ≠ header len %d", rn, r, h)
}
row := map[string]string{}
for i, h := range header {
row[h] = rec[i]
}
rows = append(rows, row)
}
return rows, nil
}
func JSON(Stdin io.Reader) (out interface{}, err error) {
stdin, err := ioutil.ReadAll(Stdin)
if err != nil {
return
}
err = json.Unmarshal(stdin, &out)
return
}