Module: Addr

Includes:
ConDuxml
Defined in:
lib/ipxact/addr.rb

Constant Summary collapse

HEX_POSTFIX =
'h'
HEX_PREFIX =
'0x'
HEX_MAG_DIGS =
4
HEX_SEP =
'_'
DEC_POSTFIX =
'd'
DEC_MAG_DIGS =
3
DEC_SEP =
','
BIN_POSTFIX =
'b'
BIN_PREFIX =
'0b'
BIN_MAG_DIGS =
4
BIN_SEP =
' '

Instance Method Summary collapse

Instance Method Details

#bit_rangeObject



23
24
25
# File 'lib/ipxact/addr.rb', line 23

def bit_range
  [position, position+width]
end

#to_bin(str, opts = {}) ⇒ String

Returns string representation of hex number.

Parameters:

  • str (String)

    string representing number; can be in any base

  • opts (Hash) (defaults to: {})

    options are: :pre => [boolean, String] # if String, notation, by default ‘0b’; hash key indicates position; if true, :post => [boolean, String] # radix notation goes at end e.g. ‘10b’; cannot contradict :pre :sep => [boolean] # adds separator characters (‘_’ for hex) every 4 digits :pad => [Fixnum] # given value indicates total digit width of number e.g. 4 => ‘000Fh’

Returns:

  • (String)

    string representation of hex number



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ipxact/addr.rb', line 59

def to_bin(str, opts={})
  s = str.to_dec.to_s(2)
  s = add_padding(s, 1) if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? BIN_SEP : opts[:sep]
    s = add_separators(s, BIN_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? BIN_POSTFIX : opts[:post]
  end
  if opts[:pre]
    s = (opts[:pre] == true ? BIN_PREFIX : opts[:pre]) + s
  end
  s
end

#to_dec(str, opts = {}) ⇒ String

Returns formatted string.

Parameters:

  • str (String)

    number as string; can be any base or notation

Returns:

  • (String)

    formatted string



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ipxact/addr.rb', line 79

def to_dec(str, opts={})
  s = str.to_dec.to_s
  s = add_padding(s, opts[:pad])     if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? DEC_SEP : opts[:sep]
    s = add_separators(s, DEC_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? DEC_POSTFIX : opts[:post]
  end
  s
end

#to_hex(str, opts = {}) ⇒ String

Returns string representation of hex number.

Parameters:

  • str (String)

    string representing number; can be in any base

  • opts (Hash) (defaults to: {})

    options are: :pre => true # radix notation goes at beginning e.g. ‘0x000F’ - default setting :post => true # radix notation goes at end e.g. ‘Fh’ :sep => true # adds separator characters (‘_’ for hex) every 4 digits :pad => [Fixnum] # given value indicates total digit width of number e.g. 4 => ‘000Fh’

Returns:

  • (String)

    string representation of hex number



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ipxact/addr.rb', line 34

def to_hex(str, opts={})
  s = str.to_dec.to_s(16).upcase
  s = add_padding(s, opts[:pad]) if opts[:pad]

  if opts[:sep]
    separator = opts[:sep] == true ? HEX_SEP : opts[:sep]
    s = add_separators(s, HEX_MAG_DIGS, separator)
  end

  if opts[:post]
    s += opts[:post] == true ? HEX_POSTFIX : opts[:post]
  end
  if opts[:pre]
    s = opts[:pre] == true ? HEX_PREFIX : opts[:pre] + s
  end
  s
end