From be8e329cef8e5f0bacaa97bdcfa37b3c99e93d24 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Fri, 25 Oct 2024 08:56:59 -0400 Subject: [PATCH] more opt --- lib/liquid/tokenizer.rb | 34 +++++++++++++++++++++++++++------- test/test_helper.rb | 2 +- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/liquid/tokenizer.rb b/lib/liquid/tokenizer.rb index 799c9c5b..0724c28d 100644 --- a/lib/liquid/tokenizer.rb +++ b/lib/liquid/tokenizer.rb @@ -14,6 +14,9 @@ module Liquid CLOSE_CURLEY = "}".ord PERCENTAGE = "%".ord + CLOSE_CURLEY_FOLLOWED_BY_CLOSE_CURLEY = (CLOSE_CURLEY << 8) | CLOSE_CURLEY + OPEN_CURLEY_FOLLOWED_BY_PERCENTAGE = (OPEN_CURLEY << 8) | PERCENTAGE + def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false) @line_number = line_number || (line_numbers ? 1 : nil) @for_liquid_tag = for_liquid_tag @@ -24,18 +27,34 @@ module Liquid def shift return if @ss.eos? - token = @for_liquid_tag ? next_liquid_token : next_token + @for_liquid_tag ? shift_liquid_tag : shift_normal + end + + private + + def shift_normal + token = next_token return nil unless token if @line_number - @line_number += @for_liquid_tag ? 1 : token.count("\n") + @line_number += token.count("\n") end token end - private + def shift_liquid_tag + token = next_liquid_token + + return (@ss = nil) unless token + + if @line_number + @line_number += 1 + end + + token + end def next_liquid_token # read until we find a \n @@ -93,20 +112,21 @@ module Liquid byte_b = byte_a while byte_b - byte_a = @ss.scan_byte while byte_a && byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY + byte_a = @ss.scan_byte while byte_a && (byte_a != CLOSE_CURLEY && byte_a != OPEN_CURLEY) break unless byte_a byte_b = @ss.scan_byte - if byte_b != CLOSE_CURLEY && byte_b != PERCENTAGE + if byte_b > CLOSE_CURLEY || (byte_b != CLOSE_CURLEY && byte_b != PERCENTAGE) byte_a = byte_b next end - if byte_a == CLOSE_CURLEY && byte_b == CLOSE_CURLEY + val = (byte_a << 8) | byte_b + if val == CLOSE_CURLEY_FOLLOWED_BY_CLOSE_CURLEY return @source.byteslice(start, @ss.pos - start) - elsif byte_a == OPEN_CURLEY && byte_b == PERCENTAGE + elsif val == OPEN_CURLEY_FOLLOWED_BY_PERCENTAGE return next_tag_token_with_start(start) end end diff --git a/test/test_helper.rb b/test/test_helper.rb index e5c88fb2..7a96f93b 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,7 +13,7 @@ if (env_mode = ENV['LIQUID_PARSER_MODE']) puts "-- #{env_mode.upcase} ERROR MODE" mode = env_mode.to_sym end -Liquid::Template.error_mode = mode +Liquid::Environment.default.error_mode = mode if ENV['LIQUID_C'] == '1' puts "-- LIQUID C"