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
A match if true; ‘nil` if not.
-
#is_list?(item) ⇒ Boolean
Checks if the given item is a list.
-
#is_prefixed?(hex) ⇒ String
A match if true; ‘nil` if not.
-
#is_primitive?(item) ⇒ Boolean
True if it’s a string primitive.
-
#remove_hex_prefix(hex) ⇒ String
An unprefixed hex-string.
-
#str_to_bytes(str) ⇒ Object
The string bytes.
Instance Method Details
#big_endian_to_int(str) ⇒ Integer
Returns 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
64 65 66 |
# File 'lib/rlp-lite/util.rb', line 64 def str_to_bytes(str) is_bytes?(str) ? str : str.b end |