Module: Marshalsea::Marshal::FloatBody

Defined in:
lib/marshalsea/marshal/float_body.rb

Class Method Summary collapse

Class Method Details

.decode(body) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/marshalsea/marshal/float_body.rb', line 10

def decode(body)
  text = body.b
  named = named_value(text)
  return [named, nil] if named

  token = prefix(text)
  tail = text.byteslice(token.bytesize..)
  [value_of(token), tail.empty? ? nil : tail]
end

.named_value(text) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/marshalsea/marshal/float_body.rb', line 20

def named_value(text)
  case text.byteslice(0, text.index(Constants::NUL_BYTE) || text.bytesize)
  when Constants::FLOAT_NAN then Float::NAN
  when Constants::FLOAT_INFINITY then Float::INFINITY
  when Constants::FLOAT_NEGATIVE_INFINITY then -Float::INFINITY
  end
end

.prefix(text) ⇒ Object



28
29
30
31
32
# File 'lib/marshalsea/marshal/float_body.rb', line 28

def prefix(text)
  match = Constants::FLOAT_HEX_PREFIX.match(text) ||
          Constants::FLOAT_DECIMAL_PREFIX.match(text)
  match ? match[0] : ""
end

.value_of(token) ⇒ Object



34
35
36
37
# File 'lib/marshalsea/marshal/float_body.rb', line 34

def value_of(token)
  stripped = token.strip
  stripped.empty? ? 0.0 : Float(stripped)
end