-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f8cd98
Showing
7 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package go_hkid | ||
|
||
type patterNotMatchError struct { | ||
} | ||
|
||
func NewPatterNotMatchError() *patterNotMatchError { | ||
return &patterNotMatchError{} | ||
} | ||
|
||
func (h patterNotMatchError) Error() string { | ||
return "Patter not match" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module "go_hkid" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package go_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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package go_hkid | ||
|
||
func CheckString(string string) *HkidResult { | ||
hkid, e := validatePatten(string) | ||
if e != nil { | ||
return NewHkidResultWithValid(NewHkidNil(), false) | ||
} | ||
|
||
if getRemainder(hkid) == hkid.part3 { | ||
return NewHkidResultWithValid(hkid, true) | ||
} | ||
|
||
return NewHkidResultWithValid(hkid, false) | ||
} | ||
|
||
func CheckPart(part1 string, part2 string, part3 string) *HkidResult { | ||
hkid := NewHkid(part1, part2, part3) | ||
return CheckString(hkid.Format()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package go_hkid | ||
|
||
type hkidInterface interface { | ||
GetPart1() string | ||
GetPart2() string | ||
GetPart3() string | ||
Format() string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package go_hkid | ||
|
||
type HkidResult struct { | ||
hkid *Hkid | ||
Valid bool | ||
} | ||
|
||
func (h *HkidResult) GetPart1() string { | ||
return h.hkid.GetPart1() | ||
} | ||
|
||
func (h *HkidResult) GetPart2() string { | ||
return h.hkid.GetPart2() | ||
} | ||
|
||
func (h *HkidResult) GetPart3() string { | ||
return h.hkid.GetPart3() | ||
} | ||
|
||
func (h *HkidResult) Format() string { | ||
return h.hkid.Format() | ||
} | ||
|
||
func NewHkidResultWithValid(hkid *Hkid, valid bool) *HkidResult { | ||
return &HkidResult{hkid: hkid, Valid: valid} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package go_hkid | ||
|
||
import "fmt" | ||
|
||
type Hkid struct { | ||
part1 string | ||
part2 string | ||
part3 string | ||
} | ||
|
||
func NewHkidNil() *Hkid { | ||
return &Hkid{} | ||
} | ||
|
||
func NewHkid(part1 string, part2 string, part3 string) *Hkid { | ||
return &Hkid{part1: clearString(part1), part2: part2, part3: clearString(part3)} | ||
} | ||
|
||
func (hkid *Hkid) GetPart1() string { | ||
return hkid.part1 | ||
} | ||
|
||
func (hkid *Hkid) GetPart2() string { | ||
return hkid.part2 | ||
} | ||
|
||
func (hkid *Hkid) GetPart3() string { | ||
return hkid.part3 | ||
} | ||
|
||
func (hkid *Hkid) Format() string { | ||
return fmt.Sprintf("%s%s(%s)", hkid.part1, hkid.part2, hkid.part3) | ||
} |