| Module | HelperMethods::ClassMethods |
| In: |
lib/swxruby/helper_module.rb
|
# File lib/swxruby/helper_module.rb, line 23
23: def calculate_bytecode_length(bytecode)
24: # Calculate bytecode length *without* counting the init array or init object action
25: bytecode.join.sub(/^(#{ActionCodes::INIT_ARRAY}|#{ActionCodes::INIT_OBJECT})/, '').length/2
26: end
# File lib/swxruby/helper_module.rb, line 28
28: def generate_push_statement(bytecode)
29: unpushed_data = []
30: # Iterate over the bytecode array in reverse and add all of the unpushed
31: # data to 'unpushed_data'
32: bytecode.reverse_each do |bytecode_chunk|
33: if bytecode_chunk.begins_with?('96') || bytecode_chunk.begins_with?('1D') then break else unpushed_data << bytecode_chunk end
34: end
35:
36: # Since we iterated over the bytecode in reverse, unpushed_data is reversed, so
37: # reverse it again before passing it into #calculate_bytecode_length
38: bytecode_length = calculate_bytecode_length(unpushed_data.reverse)
39:
40: # TODO: Replace with constant
41: '96' + integer_to_hexadecimal(bytecode_length, 2)
42: end
# File lib/swxruby/helper_module.rb, line 44
44: def integer_to_hexadecimal(integer, number_of_bytes=1)
45: make_little_endian("%0#{number_of_bytes*2}X" % integer)
46: end
# File lib/swxruby/helper_module.rb, line 48
48: def make_little_endian(hex_string)
49: # split into an array of string pairs
50: # reverse the array and join back into a string
51: pad_string_to_byte_boundary(hex_string).scan(/../).reverse.join
52: end
# File lib/swxruby/helper_module.rb, line 54
54: def pad_string_to_byte_boundary(hex_string)
55: hex_string += '0' if hex_string.length % 2 == 1
56: hex_string
57: end
Returns a string with the length of the passed hex string in bytes padded to display in ‘number_of_bytes’ bytes.
# File lib/swxruby/helper_module.rb, line 61
61: def string_length_in_bytes_hex(string, number_of_bytes)
62: # Divide length in chars by 2 to get length in bytes
63: bytecode_length_in_hex = integer_to_hexadecimal(string.length/2, number_of_bytes)
64: end