-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
170 lines (147 loc) · 2.98 KB
/
utils.go
File metadata and controls
170 lines (147 loc) · 2.98 KB
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
package goda
import (
"cmp"
"encoding"
"math"
"strconv"
"time"
)
func parseInt64(input []byte) (int64, error) {
return strconv.ParseInt(string(input), 10, 64)
}
func parseInt(input []byte) (i int, e error) {
i, e = strconv.Atoi(string(input))
return
}
func unmarshalJsonImpl[T encoding.TextUnmarshaler](ref T, data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return parseFailedError(data)
}
return ref.UnmarshalText(data[1 : len(data)-1])
}
func marshalJsonImpl[T encoding.TextAppender](ref T) ([]byte, error) {
d, e := ref.AppendText([]byte{'"'})
if e != nil {
return nil, e
}
d = append(d, '"')
return d, nil
}
func marshalTextImpl[T encoding.TextAppender](ref T) ([]byte, error) {
return ref.AppendText(nil)
}
func stringImpl[T encoding.TextAppender](ref T) string {
b, e := ref.AppendText(nil)
if e != nil {
panic(e)
}
return bytes2string(b)
}
func bytes2string(b []byte) string {
return string(b)
}
func floorDiv(x, y int64) (q int64) {
q = x / y
if (x^y) < 0 && (q*y != x) {
q = q - 1
}
return
}
func floorMod(x, y int64) (r int64) {
r = x % y
if (x^y) < 0 && r != 0 {
r = r + y
}
return
}
type comparable0[T any] interface {
Compare(T) int
IsZero() bool
}
func comparing1[E any, T comparable0[T]](f func(E) T) func(E, E) int {
return func(a, b E) int {
return f(a).Compare(f(b))
}
}
func comparing[E any, T cmp.Ordered](f func(E) T) func(E, E) int {
return func(a, b E) int {
return cmp.Compare(f(a), f(b))
}
}
func doCompare[E any](a, b E, f ...func(E, E) int) int {
for _, it := range f {
var i = it(a, b)
if i != 0 {
return i
}
}
return 0
}
func compareZero[T interface{ IsZero() bool }](a, b T) int {
if a.IsZero() {
if b.IsZero() {
return 0
}
return -1
}
if b.IsZero() {
return 1
}
return 0
}
func mustValue[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func addExactly(x, y int64) (r int64, overflow bool) {
r = x + y
overflow = ((x ^ r) & (y ^ r)) < 0
return
}
func mulExact(x, y int64) (r int64, overflow bool) {
r = x * y
overflow = ((y != 0) && (r/y != x)) || (x == math.MinInt64 && y == -1)
return
}
func minOf[T interface{ Compare(T) int }](a, b T) T {
if a.Compare(b) <= 0 {
return a
}
return b
}
func maxOf[T interface{ Compare(T) int }](a, b T) T {
if a.Compare(b) >= 0 {
return a
}
return b
}
func localDateTimeToGoTime(ldt LocalDateTime, loc *time.Location) time.Time {
return time.Date(
int(ldt.Year()),
time.Month(ldt.Month()),
ldt.DayOfMonth(),
ldt.Hour(),
ldt.Minute(),
ldt.Second(),
ldt.Nanosecond(),
loc,
)
}
func classifyZoneBoundsMode(
tbBoundsBegin, tbBoundsEnd time.Time,
taBoundsBegin, taBoundsEnd time.Time,
now time.Time,
) int {
inTb := !now.Before(tbBoundsBegin) && now.Before(tbBoundsEnd)
inTa := !now.Before(taBoundsBegin) && now.Before(taBoundsEnd)
switch {
case inTb && inTa:
return zoneOffsetModeOverlap
case !inTb && !inTa:
return zoneOffsetModeGap
default:
return zoneOffsetModeNormal
}
}