Skip to content

Commit

Permalink
Merge pull request #2 from opentoys/f-20240201-chinese
Browse files Browse the repository at this point in the history
add new module
  • Loading branch information
alonelucky authored Feb 5, 2024
2 parents c09c925 + ef97a59 commit 1d57196
Show file tree
Hide file tree
Showing 27 changed files with 2,905 additions and 40 deletions.
39 changes: 39 additions & 0 deletions chinese/idcard/area_test.go
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)
})
}
}
1 change: 0 additions & 1 deletion chinese/idcard/idcard.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package idcard

import (
_ "embed"
"fmt"
"math/rand"
"strconv"
Expand Down
28 changes: 14 additions & 14 deletions chinese/idcard/idcard_test.go
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())
}
}
63 changes: 63 additions & 0 deletions encoding/base100/base100.go
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
}
Loading

0 comments on commit 1d57196

Please sign in to comment.