Module: Rlp::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/rlp-lite/util.rb

Instance Method Summary collapse

Instance Method Details

#big_endian_to_int(str) ⇒ Integer

Returns an unpacked integer number.

Parameters:

  • str (String)

    big endian to be converted.

Returns:

  • (Integer)

    an unpacked integer number.



94
95
96
# File 'lib/rlp-lite/util.rb', line 94

def big_endian_to_int(str)
  str.unpack("H*").first.to_i(16)
end

#bin_to_hex(bin) ⇒ String

Returns a hexa-decimal string.

Parameters:

  • bin (String)

    a binary string to be unpacked.

Returns:

  • (String)

    a hexa-decimal string.

Raises:

  • (TypeError)

    if value is not a string.



54
55
56
57
# File 'lib/rlp-lite/util.rb', line 54

def bin_to_hex(bin)
  raise TypeError, "Value must be an instance of String" unless bin.instance_of? String
  bin.unpack("H*").first
end

#deserialize_big_endian_to_int(str) ⇒ Integer

Deserializes big endian data string to integer.

Parameters:

  • str (String)

    serialized big endian integer string.

Returns:

  • (Integer)

    an deserialized unsigned integer.



102
103
104
# File 'lib/rlp-lite/util.rb', line 102

def deserialize_big_endian_to_int(str)
  Sedes.big_endian_int.deserialize str.sub(/\A(\x00)+/, "")
end

#hex_to_bin(hex) ⇒ String

‘0x`-prefixed strings.

Parameters:

  • hex (String)

    a hexa-decimal string to be packed.

Returns:

  • (String)

    a packed binary string.

Raises:

  • (TypeError)

    if value is not a string or string is not hex.



42
43
44
45
46
47
# File 'lib/rlp-lite/util.rb', line 42

def hex_to_bin( hex )
  raise TypeError, "Value must be an instance of String" unless hex.instance_of? String
  hex = remove_hex_prefix( hex )
  raise TypeError, "Non-hexadecimal digit found" unless is_hex? hex
  [hex].pack("H*")
end

#int_to_big_endian(num) ⇒ String

Returns packed, big-endian integer string.

Parameters:

  • num (Integer)

    integer to be converted.

Returns:

  • (String)

    packed, big-endian integer string.



82
83
84
85
86
# File 'lib/rlp-lite/util.rb', line 82

def int_to_big_endian(num)
  hex = num.to_s(16) unless is_hex? num
  hex = "0#{hex}" if hex.size.odd?
  hex_to_bin hex
end

#is_bytes?(str) ⇒ Boolean

Returns true if it’s an ASCII-8bit encoded byte-string.

Parameters:

  • str (String)

    a string to check.

Returns:

  • (Boolean)

    true if it’s an ASCII-8bit encoded byte-string.



72
73
74
# File 'lib/rlp-lite/util.rb', line 72

def is_bytes?(str)
  str && str.instance_of?(String) && str.encoding.name == 'ASCII-8BIT'
end

#is_hex?(str) ⇒ String

Returns a match if true; ‘nil` if not.

Parameters:

  • str (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; ‘nil` if not.



13
14
15
16
17
# File 'lib/rlp-lite/util.rb', line 13

def is_hex?(str)
  return false unless str.is_a?( String )
  str = remove_hex_prefix( str )
  str.match /\A[0-9a-fA-F]*\z/
end

#is_list?(item) ⇒ Boolean

Checks if the given item is a list.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it’s a list.



119
120
121
# File 'lib/rlp-lite/util.rb', line 119

def is_list?( item )
  !is_primitive?(item) && item.respond_to?(:each)
end

#is_prefixed?(hex) ⇒ String

Returns a match if true; ‘nil` if not.

Parameters:

  • hex (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; ‘nil` if not.



32
33
34
# File 'lib/rlp-lite/util.rb', line 32

def is_prefixed?(hex)
  hex.match /\A0x/
end

#is_primitive?(item) ⇒ Boolean

Returns true if it’s a string primitive.

Parameters:

  • item (Object)

    the item to check.

Returns:

  • (Boolean)

    true if it’s a string primitive.



111
112
113
# File 'lib/rlp-lite/util.rb', line 111

def is_primitive?( item )
  item.instance_of?(String)
end

#remove_hex_prefix(hex) ⇒ String

Returns an unprefixed hex-string.

Parameters:

  • hex (String)

    a prefixed hex-string.

Returns:

  • (String)

    an unprefixed hex-string.



23
24
25
26
# File 'lib/rlp-lite/util.rb', line 23

def remove_hex_prefix(hex)
  return hex[2..-1] if is_prefixed?( hex )
  return hex
end

#str_to_bytes(str) ⇒ Object

Returns the string bytes.

Parameters:

  • str (String)

    binary string to be converted.

Returns:

  • (Object)

    the string bytes.



64
65
66
# File 'lib/rlp-lite/util.rb', line 64

def str_to_bytes(str)
  is_bytes?(str) ? str : str.b
end