summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/skip2/go-qrcode/encoder.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2021-06-16 21:00:49 +0200
committerGitHub <noreply@github.com>2021-06-16 21:00:49 +0200
commitfb5a84212c491332504fcdd4b5f4a4b97705d2e3 (patch)
treebe03e973d30d040a49a90c47f1e6b73ed8922539 /vendor/github.com/skip2/go-qrcode/encoder.go
parentdedc1c45a113d8db373d9e4638f54a0f9d29624f (diff)
downloadmatterbridge-msglm-fb5a84212c491332504fcdd4b5f4a4b97705d2e3.tar.gz
matterbridge-msglm-fb5a84212c491332504fcdd4b5f4a4b97705d2e3.tar.bz2
matterbridge-msglm-fb5a84212c491332504fcdd4b5f4a4b97705d2e3.zip
Update dependencies (#1521)
Diffstat (limited to 'vendor/github.com/skip2/go-qrcode/encoder.go')
-rw-r--r--vendor/github.com/skip2/go-qrcode/encoder.go35
1 files changed, 33 insertions, 2 deletions
diff --git a/vendor/github.com/skip2/go-qrcode/encoder.go b/vendor/github.com/skip2/go-qrcode/encoder.go
index 713378c0..6a809cfe 100644
--- a/vendor/github.com/skip2/go-qrcode/encoder.go
+++ b/vendor/github.com/skip2/go-qrcode/encoder.go
@@ -172,7 +172,7 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
}
// Classify data into unoptimised segments.
- d.classifyDataModes()
+ highestRequiredMode := d.classifyDataModes()
// Optimise segments.
err := d.optimiseDataModes()
@@ -180,6 +180,25 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
return nil, err
}
+ // Check if a single byte encoded segment would be more efficient.
+ optimizedLength := 0
+ for _, s := range d.optimised {
+ length, err := d.encodedLength(s.dataMode, len(s.data))
+ if err != nil {
+ return nil, err
+ }
+ optimizedLength += length
+ }
+
+ singleByteSegmentLength, err := d.encodedLength(highestRequiredMode, len(d.data))
+ if err != nil {
+ return nil, err
+ }
+
+ if singleByteSegmentLength <= optimizedLength {
+ d.optimised = []segment{segment{dataMode: highestRequiredMode, data: d.data}}
+ }
+
// Encode data.
encoded := bitset.New()
for _, s := range d.optimised {
@@ -192,9 +211,15 @@ func (d *dataEncoder) encode(data []byte) (*bitset.Bitset, error) {
// classifyDataModes classifies the raw data into unoptimised segments.
// e.g. "123ZZ#!#!" =>
// [numeric, 3, "123"] [alphanumeric, 2, "ZZ"] [byte, 4, "#!#!"].
-func (d *dataEncoder) classifyDataModes() {
+//
+// Returns the highest data mode needed to encode the data. e.g. for a mixed
+// numeric/alphanumeric input, the highest is alphanumeric.
+//
+// dataModeNone < dataModeNumeric < dataModeAlphanumeric < dataModeByte
+func (d *dataEncoder) classifyDataModes() dataMode {
var start int
mode := dataModeNone
+ highestRequiredMode := mode
for i, v := range d.data {
newMode := dataModeNone
@@ -217,9 +242,15 @@ func (d *dataEncoder) classifyDataModes() {
mode = newMode
}
+
+ if newMode > highestRequiredMode {
+ highestRequiredMode = newMode
+ }
}
d.actual = append(d.actual, segment{dataMode: mode, data: d.data[start:len(d.data)]})
+
+ return highestRequiredMode
}
// optimiseDataModes optimises the list of segments to reduce the overall output