Skip to content

Commit

Permalink
use string buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkthorne committed Jul 4, 2019
1 parent 2320b64 commit 8f36c99
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion samples/basic_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ require "../src/qr"

qr_code = QR.encode("this is a smoke test")
qr_code.level = 3
qr_code.print
qr_code.print
78 changes: 42 additions & 36 deletions src/qr/code.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,47 +65,53 @@ module QR
@casesensitive
end

def print
realwidth = (@qrcode.width + @margin * 2)
empty = " "
lowhalf = "\342\226\204"
uphalf = "\342\226\200"
full = "\342\226\210"
white = ""
reset = ""

(@margin / 2).times { realwidth.times { print(full) }; print("\n") }

@qrcode.width.times { |y|
top = (y % 2) == 0
next if !top
row1 = @qrcode.data + y * @qrcode.width
row2 = row1 + @qrcode.width
print(white)

@margin.times { print(full) }

@qrcode.width.times { |x|
if (row1[x] & 1 == 0)
if (top && (row2[x] & 1 == 0))
print(full)
def to_s
String::Builder.build do |builder|
realwidth = (@qrcode.width + @margin * 2)
empty = " "
lowhalf = "\342\226\204"
uphalf = "\342\226\200"
full = "\342\226\210"
white = ""
reset = ""

(@margin / 2).times { realwidth.times { builder << full }; builder << "\n" }

@qrcode.width.times { |y|
top = (y % 2) == 0
next if !top
row1 = @qrcode.data + y * @qrcode.width
row2 = row1 + @qrcode.width
builder << white

@margin.times { builder << full }

@qrcode.width.times { |x|
if (row1[x] & 1 == 0)
if (top && (row2[x] & 1 == 0))
builder << full
else
builder << uphalf
end
elsif (top && (row2[x] & 1 == 0))
builder << lowhalf
else
print(uphalf)
builder << empty
end
elsif (top && (row2[x] & 1 == 0))
print(lowhalf)
else
print(empty)
end
}
}

@margin.times { print(full) }
@margin.times { builder << full }

print(reset)
print("\n")
}
builder << reset
builder << "\n"
}

(@margin / 2).times { realwidth.times { print(full) }; print("\n") }
(@margin / 2).times { realwidth.times { builder << full }; builder << "\n" }
end
end

def print
puts to_s
end

private def ext_qrcode
Expand Down

0 comments on commit 8f36c99

Please sign in to comment.