Skip to content

Commit

Permalink
Fix IPv4Header#to_ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 8, 2016
1 parent 0c7870e commit c00240a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 0 additions & 1 deletion features/ipv4_header.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Feature: IPv4Header
| destination_ip_address | 4.3.2.1 |
| ip_option | |

@wip
Scenario: convert IPv4 header to Ruby code
When I eval the following Ruby code:
"""ruby
Expand Down
14 changes: 12 additions & 2 deletions lib/pio/ruby_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ def to_ruby
bytes = ''
bit = false
bit_names = []
total_bit_length = 0
field_names.each do |each|
next unless __send__("#{each}?")
if /Bit(\d+)$/ =~ __send__(each).class.to_s
bit_length = Regexp.last_match(1)
bit_length = Regexp.last_match(1).to_i
total_bit_length += bit_length
if bit
bit_names << each
bytes << format("_%0#{bit_length}b", __send__(each))
Expand All @@ -28,11 +30,19 @@ def to_ruby
else
if bit
bytes << ", # #{bit_names.join(', ')}\n"
pack_template << 'n'
if total_bit_length == 8
pack_template << 'C'
elsif total_bit_length == 16
pack_template << 'n'
else
raise
end
total_bit_length = 0
bit_names = []
bit = false
end
list = __send__(each).to_hex
next if list.empty?
bytes << " #{list}, # #{each}\n"
pack_template << 'C' * (list.count(',') + 1)
end
Expand Down

0 comments on commit c00240a

Please sign in to comment.