Skip to content

Commit

Permalink
Replace bytes.Buffer with strings.Builder for string concat
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
Jacalz committed Oct 30, 2023
1 parent e38b4ad commit 2377abe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions qrcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"io"
"log"
"os"
"strings"

"github.com/rymdport/go-qrcode/bitset"
"github.com/rymdport/go-qrcode/reedsolomon"
Expand Down Expand Up @@ -527,55 +528,55 @@ func (q *QRCode) addPadding() {
// ToString produces a multi-line string that forms a QR-code image.
func (q *QRCode) ToString(inverseColor bool) string {
bits := q.Bitmap()
var buf bytes.Buffer
var builder strings.Builder
for y := range bits {
for x := range bits[y] {
if bits[y][x] != inverseColor {
buf.WriteString(" ")
builder.WriteString(" ")
} else {
buf.WriteString("██")
builder.WriteString("██")
}
}
buf.WriteString("\n")
builder.WriteString("\n")
}
return buf.String()
return builder.String()
}

// ToSmallString produces a multi-line string that forms a QR-code image, a
// factor two smaller in x and y then ToString.
func (q *QRCode) ToSmallString(inverseColor bool) string {
bits := q.Bitmap()
var buf bytes.Buffer
var builder strings.Builder
// if there is an odd number of rows, the last one needs special treatment
for y := 0; y < len(bits)-1; y += 2 {
for x := range bits[y] {
if bits[y][x] == bits[y+1][x] {
if bits[y][x] != inverseColor {
buf.WriteString(" ")
builder.WriteString(" ")
} else {
buf.WriteString("█")
builder.WriteString("█")
}
} else {
if bits[y][x] != inverseColor {
buf.WriteString("▄")
builder.WriteString("▄")
} else {
buf.WriteString("▀")
builder.WriteString("▀")
}
}
}
buf.WriteString("\n")
builder.WriteString("\n")
}
// special treatment for the last row if odd
if len(bits)%2 == 1 {
y := len(bits) - 1
for x := range bits[y] {
if bits[y][x] != inverseColor {
buf.WriteString(" ")
builder.WriteString(" ")
} else {
buf.WriteString("▀")
builder.WriteString("▀")
}
}
buf.WriteString("\n")
builder.WriteString("\n")
}
return buf.String()
return builder.String()
}
2 changes: 1 addition & 1 deletion qrcode_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func zbarimgDecode(q *QRCode) (string, error) {
cmd := exec.Command("zbarimg", "--quiet", "-Sdisable",
"-Sqrcode.enable", "-")

var out bytes.Buffer
var out strings.Builder

cmd.Stdin = bytes.NewBuffer(png)
cmd.Stdout = &out
Expand Down

0 comments on commit 2377abe

Please sign in to comment.