-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
114 lines (88 loc) · 2.62 KB
/
find.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
package simplecsv
import (
"regexp"
"strings"
)
// FindInColumn returns a slice with the rownumbers where the "word" is in the columnPosition
// If the column is not valid it returns an empty slice and a second false value
func (s SimpleCsv) FindInColumn(columnPosition int, word string) ([]int, bool) {
var validColumn bool
results := []int{}
if columnPosition < 0 || columnPosition >= len(s[0]) {
validColumn = false
return results, validColumn
}
validColumn = true
numberOfRows := len(s)
for i := 0; i < numberOfRows; i++ {
if strings.ToLower(s[i][columnPosition]) == strings.ToLower(word) {
results = append(results, i)
}
}
return results, validColumn
}
// FindInField returns a slice with the rownumbers where the "word" is in the column name
// If the column is not valid it returns an empty slice and a second false value
func (s SimpleCsv) FindInField(columnName string, word string) ([]int, bool) {
columnPosition := s.GetHeaderPosition(columnName)
var validColumn bool
results := []int{}
if columnPosition == -1 {
validColumn = false
return results, validColumn
}
validColumn = true
numberOfRows := len(s)
for i := 1; i < numberOfRows; i++ {
if strings.ToLower(s[i][columnPosition]) == strings.ToLower(word) {
results = append(results, i)
}
}
return results, validColumn
}
// MatchInColumn returns a slice with the rownumbers where the regular expression applies in the columnPosition
// If the column or regular expression are not valid it returns an empty slice and a second false value
func (s SimpleCsv) MatchInColumn(columnPosition int, regularexpression string) ([]int, bool) {
var ok bool
results := []int{}
r, u := regexp.Compile(regularexpression)
if u != nil {
ok = false
return results, ok
}
if columnPosition < 0 || columnPosition >= len(s[0]) {
ok = false
return results, ok
}
ok = true
numberOfRows := len(s)
for i := 0; i < numberOfRows; i++ {
if r.MatchString(s[i][columnPosition]) {
results = append(results, i)
}
}
return results, ok
}
// MatchInField returns a slice with the rownumbers where the regular expression applies in the column name
func (s SimpleCsv) MatchInField(columnName string, regularexpression string) ([]int, bool) {
columnPosition := s.GetHeaderPosition(columnName)
var ok bool
results := []int{}
r, u := regexp.Compile(regularexpression)
if columnPosition == -1 {
ok = false
return results, ok
}
if u != nil {
ok = false
return results, ok
}
ok = true
numberOfRows := len(s)
for i := 1; i < numberOfRows; i++ {
if r.MatchString(s[i][columnPosition]) {
results = append(results, i)
}
}
return results, ok
}