-
Notifications
You must be signed in to change notification settings - Fork 12
/
region.go
243 lines (214 loc) · 4.67 KB
/
region.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package z
import (
"errors"
"regexp"
"strconv"
"strings"
"time"
)
type rtype int
const (
intRegion rtype = iota
longRegion
floatRegion
float64Region
dateRegion
nilValue
)
type Region struct {
Left interface{}
Right interface{}
LeftOpen bool
RightOpen bool
_type_ rtype
}
func (r *Region) String() string {
sb := StringBuilder()
if r.LeftOpen {
sb.Append("(")
} else {
sb.Append("[")
}
if r.HasLeft() {
sb.Append(r.Left)
}
sb.Append(",")
if r.HasRight() {
sb.Append(r.Right)
}
if r.RightOpen {
sb.Append(")")
} else {
sb.Append("]")
}
return sb.String()
}
// 正负整数
const REX_INT string = "^(-?)(\\d+)$"
// 正负浮点数
const REX_FLOAT string = "^(-?)(\\d+)\\.(\\d+)$"
// yyyy-MM-dd
const REX_DATE string = "^([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))$"
// yyyy-MM-dd hh:mm:ss
const REX_DATE_TIME string = "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))\\s+(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"
func MakeRegion(rstr string) *Region {
r := new(Region)
leftstr, rightstr, leftOpen, rightOpen := extractLeftAndRight(rstr)
r.LeftOpen = leftOpen
r.RightOpen = rightOpen
var rt rtype
r.Left, rt = toAppropriateType(leftstr)
if rt != nilValue {
r._type_ = rt
}
r.Right, rt = toAppropriateType(rightstr)
if rt != nilValue {
r._type_ = rt
}
return r
}
func toAppropriateType(str string) (interface{}, rtype) {
// 空
if IsBlank(str) {
return nil, nilValue
} else {
str = Trim(str)
}
regInt := regexp.MustCompile(REX_INT)
if regInt.MatchString(str) {
sint, err1 := strconv.Atoi(str)
if err1 != nil {
sint64, err2 := strconv.ParseInt(str, 10, 64)
if err2 != nil {
panic(err2)
} else {
return sint64, longRegion
}
} else {
return sint, intRegion
}
}
regFloat := regexp.MustCompile(REX_FLOAT)
if regFloat.MatchString(str) {
sfloat, err1 := strconv.ParseFloat(str, 32)
if err1 != nil {
sfloat64, err2 := strconv.ParseFloat(str, 64)
if err2 != nil {
panic(err2)
} else {
return sfloat64, float64Region
}
} else {
return float32(sfloat), floatRegion
}
}
regDateTime := regexp.MustCompile(REX_DATE_TIME)
if regDateTime.MatchString(str) {
sdatetime, err1 := time.ParseInLocation("2006-01-02 15:04:05", TrimExtraSpace(str), time.Local)
if err1 != nil {
panic(err1)
} else {
return sdatetime, dateRegion
}
}
regDate := regexp.MustCompile(REX_DATE)
if regDate.MatchString(str) {
sdate, err1 := time.ParseInLocation("2006-01-02", str, time.Local)
if err1 != nil {
panic(err1)
} else {
return sdate, dateRegion
}
}
// 没有可以匹配的?
panic(errors.New("not a region appropriate type, " + str))
}
func extractLeftAndRight(rstr string) (left, right string, lopen, ropen bool) {
lr := strings.Split(rstr, ",")
if len(lr) == 2 {
lopen = strings.HasPrefix(lr[0], "(")
ropen = strings.HasSuffix(lr[1], ")")
left = lr[0][1:]
right = lr[1][:len(lr[1])-1]
DebugPrintf("region (%s, %s, %v, %v)\n", left, right, lopen, ropen)
// TODO 左右自动调整位置???
return
}
panic("wrong format for region, " + rstr)
}
func (r *Region) HasLeft() bool {
return r.Left != nil
}
func (r *Region) HasRight() bool {
return r.Right != nil
}
func (r *Region) LeftInt() int {
nleft, ok := r.Left.(int)
if ok {
return nleft
}
panic("not int type")
}
func (r *Region) RightInt() int {
nright, ok := r.Right.(int)
if ok {
return nright
}
panic("not int type")
}
func (r *Region) LeftLong() int64 {
nleft, ok := r.Left.(int64)
if ok {
return nleft
}
panic("not long type")
}
func (r *Region) RightLong() int64 {
nright, ok := r.Right.(int64)
if ok {
return nright
}
panic("not long type")
}
func (r *Region) LeftFloat() float32 {
nleft, ok := r.Left.(float32)
if ok {
return nleft
}
panic("not float32 type")
}
func (r *Region) RightFloat() float32 {
nright, ok := r.Right.(float32)
if ok {
return nright
}
panic("not float32 type")
}
func (r *Region) LeftFloat64() float64 {
nleft, ok := r.Left.(float64)
if ok {
return nleft
}
panic("not float64 type")
}
func (r *Region) RightFloat64() float64 {
nright, ok := r.Right.(float64)
if ok {
return nright
}
panic("not float64 type")
}
func (r *Region) LeftDate() time.Time {
nleft, ok := r.Left.(time.Time)
if ok {
return nleft
}
panic("not date type")
}
func (r *Region) RightDate() time.Time {
nright, ok := r.Right.(time.Time)
if ok {
return nright
}
panic("not date type")
}