Test Failed
Push — master ( 2f8cd9...4364b8 )
by iLex
01:33
created
Severity
1
package go_hkid
0 ignored issues
show
don't use an underscore in package name
Loading history...
2
3
import (
4
	"regexp"
5
	"strconv"
6
	"strings"
7
)
8
9
func clearString(s string) string {
10
	newString := strings.Trim(s, " ")
11
	newString = strings.ToUpper(newString)
12
	return newString
13
}
14
15
func getRemainder(hkid *Hkid) string {
16
	charSum := getCharSum(hkid.part1)
17
	hkidSum := calPart2Remainder(hkid.part2, charSum)
18
19
	remainder := ""
20
21
	switch hkidSum {
22
	case 11:
23
		remainder = "0"
24
	case 10:
25
		remainder = "A"
26
	default:
27
		remainder = strconv.Itoa(hkidSum)
28
	}
29
30
	return remainder
31
}
32
33
func calPart2Remainder(s string, i int) int {
34
	sum := 0
35
36
	for k, v := range s {
37
		i, _ := strconv.Atoi(string(v))
38
		sum += (7 - k) * i
39
	}
40
41
	mod := 11
42
	return mod - ((i + sum) % mod)
43
}
44
45
func getCharSum(part1 string) int {
46
	charMap := getCharMap()
47
	if len(part1) == 1 {
48
		return 324 + charMap[part1]*8
49
	}
50
51
	total := 0
52
	weight := getWeight()
53
	for k, v := range part1 {
54
		total += weight[k] * charMap[string(v)]
55
	}
56
	return total
57
}
58
59
func getWeight() map[int]int {
60
	weight := make(map[int]int)
61
	weight[0] = 9
62
	weight[1] = 8
63
	return weight
64
}
65
66
func getCharMap() map[string]int {
67
	asciiNum := 65 // Uppercase A
68
	idCharMap := make(map[string]int)
69
70
	for i := 0; i <= 26; i++ {
71
		idCharMap[string(i+asciiNum)] = 10 + i
72
	}
73
74
	return idCharMap
75
}
76
77
func validatePatten(string string) (*Hkid, error) {
78
	r, _ := regexp.Compile("^(?P<p1>\\D{1,2})(?P<p2>\\d{6})\\((?P<p3>[\\w{1}0-9aA])\\)$")
79
	match := r.FindStringSubmatch(string)
80
	if len(match) == 0 {
81
		return NewHkidNil(), NewPatterNotMatchError()
82
	}
83
	return NewHkid(match[1], match[2], match[3]), nil
84
}
85