Class: Amp::Core::Support::HexString

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.sha1(str) ⇒ Object



42
43
44
# File 'lib/amp-core/support/hex_string.rb', line 42

def self.sha1(str)
  HexString.from_bin(Digest::SHA1.new.update(str).digest)
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

Returns:

  • (Boolean)


81
82
83
# File 'lib/amp-core/support/hex_string.rb', line 81

def empty?
  to_bin.size == 0
end

#hexlifyObject

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

#ordFixnum, 0 <= x < 256] The value of the first byte.

Returns the value of the first byte of the string.

Returns:

  • (Fixnum, 0 <= x < 256] The value of the first byte.)

    Fixnum, 0 <= x < 256] The value of the first byte.



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_binObject

Return raw binary data



59
60
61
# File 'lib/amp-core/support/hex_string.rb', line 59

def to_bin
  @binary ||= unhexlify
end

#to_hexObject 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

#unhexlifyString

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.

Examples:

StringUtils.unhexlify(“DEADBEEF”) #=> “336255276357”

Returns:

  • (String)

    the string decoded from hex form



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