Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Advance tokenizer array offset instead of using Array#shift #1653

Merged
merged 1 commit into from Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/liquid/tokenizer.rb
Expand Up @@ -8,11 +8,15 @@ def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: f
@source = source.to_s.to_str
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@offset = 0
@tokens = tokenize
end

def shift
(token = @tokens.shift) || return
token = @tokens[@offset]
return nil unless token

@offset += 1

if @line_number
@line_number += @for_liquid_tag ? 1 : token.count("\n")
Expand All @@ -31,7 +35,9 @@ def tokenize
tokens = @source.split(TemplateParser)

# 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
end
Expand Down