-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
84 lines (68 loc) · 1.47 KB
/
helper.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
package hkid
import (
"regexp"
"strconv"
"strings"
)
func clearString(s string) string {
newString := strings.Trim(s, " ")
newString = strings.ToUpper(newString)
return newString
}
func getRemainder(hkid *hkid) string {
charSum := getCharSum(hkid.part1)
hkidSum := calPart2Remainder(hkid.part2, charSum)
remainder := ""
switch hkidSum {
case 11:
remainder = "0"
case 10:
remainder = "A"
default:
remainder = strconv.Itoa(hkidSum)
}
return remainder
}
func calPart2Remainder(s string, i int) int {
sum := 0
for k, v := range s {
i, _ := strconv.Atoi(string(v))
sum += (7 - k) * i
}
mod := 11
return mod - ((i + sum) % mod)
}
func getCharSum(part1 string) int {
charMap := getCharMap()
if len(part1) == 1 {
return 324 + charMap[part1]*8
}
total := 0
weight := getWeight()
for k, v := range part1 {
total += weight[k] * charMap[string(v)]
}
return total
}
func getWeight() map[int]int {
weight := make(map[int]int)
weight[0] = 9
weight[1] = 8
return weight
}
func getCharMap() map[string]int {
asciiNum := 65 // Uppercase A
idCharMap := make(map[string]int)
for i := 0; i <= 26; i++ {
idCharMap[string(i+asciiNum)] = 10 + i
}
return idCharMap
}
func validatePatten(string string) (*hkid, error) {
r, _ := regexp.Compile(`^(?P<p1>\D{1,2})(?P<p2>\d{6})\((?P<p3>[\w{1}0-9aA])\)$`)
match := r.FindStringSubmatch(string)
if len(match) == 0 {
return newHkidNil(), newPatterNotMatchError()
}
return newHkid(match[1], match[2], match[3]), nil
}