mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Merge pull request #1477 from Watson1978/performance
Increase parsing performance
This commit is contained in:
@@ -231,8 +231,8 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_variable(token, parse_context)
|
def create_variable(token, parse_context)
|
||||||
token.scan(ContentOfVariable) do |content|
|
if token =~ ContentOfVariable
|
||||||
markup = content.first
|
markup = Regexp.last_match(1)
|
||||||
return Variable.new(markup, parse_context)
|
return Variable.new(markup, parse_context)
|
||||||
end
|
end
|
||||||
BlockBody.raise_missing_variable_terminator(token, parse_context)
|
BlockBody.raise_missing_variable_terminator(token, parse_context)
|
||||||
|
|||||||
+11
-10
@@ -10,21 +10,23 @@ module Liquid
|
|||||||
'empty' => ''
|
'empty' => ''
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
||||||
SINGLE_QUOTED_STRING = /\A\s*'(.*)'\s*\z/m
|
INTEGERS_REGEX = /\A(-?\d+)\z/
|
||||||
DOUBLE_QUOTED_STRING = /\A\s*"(.*)"\s*\z/m
|
FLOATS_REGEX = /\A(-?\d[\d\.]+)\z/
|
||||||
INTEGERS_REGEX = /\A\s*(-?\d+)\s*\z/
|
|
||||||
FLOATS_REGEX = /\A\s*(-?\d[\d\.]+)\s*\z/
|
|
||||||
|
|
||||||
# Use an atomic group (?>...) to avoid pathological backtracing from
|
# Use an atomic group (?>...) to avoid pathological backtracing from
|
||||||
# malicious input as described in https://github.com/Shopify/liquid/issues/1357
|
# malicious input as described in https://github.com/Shopify/liquid/issues/1357
|
||||||
RANGES_REGEX = /\A\s*\(\s*(?>(\S+)\s*\.\.)\s*(\S+)\s*\)\s*\z/
|
RANGES_REGEX = /\A\(\s*(?>(\S+)\s*\.\.)\s*(\S+)\s*\)\z/
|
||||||
|
|
||||||
def self.parse(markup)
|
def self.parse(markup)
|
||||||
|
return nil unless markup
|
||||||
|
|
||||||
|
markup = markup.strip
|
||||||
|
if (markup.start_with?('"') && markup.end_with?('"')) ||
|
||||||
|
(markup.start_with?("'") && markup.end_with?("'"))
|
||||||
|
return markup[1..-2]
|
||||||
|
end
|
||||||
|
|
||||||
case markup
|
case markup
|
||||||
when nil
|
|
||||||
nil
|
|
||||||
when SINGLE_QUOTED_STRING, DOUBLE_QUOTED_STRING
|
|
||||||
Regexp.last_match(1)
|
|
||||||
when INTEGERS_REGEX
|
when INTEGERS_REGEX
|
||||||
Regexp.last_match(1).to_i
|
Regexp.last_match(1).to_i
|
||||||
when RANGES_REGEX
|
when RANGES_REGEX
|
||||||
@@ -32,7 +34,6 @@ module Liquid
|
|||||||
when FLOATS_REGEX
|
when FLOATS_REGEX
|
||||||
Regexp.last_match(1).to_f
|
Regexp.last_match(1).to_f
|
||||||
else
|
else
|
||||||
markup = markup.strip
|
|
||||||
if LITERALS.key?(markup)
|
if LITERALS.key?(markup)
|
||||||
LITERALS[markup]
|
LITERALS[markup]
|
||||||
else
|
else
|
||||||
|
|||||||
Reference in New Issue
Block a user