-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
125 lines (109 loc) · 2.68 KB
/
utils.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
package cobranca
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
type PadType string
const (
StrPadLeft PadType = "str_pad_left"
StrPadRight PadType = "str_pad_right"
)
var (
transliterations = map[string]*regexp.Regexp{
"A": regexp.MustCompile(`À|Á|Â|Ã|Ä|à|á|â|ã|ä`),
"AA": regexp.MustCompile(`Å|å`),
"AE": regexp.MustCompile(`Æ|æ`),
"C": regexp.MustCompile(`Ç|ç`),
"E": regexp.MustCompile(`È|É|Ê|Ë|è|é|ê|ë`),
"D": regexp.MustCompile(`Ð|ð`),
"I": regexp.MustCompile(`Ì|Í|Î|Ï|ì|í|î|ï`),
"L": regexp.MustCompile(`Ł|ł`),
"N": regexp.MustCompile(`Ñ|ñ|ń`),
"O": regexp.MustCompile(`Ò|Ó|Ô|Õ|Ö|ò|ó|ô|õ|ö|ō`),
"OE": regexp.MustCompile(`Œ|Ø|œ|ø`),
"Th": regexp.MustCompile(`Þ`),
"U": regexp.MustCompile(`Ù|Ú|Û|Ü|ù|ú|û|ü|ũ|ū|ŭ|ů|ű|ų`),
"Y": regexp.MustCompile(`Ý|ý|ÿ`),
"S": regexp.MustCompile(`ś`),
"SS": regexp.MustCompile(`ß`),
"Z": regexp.MustCompile(`ż`),
"TH": regexp.MustCompile(`þ`),
"% ": regexp.MustCompile(`%`),
}
)
func Brancos(s string, l int) string {
s = Sanitize(s)
if len(s) > l {
return s[0:l]
}
return StrPad(s, l, " ", StrPadRight)
}
func BrancosLeft(s string, l int) string {
s = Sanitize(s)
if len(s) > l {
return s[len(s)-l:]
}
return StrPad(s, l, " ", StrPadLeft)
}
func Date(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
func OnlyNumbers(s string) string {
reg, _ := regexp.Compile("[^0-9]+")
return reg.ReplaceAllString(s, "")
}
// Sanitize replaces non-ASCII characters with an ASCII approximation, or if none exists, to “?”.
func Sanitize(word string) string {
for repl, regex := range transliterations {
word = regex.ReplaceAllString(word, repl)
}
var safe string
for _, r := range word {
if isAscii(r) {
safe += string(r)
} else {
safe += "?"
}
}
return strings.ToUpper(safe)
}
func isAscii(s rune) bool {
return int(s) >= 32 && int(s) <= 126
}
func SemMascara(s string) string {
s = strings.Replace(s, " ", "", -1)
s = strings.Replace(s, ".", "", -1)
s = strings.Replace(s, "-", "", -1)
s = strings.Replace(s, "/", "", -1)
return s
}
func StrPad(s string, padLen int, padStr string, padType PadType) string {
LenS := len(s)
if LenS == padLen {
return s
}
if LenS > padLen {
if padType == StrPadLeft {
return s[LenS-padLen:]
} else {
return s[0:padLen]
}
}
c := (padLen - len(s)) / len(padStr)
r := strings.Repeat(padStr, c)
if padType == StrPadLeft {
return r + s
} else {
return s + r
}
}
func Zeros(s string, l int) string {
if len(s) > l {
return s[len(s)-l:]
}
f := "%0" + strconv.Itoa(l) + "s"
return fmt.Sprintf(f, s)
}