mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 00:55:11 -07:00
refactor: Advance tokenizer array offset instead of using Array#shift (#1653)
Array#shift would move all the remaining elements of the array, which is slower for larger arrays.
This commit is contained in:
@@ -8,11 +8,15 @@ module Liquid
|
|||||||
@source = source.to_s.to_str
|
@source = source.to_s.to_str
|
||||||
@line_number = line_number || (line_numbers ? 1 : nil)
|
@line_number = line_number || (line_numbers ? 1 : nil)
|
||||||
@for_liquid_tag = for_liquid_tag
|
@for_liquid_tag = for_liquid_tag
|
||||||
|
@offset = 0
|
||||||
@tokens = tokenize
|
@tokens = tokenize
|
||||||
end
|
end
|
||||||
|
|
||||||
def shift
|
def shift
|
||||||
(token = @tokens.shift) || return
|
token = @tokens[@offset]
|
||||||
|
return nil unless token
|
||||||
|
|
||||||
|
@offset += 1
|
||||||
|
|
||||||
if @line_number
|
if @line_number
|
||||||
@line_number += @for_liquid_tag ? 1 : token.count("\n")
|
@line_number += @for_liquid_tag ? 1 : token.count("\n")
|
||||||
@@ -31,7 +35,9 @@ module Liquid
|
|||||||
tokens = @source.split(TemplateParser)
|
tokens = @source.split(TemplateParser)
|
||||||
|
|
||||||
# removes the rogue empty element at the beginning of the array
|
# removes the rogue empty element at the beginning of the array
|
||||||
tokens.shift if tokens[0]&.empty?
|
if tokens[0]&.empty?
|
||||||
|
@offset += 1
|
||||||
|
end
|
||||||
|
|
||||||
tokens
|
tokens
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user