mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 18:30:40 -07:00
extend fast-path to handle quoted string literal variables (262 more fast-pathed)
This commit is contained in:
+32
-20
@@ -109,30 +109,38 @@ module Liquid
|
|||||||
end
|
end
|
||||||
return false if pos >= len
|
return false if pos >= len
|
||||||
|
|
||||||
# Check first byte: must be identifier start, quote, or digit for fast path
|
|
||||||
b = markup.getbyte(pos)
|
b = markup.getbyte(pos)
|
||||||
|
|
||||||
# Only handle identifier-started expressions (covers ~95% of variables)
|
if b == 39 || b == 34 # single or double quote
|
||||||
return false unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95
|
# Quoted string literal: scan to matching close quote
|
||||||
|
quote = b
|
||||||
# Scan the name portion: [\w-]*(\.[\w-]*)*
|
name_start = pos
|
||||||
name_start = pos
|
pos += 1
|
||||||
pos += 1
|
pos += 1 while pos < len && markup.getbyte(pos) != quote
|
||||||
while pos < len
|
pos += 1 if pos < len # skip closing quote
|
||||||
b = markup.getbyte(pos)
|
name_end = pos
|
||||||
if (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || (b >= 48 && b <= 57) || b == 95 || b == 45
|
elsif (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95
|
||||||
pos += 1
|
# Identifier: scan [\w-]*(\.[\w-]*)*
|
||||||
elsif b == 46 # '.'
|
name_start = pos
|
||||||
pos += 1
|
pos += 1
|
||||||
return false if pos >= len
|
while pos < len
|
||||||
b = markup.getbyte(pos)
|
b = markup.getbyte(pos)
|
||||||
return false unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95
|
if (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || (b >= 48 && b <= 57) || b == 95 || b == 45
|
||||||
pos += 1
|
pos += 1
|
||||||
else
|
elsif b == 46 # '.'
|
||||||
break
|
pos += 1
|
||||||
|
return false if pos >= len
|
||||||
|
b = markup.getbyte(pos)
|
||||||
|
return false unless (b >= 97 && b <= 122) || (b >= 65 && b <= 90) || b == 95
|
||||||
|
pos += 1
|
||||||
|
else
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
name_end = pos
|
||||||
|
else
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
name_end = pos
|
|
||||||
|
|
||||||
# Skip whitespace after name
|
# Skip whitespace after name
|
||||||
while pos < len
|
while pos < len
|
||||||
@@ -146,7 +154,11 @@ module Liquid
|
|||||||
cache = parse_context.expression_cache
|
cache = parse_context.expression_cache
|
||||||
ss = parse_context.string_scanner
|
ss = parse_context.string_scanner
|
||||||
|
|
||||||
if Expression::LITERALS.key?(expr_markup)
|
first_byte = expr_markup.getbyte(0)
|
||||||
|
if first_byte == 39 || first_byte == 34 # quoted string
|
||||||
|
# Strip quotes for string literal
|
||||||
|
@name = expr_markup.byteslice(1, expr_markup.bytesize - 2)
|
||||||
|
elsif Expression::LITERALS.key?(expr_markup)
|
||||||
@name = Expression::LITERALS[expr_markup]
|
@name = Expression::LITERALS[expr_markup]
|
||||||
elsif cache
|
elsif cache
|
||||||
@name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse(expr_markup, ss, cache).freeze)
|
@name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse(expr_markup, ss, cache).freeze)
|
||||||
|
|||||||
Reference in New Issue
Block a user