require 'ipaddr'


TESTCASES = [,                ,
                ,                      ,
                , ]                            

output = [%w(String Address Port Family Hex),
          %w(------ ------- ---- ------ ---)]

def output_table(rows)
  widths = []
  rows.each {|row| row.each_with_index {|col, i| widths[i] = [widths[i].to_i, col.to_s.length].max }}
  format = widths.map {|size| }.join()
  rows.each {|row| puts format % row}
end

TESTCASES.each do |str|
  case str  
  when /\A\[(?<address> .* )\]:(?<port> \d+ )\z/x      
    address, port = $~[:address], $~[:port]
  when /\A(?<address> [^:]+ ):(?<port> \d+ )\z/x       
    address, port = $~[:address], $~[:port]
  else                                                 
    address, port = str, nil
  end

  ip_addr = IPAddr.new(address) 
  family =  if ip_addr.ipv4?
  family =  if ip_addr.ipv6?

  output << [str, ip_addr.to_s, port.to_s, family, ip_addr.to_i.to_s(16)]
end

output_table(output)