use getbyte dispatch instead of start_with? in parse_for_document

This commit is contained in:
Tobi Lutke
2026-04-04 17:33:53 -07:00
committed by Chris Pak
parent 28f29433e4
commit da57336be9
+18 -3
View File
@@ -175,11 +175,17 @@ module Liquid
end
end
OPEN_CURLEY_BYTE = 123 # '{'.ord
PERCENT_BYTE = 37 # '%'.ord
private def parse_for_document(tokenizer, parse_context, &block)
while (token = tokenizer.shift)
next if token.empty?
case
when token.start_with?(TAGSTART)
first_byte = token.getbyte(0)
if first_byte == OPEN_CURLEY_BYTE
second_byte = token.getbyte(1)
if second_byte == PERCENT_BYTE
whitespace_handler(token, parse_context)
parsed = BlockBody.parse_tag_token(token)
unless parsed
@@ -206,10 +212,19 @@ module Liquid
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
@nodelist << new_tag
when token.start_with?(VARSTART)
elsif second_byte == OPEN_CURLEY_BYTE
whitespace_handler(token, parse_context)
@nodelist << create_variable(token, parse_context)
@blank = false
else
# Fallback: text token starting with '{'
if parse_context.trim_whitespace
token.lstrip!
end
parse_context.trim_whitespace = false
@nodelist << token
@blank &&= token.match?(WhitespaceOrNothing)
end
else
if parse_context.trim_whitespace
token.lstrip!