mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 09:05:13 -07:00
Simplify parse_number
We don't need all the multi-dot logic in a world where number comes out of the Lexer.
This commit is contained in:
@@ -57,63 +57,22 @@ module Liquid
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if (num = parse_number(markup, ss))
|
if (num = parse_number(markup))
|
||||||
num
|
num
|
||||||
else
|
else
|
||||||
VariableLookup.parse(markup, ss, cache)
|
VariableLookup.parse(markup, ss, cache)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_number(markup, ss)
|
def parse_number(markup)
|
||||||
# check if the markup is simple integer or float
|
# check if the markup is simple integer or float
|
||||||
case markup
|
case markup
|
||||||
when INTEGER_REGEX
|
when INTEGER_REGEX
|
||||||
return Integer(markup, 10)
|
Integer(markup, 10)
|
||||||
when FLOAT_REGEX
|
when FLOAT_REGEX
|
||||||
return markup.to_f
|
markup.to_f
|
||||||
end
|
|
||||||
|
|
||||||
ss.string = markup
|
|
||||||
# the first byte must be a digit or a dash
|
|
||||||
byte = ss.scan_byte
|
|
||||||
|
|
||||||
return false if byte != DASH && (byte < ZERO || byte > NINE)
|
|
||||||
|
|
||||||
if byte == DASH
|
|
||||||
peek_byte = ss.peek_byte
|
|
||||||
|
|
||||||
# if it starts with a dash, the next byte must be a digit
|
|
||||||
return false if peek_byte.nil? || !(peek_byte >= ZERO && peek_byte <= NINE)
|
|
||||||
end
|
|
||||||
|
|
||||||
# The markup could be a float with multiple dots
|
|
||||||
first_dot_pos = nil
|
|
||||||
num_end_pos = nil
|
|
||||||
|
|
||||||
while (byte = ss.scan_byte)
|
|
||||||
return false if byte != DOT && (byte < ZERO || byte > NINE)
|
|
||||||
|
|
||||||
# we found our number and now we are just scanning the rest of the string
|
|
||||||
next if num_end_pos
|
|
||||||
|
|
||||||
if byte == DOT
|
|
||||||
if first_dot_pos.nil?
|
|
||||||
first_dot_pos = ss.pos
|
|
||||||
else
|
|
||||||
# we found another dot, so we know that the number ends here
|
|
||||||
num_end_pos = ss.pos - 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
num_end_pos = markup.length if ss.eos?
|
|
||||||
|
|
||||||
if num_end_pos
|
|
||||||
# number ends with a number "123.123"
|
|
||||||
markup.byteslice(0, num_end_pos).to_f
|
|
||||||
else
|
else
|
||||||
# number ends with a dot "123."
|
false
|
||||||
markup.byteslice(0, first_dot_pos).to_f
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user