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.



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

def big_endian_to_int( str )
  str.unpack("H*")[0].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.



59
60
61
62
# File 'lib/rlp-lite/util.rb', line 59

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.



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

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.



47
48
49
50
51
52
# File 'lib/rlp-lite/util.rb', line 47

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.



89
90
91
92
93
# File 'lib/rlp-lite/util.rb', line 89

def int_to_big_endian( num )
  hex = num.to_s(16)
  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.



78
79
80
# File 'lib/rlp-lite/util.rb', line 78

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

#is_hex?(str) ⇒ String

Checks if a string is hex-adecimal (string).

Parameters:

  • str (String)

    a string to be checked.

Returns:

  • (String)

    a match if true; ‘nil` if not.



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

def is_hex?( str )
  return false unless str.is_a?( String )
  str = strip_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.



128
129
130
# File 'lib/rlp-lite/util.rb', line 128

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

#is_prefixed?(hex) ⇒ String Also known as: is_hex_prefixed?, start_with_0x?

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.



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

def is_prefixed?(hex)
  ## was: hex.match /\A0x/
  ##   tood/check:  add support for (upcase) 0X too - why? why not?
  hex.start_with?( '0x' ) ||
  hex.start_with?( '0X' )
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.



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

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

#str_to_bytes(str) ⇒ Object

Returns the string bytes.

Parameters:

  • str (String)

    binary string to be converted.

Returns:

  • (Object)

    the string bytes.



69
70
71
# File 'lib/rlp-lite/util.rb', line 69

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

#strip_hex_prefix(hex) ⇒ String Also known as: remove_hex_prefix, strip_0x

Returns an unprefixed hex-string.

Parameters:

  • hex (String)

    a prefixed hex-string.

Returns:

  • (String)

    an unprefixed hex-string.



21
22
23
# File 'lib/rlp-lite/util.rb', line 21

def strip_hex_prefix(hex)
  is_prefixed?( hex ) ? hex[2..-1] : hex
end