class Crass::Tokenizer

def convert_string_to_number(str)

4.3.14. http://dev.w3.org/csswg/css-syntax/#convert-a-string-to-a-number

Converts a valid CSS number string into a number and returns the number.
def convert_string_to_number(str)
  matches = RE_NUMBER_STR.match(str)
  s = matches[:sign] == '-' ? -1 : 1
  i = matches[:integer].to_i
  f = matches[:fractional].to_i
  d = matches[:fractional] ? matches[:fractional].length : 0
  t = matches[:exponent_sign] == '-' ? -1 : 1
  e = matches[:exponent].to_i
  # I know this formula looks nutty, but it's exactly what's defined in the
  # spec, and it works.
  value = s * (i + f * 10**-d) * 10**(t * e)
  # Maximum and minimum values aren't defined in the spec, but are enforced
  # here for sanity.
  if value > Float::MAX
    value = Float::MAX
  elsif value < -Float::MAX
    value = -Float::MAX
  end
  value
end