Class: Amp::Core::Support::HexString
- Inherits:
-
Object
- Object
- Amp::Core::Support::HexString
- Includes:
- Comparable
- Defined in:
- lib/amp-core/support/hex_string.rb
Overview
This module is a set of string functions that we use frequently. They sued to be monkey-patched onto the String class, but we don’t do that anymore.
Class Method Summary collapse
-
.as_binary(s) ⇒ Object
Mainly internal/test helper method; change the encoding in 1.9.
-
.from_bin(bin) ⇒ Object
Construct a HexString.
-
.from_hex(hex) ⇒ Object
Construct a HexString.
- .sha1(str) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #empty? ⇒ Boolean
-
#hexlify ⇒ Object
Converts this text into hex.
-
#initialize(binary_part, hex_part) ⇒ HexString
constructor
Assumes one or the other argument will be nil.
-
#ord ⇒ Fixnum, 0 <= x < 256] The value of the first byte.
Returns the value of the first byte of the string.
-
#to_bin ⇒ Object
Return raw binary data.
-
#to_hex ⇒ Object
(also: #to_s)
Return hexidecimal representation of binary data.
-
#unhexlify ⇒ String
Converts a string of hex into the binary values it represents.
Constructor Details
#initialize(binary_part, hex_part) ⇒ HexString
Assumes one or the other argument will be nil. Mostly an internal constructor
48 49 50 51 |
# File 'lib/amp-core/support/hex_string.rb', line 48 def initialize(binary_part, hex_part) @binary = self.class.as_binary(binary_part) @hex = hex_part end |
Class Method Details
.as_binary(s) ⇒ Object
Mainly internal/test helper method; change the encoding in 1.9
34 35 36 37 38 39 40 |
# File 'lib/amp-core/support/hex_string.rb', line 34 def self.as_binary(s) if s.respond_to?(:force_encoding) s.force_encoding('binary') else s end end |
.from_bin(bin) ⇒ Object
Construct a HexString
24 25 26 |
# File 'lib/amp-core/support/hex_string.rb', line 24 def self.from_bin(bin) HexString === bin ? bin : new(bin, nil) end |
.from_hex(hex) ⇒ Object
Construct a HexString
29 30 31 |
# File 'lib/amp-core/support/hex_string.rb', line 29 def self.from_hex(hex) HexString === hex ? hex : new(nil, hex) end |
Instance Method Details
#<=>(other) ⇒ Object
53 54 55 |
# File 'lib/amp-core/support/hex_string.rb', line 53 def <=>(other) to_s <=> other.to_s end |
#empty? ⇒ Boolean
81 82 83 |
# File 'lib/amp-core/support/hex_string.rb', line 81 def empty? to_bin.size == 0 end |
#hexlify ⇒ Object
Converts this text into hex. each letter is replaced with it’s hex counterpart
73 74 75 76 77 78 79 |
# File 'lib/amp-core/support/hex_string.rb', line 73 def hexlify hex = "" @binary.each_byte do |i| hex << i.to_s(16).rjust(2, "0") end hex end |
#ord ⇒ Fixnum, 0 <= x < 256] The value of the first byte.
Returns the value of the first byte of the string.
89 90 91 92 |
# File 'lib/amp-core/support/hex_string.rb', line 89 def ord raise ArgumentError.new('empty string') if to_bin.empty? to_bin[0] end |
#to_bin ⇒ Object
Return raw binary data
59 60 61 |
# File 'lib/amp-core/support/hex_string.rb', line 59 def to_bin @binary ||= unhexlify end |
#to_hex ⇒ Object Also known as: to_s
Return hexidecimal representation of binary data
64 65 66 |
# File 'lib/amp-core/support/hex_string.rb', line 64 def to_hex @hex ||= hexlify end |
#unhexlify ⇒ String
Converts a string of hex into the binary values it represents. This is used for when we store a node ID in a human-readable format, and need to convert it back.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/amp-core/support/hex_string.rb', line 99 def unhexlify bin = "\000" * (@hex.size/2) c = 0 (0..@hex.size-2).step(2) do |i| byte = @hex[i,2].to_i(16) bin[c] = byte c += 1 end bin end |