-
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.
Merge pull request #2 from opentoys/f-20240201-chinese
add new module
- Loading branch information
Showing
27 changed files
with
2,905 additions
and
40 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,39 @@ | ||
package idcard | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestRegisterCode(t *testing.T) { | ||
type args struct { | ||
data map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
RegisterCode(tt.args.data) | ||
}) | ||
} | ||
} | ||
|
||
func TestConcatCode(t *testing.T) { | ||
type args struct { | ||
data map[string]string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ConcatCode(tt.args.data) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package idcard | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"math/rand" | ||
"strconv" | ||
|
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package idcard | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCheck(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
var no = Generate(true) | ||
var id = IdCard(no) | ||
fmt.Println(id, id.Check(), id.Parse(), id.Age()) | ||
} | ||
} | ||
package idcard | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestCheck(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
var no = Generate(true) | ||
var id = IdCard(no) | ||
fmt.Println(id, id.Check(), id.Parse(), id.Age()) | ||
} | ||
} |
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,63 @@ | ||
// Fork https://github.com/stek29/base100 | ||
package base100 | ||
|
||
// Licensed under UNLICENSE | ||
// See UNLICENSE provided with this file for details | ||
// For more information, please refer to <http://unlicense.org/> | ||
|
||
const ( | ||
first = 0xf0 | ||
second = 0x9f | ||
|
||
shift = 55 | ||
divisor = 64 | ||
|
||
third = 0x8f | ||
forth = 0x80 | ||
) | ||
|
||
// Encode tranforms bytes into base100 utf-8 encoded string | ||
func Encode(data []byte) string { | ||
result := make([]byte, len(data)*4) | ||
for i, b := range data { | ||
result[i*4+0] = first | ||
result[i*4+1] = second | ||
result[i*4+2] = byte((uint16(b)+shift)/divisor + third) | ||
result[i*4+3] = (b+shift)%divisor + forth | ||
} | ||
return string(result) | ||
} | ||
|
||
// InvalidInputError is returned when Decode fails | ||
type InvalidInputError struct { | ||
message string | ||
} | ||
|
||
func (e InvalidInputError) Error() string { | ||
return e.message | ||
} | ||
|
||
// ErrInvalidLength is returned when length of string being decoded is | ||
// not divisible by four | ||
var ErrInvalidLength = InvalidInputError{"len(data) should be divisible by 4"} | ||
|
||
// ErrInvalidData is returned if data is not a valid base100 string | ||
var ErrInvalidData = InvalidInputError{"data is invalid"} | ||
|
||
// Decode transforms base100 utf-8 encoded string into bytes | ||
func Decode(data string) ([]byte, error) { | ||
if len(data)%4 != 0 { | ||
return nil, ErrInvalidLength | ||
} | ||
|
||
result := make([]byte, len(data)/4) | ||
for i := 0; i != len(data); i += 4 { | ||
if data[i+0] != first || data[i+1] != second { | ||
return nil, ErrInvalidData | ||
} | ||
|
||
result[i/4] = (data[i+2]-third)*divisor + | ||
data[i+3] - forth - shift | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.