forked from tn47/goledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
105 lines (91 loc) · 1.85 KB
/
util.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
package api
import (
"bytes"
"fmt"
"hash/crc64"
"strings"
"time"
)
var _ = fmt.Sprintf("dummy")
// Parsecsv parses the input string for comma separated string values and
// return parsed strings.
func Parsecsv(input string) []string {
if input == "" {
return nil
}
ss := strings.Split(input, ",")
var outs []string
for _, s := range ss {
s = strings.Trim(s, " \t\r\n")
if s == "" {
continue
}
outs = append(outs, s)
}
return outs
}
// Maxints return the max value amont numbers.
func Maxints(numbers ...int) int {
maxNum := numbers[0]
for _, item := range numbers {
if maxNum < item {
maxNum = item
}
}
return maxNum
}
// Repeatstr to repeat the string `string` n times and return the same.
func Repeatstr(str string, n int) string {
out := ""
for i := 0; i < n; i++ {
out += str
}
return out
}
func StringSet(xs []string) []string {
// TODO: is there a better algorithm to identify duplicates
ys := make([]string, len(xs))
outer:
for _, x := range xs {
for _, y := range ys {
if x == y {
continue outer
}
}
ys = append(ys, x)
}
return ys
}
func ValidateDate(tm time.Time, year, month, date, hour, min, sec int) bool {
y, m, d := tm.Date()
h, t, s := tm.Clock()
if y != year || m != time.Month(month) || d != date {
return false
} else if h != hour || t != min || s != sec {
return false
}
return true
}
func HasString(xs []string, y string) bool {
for _, x := range xs {
if y == x {
return true
}
}
return false
}
func GetStacktrace(skip int, stack []byte) string {
var buf bytes.Buffer
lines := strings.Split(string(stack), "\n")
for _, call := range lines[skip*2:] {
buf.WriteString(fmt.Sprintf("%s\n", call))
}
return buf.String()
}
var isoCrc64 *crc64.Table
func Crc64(data []byte) uint64 {
return crc64.Checksum(data, isoCrc64)
}
func init() {
isoCrc64 = crc64.MakeTable(crc64.ISO)
}