diff --git a/debug.go b/debug.go index 680111f..8e30560 100644 --- a/debug.go +++ b/debug.go @@ -81,7 +81,7 @@ func debugDrawTo(w io.Writer, mat Matrix) error { // background rectangle(0, 0, width, height, img, color.Gray16{Y: 0xff12}) - mat.Iterate(IterDirection_COLUMN, func(x int, y int, v qrvalue) { + mat.iter(IterDirection_COLUMN, func(x int, y int, v qrvalue) { sx := x*blockWidth + padding sy := y*blockWidth + padding es := (x+1)*blockWidth + padding diff --git a/mask.go b/mask.go index cdee293..97b6b97 100644 --- a/mask.go +++ b/mask.go @@ -75,7 +75,7 @@ func (m *mask) masking() { panic("impossible panic, contact maintainer plz") } - m.mat.Iterate(IterDirection_COLUMN, func(x, y int, s qrvalue) { + m.mat.iter(IterDirection_COLUMN, func(x, y int, s qrvalue) { // skip the function modules if v, _ := m.mat.at(x, y); v.qrtype() != QRType_INIT { _ = m.mat.set(x, y, QRValue_INIT_V0) diff --git a/mask_test.go b/mask_test.go index 2c314de..691a775 100644 --- a/mask_test.go +++ b/mask_test.go @@ -15,7 +15,7 @@ func TestMask(t *testing.T) { require.NoError(t, err) var stateInitCnt int - qrc.mat.Iterate(IterDirection_COLUMN, func(x, y int, s qrvalue) { + qrc.mat.iter(IterDirection_COLUMN, func(x, y int, s qrvalue) { if s.qrtype() == QRType_INIT { stateInitCnt++ } diff --git a/matrix.go b/matrix.go index ae38922..5fb9559 100644 --- a/matrix.go +++ b/matrix.go @@ -49,7 +49,7 @@ func (m *Matrix) init() { // print to stdout func (m *Matrix) print() { - m.Iterate(IterDirection_ROW, func(x, y int, s qrvalue) { + m.iter(IterDirection_ROW, func(x, y int, s qrvalue) { fmt.Printf("%s ", s) if (x + 1) == m.width { fmt.Println() @@ -96,7 +96,7 @@ func (m *Matrix) set(w, h int, c qrvalue) error { return nil } -// at state qrbool from matrix with position {x, y} +// at state qrvalue from matrix with position {x, y} func (m *Matrix) at(w, h int) (qrvalue, error) { if w >= m.width || w < 0 { return QRValue_INIT_V0, ErrorOutRangeOfW @@ -120,26 +120,28 @@ const ( // Iterate the Matrix with loop direction IterDirection_ROW major or IterDirection_COLUMN major. // IterDirection_COLUMN is recommended. -func (m *Matrix) Iterate(dir iterDirection, f func(int, int, qrvalue)) { +func (m *Matrix) Iterate(direction iterDirection, fn func(x, y int, s QRValue)) { + m.iter(direction, fn) +} + +func (m *Matrix) iter(dir iterDirection, visitFn func(x int, y int, v qrvalue)) { // row direction first if dir == IterDirection_ROW { for h := 0; h < m.height; h++ { for w := 0; w < m.width; w++ { - f(w, h, m.mat[w][h]) + visitFn(w, h, m.mat[w][h]) } } return } // column direction first - if dir == IterDirection_COLUMN { - for w := 0; w < m.width; w++ { - for h := 0; h < m.height; h++ { - f(w, h, m.mat[w][h]) - } + for w := 0; w < m.width; w++ { + for h := 0; h < m.height; h++ { + visitFn(w, h, m.mat[w][h]) } - return } + return } // Row return a row of matrix, cur should be y dimension. diff --git a/matrix_test.go b/matrix_test.go index 86417df..ed524e6 100644 --- a/matrix_test.go +++ b/matrix_test.go @@ -1,7 +1,6 @@ package qrcode import ( - "reflect" "testing" "github.com/stretchr/testify/assert" @@ -41,25 +40,16 @@ func TestMatrix_Copy(t *testing.T) { // change origin _ = m1.set(2, 2, QRValue_DATA_V1) + assert.Equal(t, m2, got) - // compare copy of the matrix - if !reflect.DeepEqual(got, m2) { - t.Errorf("Matrix.Copy() = %v, want %v", got, m2) - t.Fail() - } - - if s, err := m1.at(2, 2); err != nil { - t.Errorf("Matrix.at() = %v, want %v", QRValue_DATA_V1, s) - t.Fail() - } else if s != QRValue_DATA_V1 { - t.Errorf("Matrix.Copy() = %v, want %v", QRValue_DATA_V1, s) - t.Fail() - } + s, err := m1.at(2, 2) + assert.NoError(t, err) + assert.Equal(t, QRValue_DATA_V1, s) - if s, _ := got.at(2, 2); s != QRValue_DATA_V1 { - t.Errorf("Matrix.Copy() = %v, want %v", QRValue_DATA_V1, s) - t.Fail() - } + s, err = got.at(2, 2) + assert.NoError(t, err) + assert.NotEqual(t, QRValue_DATA_V1, s) + assert.Equal(t, QRValue_INIT_V0, s) } //func Test_stateSliceMatched(t *testing.T) { @@ -99,7 +89,7 @@ func TestMatrix_Copy(t *testing.T) { //} // go test -run=NONE -bench Benchmark_Iterate -count 10 > old.txt -// after change to `m.Iterate(IterDirection_COLUMN, rowIteration)` +// after change to `m.iter(IterDirection_COLUMN, rowIteration)` // go test -run=NONE -bench Benchmark_Iterate -count 10 > new.txt // benchstat old.txt new.txt func Benchmark_Iterate(b *testing.B) { @@ -119,8 +109,8 @@ func Benchmark_Iterate(b *testing.B) { } for i := 0; i < b.N; i++ { - //m.Iterate(IterDirection_ROW, rowIteration) - m.Iterate(IterDirection_COLUMN, rowIteration) + //m.iter(IterDirection_ROW, rowIteration) + m.iter(IterDirection_COLUMN, rowIteration) } } diff --git a/matrix_type.go b/matrix_type.go index 434483c..9f97d74 100644 --- a/matrix_type.go +++ b/matrix_type.go @@ -1,5 +1,7 @@ package qrcode +type QRType = qrtype + // qrtype type qrtype uint8 @@ -43,6 +45,16 @@ func (s qrtype) String() string { return "?" } +type QRValue = qrvalue + +func (v QRValue) Type() qrtype { + return v.qrtype() +} + +func (v QRValue) IsSet() bool { + return v.qrbool() +} + // qrvalue represents the value of the matrix, it is composed of the qrtype(7bits) and the value(1bits). // such as: 0b0000,0011 (QRValue_DATA_V1) represents the qrtype is QRType_DATA and the value is 1. type qrvalue uint8 diff --git a/qrcode.go b/qrcode.go index 5cc8cc7..68980cf 100644 --- a/qrcode.go +++ b/qrcode.go @@ -664,7 +664,7 @@ func (q *QRCode) masking() { // all mask patter and check the maskScore choose the lowest mask result func (q *QRCode) xorMask(m *Matrix, mask *mask) { - mask.mat.Iterate(IterDirection_COLUMN, func(x, y int, v qrvalue) { + mask.mat.iter(IterDirection_COLUMN, func(x, y int, v qrvalue) { // skip the empty place if v.qrtype() == QRType_INIT { return diff --git a/utilities.go b/utilities.go index 45b2966..5ec026a 100644 --- a/utilities.go +++ b/utilities.go @@ -3,7 +3,7 @@ package qrcode // samestate judge two matrix qrtype is same with binary semantic. // QRValue_DATA_V0/QRType_INIT only equal to QRValue_DATA_V0, other state are equal to each other. func samestate(s1, s2 qrvalue) bool { - return s1.qrbool() && s2.qrbool() + return s1.qrbool() == s2.qrbool() } func abs(x int) int {