Module: Rlp::Util
Instance Method Summary collapse
-
#big_endian_to_int(str) ⇒ Integer
An unpacked integer number.
-
#bin_to_hex(bin) ⇒ String
A hexa-decimal string.
-
#deserialize_big_endian_to_int(str) ⇒ Integer
Deserializes big endian data string to integer.
-
#hex_to_bin(hex) ⇒ String
‘0x`-prefixed strings.
-
#int_to_big_endian(num) ⇒ String
Packed, big-endian integer string.
-
#is_bytes?(str) ⇒ Boolean
True if it’s an ASCII-8bit encoded byte-string.
-
#is_hex?(str) ⇒ String
Checks if a string is hex-adecimal (string).
-
#is_list?(item) ⇒ Boolean
Checks if the given item is a list.
-
#is_prefixed?(hex) ⇒ String
(also: #is_hex_prefixed?, #start_with_0x?)
A match if true; ‘nil` if not.
-
#is_primitive?(item) ⇒ Boolean
True if it’s a string primitive.
-
#str_to_bytes(str) ⇒ Object
The string bytes.
-
#strip_hex_prefix(hex) ⇒ String
(also: #remove_hex_prefix, #strip_0x)
An unprefixed hex-string.
Instance Method Details
#big_endian_to_int(str) ⇒ Integer
Returns 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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
21 22 23 |
# File 'lib/rlp-lite/util.rb', line 21 def strip_hex_prefix(hex) is_prefixed?( hex ) ? hex[2..-1] : hex end |