-
Notifications
You must be signed in to change notification settings - Fork 12
/
cmp.go
185 lines (172 loc) · 3.19 KB
/
cmp.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
// docs:
// https://sqlite.org/datatype3.html chapter "4. Comparison Expressions"
package db
import (
"bytes"
"strings"
)
type collate func(string, string) int
var DefaultCollate = "binary"
// available collate functions
var CollateFuncs = map[string]func(string, string) int{
"binary": strings.Compare,
"rtrim": func(a, b string) int {
return strings.Compare(
strings.TrimRight(a, " \t\r\n"),
strings.TrimRight(b, " \t\r\n"),
)
},
"nocase": func(a, b string) int {
lc := func(r rune) rune {
if r >= 'A' && r <= 'Z' {
return rune(strings.ToLower(string(r))[0])
}
return r
}
return strings.Compare(
strings.Map(lc, a),
strings.Map(lc, b),
)
},
}
type Key []KeyCol
type KeyCol struct {
V interface{}
Collate string // either empty or names a valid CollateFuncs key
Desc bool
}
func Equals(key Key, r Record) bool {
for i, k := range key {
if len(r)-1 < i {
return false
}
coll := DefaultCollate
if k.Collate != "" {
coll = k.Collate
}
if compare(k.V, r[i], CollateFuncs[coll]) != 0 {
return false
}
}
return true
}
// True if r is eq or bigger than key
func Search(key Key, r Record) bool {
for i, k := range key {
if len(r)-1 < i {
return false
}
coll := DefaultCollate
if k.Collate != "" {
coll = k.Collate
}
cmp := compare(k.V, r[i], CollateFuncs[coll])
if k.Desc {
switch {
case cmp > 0:
return true
case cmp == 0:
default:
return false
}
} else {
switch {
case cmp < 0:
return true
case cmp == 0:
case cmp > 0:
return false
}
}
}
return true
}
// compare record values, with ordering according to SQLite's type sort order:
// nil < {int64|float64} < string < []byte
//
// same logic as strings.Compare:
// returns:
// -1 when a is smaller than b
// 0 when all fields from a match b
// 1 when a is bigger than b
func compare(a, b interface{}, c collate) int {
switch at := a.(type) {
case nil:
switch b.(type) {
case nil:
return 0
case int64, float64, string, []byte:
return -1
default:
panic("impossible cmp type")
}
case int64:
switch bt := b.(type) {
case nil:
return 1
case int64:
return cmpInt64(at, bt)
case float64:
return cmpFloat64(float64(at), bt)
case string, []byte:
return -1
default:
panic("impossible cmp type")
}
case float64:
switch bt := b.(type) {
case nil:
return 1
case int64:
return cmpFloat64(at, float64(bt))
case float64:
return cmpFloat64(at, bt)
case string, []byte:
return -1
default:
panic("impossible cmp type")
}
case string:
switch bt := b.(type) {
case nil, int64, float64:
return 1
case string:
return c(at, bt)
case []byte:
return -1
default:
panic("impossible cmp type")
}
case []byte:
switch bt := b.(type) {
case nil, int64, float64, string:
return 1
case []byte:
return bytes.Compare(at, bt)
default:
panic("impossible cmp type")
}
default:
panic("impossible cmp type for a")
}
}
func cmpInt64(a, b int64) int {
switch {
case a < b:
return -1
case a == b:
return 0
default:
return 1
}
}
func cmpFloat64(a, b float64) int {
switch {
case a < b:
return -1
case a == b:
return 0
default:
return 1
}
}