Method: CRC::Utils#export_table

Defined in:
lib/crc/_utils.rb

#export_table(table, bitsize, linewidth, indentsize = 2) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/crc/_utils.rb', line 82

def export_table(table, bitsize, linewidth, indentsize = 2)
  bitsize0 = bitsize.to_i
  indent = " " * indentsize.to_i
  case
  when bitsize0 > 64 || bitsize0 < 1
    raise "invalid bitsize (expected to 1..64, but given #{bitsize})"
  when bitsize0 > 32
    packformat = "Q>"
    hexwidth = 16
  when bitsize0 > 16
    packformat = "N"
    hexwidth = 8
  when bitsize0 > 8
    packformat = "n"
    hexwidth = 4
  else # when bitsize0 > 0
    packformat = "C"
    hexwidth = 2
  end
  table = table.to_a.pack("#{packformat}*").unpack("H*")[0]
  table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}{#{linewidth}}+$)/, "\n")
  table.gsub!(/(?<=\w)(?=\w{#{hexwidth}}+$)/, " ")
  table.gsub!(/(?<=\w)(?=\s|$)/, ",")
  table.gsub!(/(?:(?<=^)|(?<=\s))(?=\w)/, "0x")
  table.gsub!(/^/, "#{indent}  ")
  <<-EOS
#{indent}TABLE = [
#{table}
#{indent}].freeze
  EOS
end