Skip to content

Commit

Permalink
Fix encoding of UTF-8 strings
Browse files Browse the repository at this point in the history
Signed-off-by: Yauhen Kharuzhy <jekhor@gmail.com>
  • Loading branch information
jekhor committed Apr 29, 2013
1 parent e7801fc commit 79ac6c2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
12 changes: 3 additions & 9 deletions lib/rqrcode/qrcode/qr_8bit_byte.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@ def initialize( data )


def get_length
@data.size
@data.bytesize
end


def write( buffer)

buffer.byte_encoding_start(get_length)

( 0...@data.size ).each do |i|
c = @data[i]
c = c.ord if c.respond_to?(:ord)#String#[] returns single-char string in 1.9, .ord gets ASCII pos
buffer.put( c, 8 )


@data.each_byte do |b|
buffer.put(b, 8)
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions lib/rqrcode/qrcode/qr_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,19 @@ def initialize( string, *args )
if !QRERRORCORRECTLEVEL.has_key?(level)
raise QRCodeArgumentError, "Unknown error correction level `#{level.inspect}`"
end

@data = string
max_size_array = QRAlphanumeric.valid_data?( @data ) ? QRMAXDIGITS[level][:mode_alpha_numk] : QRMAXDIGITS[level][:mode_8bit_byte]
size = options[:size] || smallest_size_for(string, max_size_array)

mode = QRAlphanumeric.valid_data?( @data ) ? :mode_alpha_numk : :mode_8bit_byte

max_size_array = QRMAXDIGITS[level][mode]
size = options[:size] || smallest_size_for(string, max_size_array)

@error_correct_level = QRERRORCORRECTLEVEL[level]
@version = size
@module_count = @version * 4 + QRPOSITIONPATTERNLENGTH
@modules = Array.new( @module_count )
@data_list = QRAlphanumeric.valid_data?( @data ) ? QRAlphanumeric.new( @data ) : QR8bitByte.new( @data )
@data_list = (mode == :mode_alpha_numk) ? QRAlphanumeric.new( @data ) : QR8bitByte.new( @data )
@data_cache = nil
self.make
end
Expand Down Expand Up @@ -367,7 +369,7 @@ def map_data( data, mask_pattern ) #:nodoc:
end

def smallest_size_for(string, max_size_array) #:nodoc:
l = string.length
l = string.bytesize
ver = max_size_array.index{|i| i >= l}
raise QRCodeRunTimeError,"code length overflow. (#{1} digits > any version capacity)" unless ver
ver + 1
Expand Down

0 comments on commit 79ac6c2

Please sign in to comment.