-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
99 lines (86 loc) · 1.71 KB
/
utility.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
package csvutil
import (
"encoding/csv"
"io"
"math/rand"
"strings"
"golang.org/x/text/encoding/japanese"
)
var halfWidthNums = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-"}
var fullWidthNums = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-"}
func reader(r io.Reader, enc string) (*csv.Reader, bool) {
if enc == "sjis" {
return NewReaderWithEnc(r, japanese.ShiftJIS), false
} else if enc == "eucjp" {
return NewReaderWithEnc(r, japanese.EUCJP), false
}
return NewReader(r)
}
func writer(w io.Writer, bom bool, enc string) *csv.Writer {
if enc == "sjis" {
return NewWriterWithEnc(w, japanese.ShiftJIS)
} else if enc == "eucjp" {
return NewWriterWithEnc(w, japanese.EUCJP)
}
return NewWriter(w, bom)
}
func isDigit(s string) bool {
if s == "" {
return false
}
for _, b := range []byte(s) {
if b < 0x30 || 0x39 < b {
return false
}
}
return true
}
func isEmptyOrDigit(s string) bool {
if s == "" {
return true
}
return isDigit(s)
}
func lot(n int) bool {
if n == 100 {
return true
}
if n == 0 {
return false
}
return rand.Intn(100) < n
}
func sampleString(ss []string) string {
return ss[rand.Intn(len(ss))]
}
func containsString(ss []string, s string) bool {
for _, s2 := range ss {
if s2 == s {
return true
}
}
return false
}
func containsInt(is []int, i int) bool {
for _, i2 := range is {
if i2 == i {
return true
}
}
return false
}
func containsRune(rs []rune, r rune) bool {
for _, r2 := range rs {
if r2 == r {
return true
}
}
return false
}
func toFullWidthNum(s string) string {
fs := s
for i, hn := range halfWidthNums {
fs = strings.Replace(fs, hn, fullWidthNums[i], -1)
}
return fs
}