Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: implementing kanji encoding mode #108

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const (
EncModeAlphanumeric
// EncModeByte mode ...
EncModeByte
// EncModeJP mode ...
EncModeJP
// EncModeKanji mode ...
EncModeKanji
)

// WithEncodingMode sets the encoding mode.
Expand Down
253 changes: 253 additions & 0 deletions chardet.go

Large diffs are not rendered by default.

339 changes: 339 additions & 0 deletions chardet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,339 @@
package qrcode

import (
"testing"
)

func Test_analyzeNum(t *testing.T) {
type args struct {
byt rune
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case 0",
args: args{byt: '0'},
want: true,
},
{
name: "case 1",
args: args{byt: 'a'},
want: false,
},
{
name: "case 2",
args: args{byt: 'A'},
want: false,
},
{
name: "case 3",
args: args{byt: '9'},
want: true,
},
{
name: "case 4",
args: args{byt: '*'},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := analyzeNum(tt.args.byt); got != tt.want {
t.Errorf("analyzeNum() = %v, want %v", got, tt.want)
}
})
}
}

func Test_analyzeAlphanum(t *testing.T) {
type args struct {
byt rune
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case 0",
args: args{byt: '0'},
want: true,
},
{
name: "case 1",
args: args{byt: 'a'},
want: false,
},
{
name: "case 2",
args: args{byt: 'A'},
want: true,
},
{
name: "case 3",
args: args{byt: '9'},
want: true,
},
{
name: "case 4",
args: args{byt: '*'},
want: true,
},
{
name: "case 5",
args: args{byt: '?'},
want: false,
},
{
name: "case 6",
args: args{byt: '&'},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := analyzeAlphaNum(tt.args.byt); got != tt.want {
t.Errorf("analyzeAlphaNum() = %v, want %v", got, tt.want)
}
})
}
}

func Test_analyzeByte(t *testing.T) {
type args struct {
byt rune
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case 0",
args: args{byt: '0'},
want: true,
},
{
name: "case 1",
args: args{byt: 'a'},
want: true,
},
{
name: "case 2",
args: args{byt: 'A'},
want: true,
},
{
name: "case 3",
args: args{byt: '9'},
want: true,
},
{
name: "case 4",
args: args{byt: '*'},
want: true,
},
{
name: "case 5",
args: args{byt: '?'},
want: true,
},
{
name: "case 6",
args: args{byt: '&'},
want: true,
},
{
name: "case 7",
args: args{byt: 'Ö'},
want: true,
},
{
name: "case 8",
args: args{byt: 'に'},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := analyzeByte(tt.args.byt); got != tt.want {
t.Errorf("analyzeByte() = %v, want %v", got, tt.want)
}
})
}
}

func Test_analyzeJP(t *testing.T) {
type args struct {
r rune
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case 0",
args: args{r: '0'},
want: false,
},
{
name: "case 1",
args: args{r: 'a'},
want: false,
},
{
name: "case 2",
args: args{r: 'A'},
want: false,
},
{
name: "case 3",
args: args{r: '9'},
want: false,
},
{
name: "case 4",
args: args{r: '*'},
want: false,
},
{
name: "case 5",
args: args{r: '?'},
want: false,
},
{
name: "case 6",
args: args{r: '&'},
want: false,
},
{
name: "case 7",
args: args{r: 'Ö'},
want: false,
},
{
name: "case 8",
args: args{r: 'に'},
want: true,
},
{
name: "case 9",
args: args{r: '茗'},
want: true,
},
{
name: "case 10",
args: args{r: '杆'},
want: true,
},
{
name: "case 11",
args: args{r: '荷'},
want: true,
},
{
name: "case 12",
args: args{r: '杠'},
want: true,
},
{
name: "case 13",
args: args{r: '杙'},
want: true,
},
{
name: "case 14",
args: args{r: '杣'},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := analyzeJP(tt.args.r); got != tt.want {
t.Errorf("analyzeJP(%c=0x%x) = %v, want %v", tt.args.r, tt.args.r, got, tt.want)
}
})
}
}

func Test_analyzeMode(t *testing.T) {
type args struct {
raw string
}
tests := []struct {
name string
args args
want encMode
wantErr bool
}{
{
name: "case 0",
args: args{raw: "123120899231"},
want: EncModeNumeric,
wantErr: false,
},
{
name: "case 1",
args: args{raw: ":/1231H208*99231FBJO"},
want: EncModeAlphanumeric,
wantErr: false,
},
{
name: "case 2",
args: args{raw: "hahah1298312hG&^FBJO@jhgG*"},
want: EncModeByte,
},
{
name: "case 3",
args: args{raw: "JKAHDOIANKQOIHCMJKASJ"},
want: EncModeAlphanumeric,
wantErr: false,
},
{
name: "case 4",
args: args{raw: "https://baidu.com?keyword=_JSO==GA"},
want: EncModeByte,
wantErr: false,
},
{
name: "case 5",
args: args{raw: "茗荷"},
want: EncModeKanji,
wantErr: false,
},
{
name: "case 6 (swedish letter)",
args: args{raw: "Övrigt aksldjlk Övrigt should JP encMode?"},
want: EncModeByte,
},
{
name: "case 7 (japanese letter)",
args: args{raw: "嵋嶄"},
want: EncModeKanji,
wantErr: false,
},
{
name: "issue#28 alphanum mode does not support lower case letter",
args: args{raw: "a"},
want: EncModeByte,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := analyzeEncodeModeFromRaw(tt.args.raw)
if (err != nil) != tt.wantErr {
t.Errorf("analyzeMode() error = %v, wantErr %v", err, tt.wantErr)
return
}

if got != tt.want {
t.Errorf("analyzeMode() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions cmd/wasm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (o *genOption) encodeOptions() []qrcode.EncodeOption {
out = append(out, qrcode.WithEncodingMode(qrcode.EncModeNumeric))
case uint8(qrcode.EncModeByte):
out = append(out, qrcode.WithEncodingMode(qrcode.EncModeByte))
case uint8(qrcode.EncModeJP):
out = append(out, qrcode.WithEncodingMode(qrcode.EncModeJP))
case uint8(qrcode.EncModeKanji):
out = append(out, qrcode.WithEncodingMode(qrcode.EncModeKanji))
}

switch o.encodeOption.ecLevel {
Expand Down
Loading
Loading