mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-26 13:45:13 -07:00
micro optimization
This commit is contained in:
@@ -6,17 +6,20 @@ module Liquid
|
|||||||
class Tokenizer
|
class Tokenizer
|
||||||
attr_reader :line_number, :for_liquid_tag
|
attr_reader :line_number, :for_liquid_tag
|
||||||
|
|
||||||
TAG_START = /\{%/
|
|
||||||
TAG_END = /%\}/
|
TAG_END = /%\}/
|
||||||
VARIABLE_START = /\{\{/
|
VARIABLE_START = /\{\{/
|
||||||
VARIABLE_END = /\}\}/
|
VARIABLE_END = /\}\}/
|
||||||
TAG_OR_VARIABLE_START = /\{[\{\%}]/
|
TAG_OR_VARIABLE_START = /\{[\{\%}]/
|
||||||
|
NEWLINE = /\n/
|
||||||
|
|
||||||
|
OPEN_CURLEY = 123
|
||||||
|
CLOSE_CURLEY = 125
|
||||||
|
PERCENTAGE = 37
|
||||||
|
|
||||||
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
|
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
|
||||||
@source = source
|
@source = source
|
||||||
@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
|
|
||||||
@ss = StringScanner.new(source)
|
@ss = StringScanner.new(source)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -27,8 +30,6 @@ module Liquid
|
|||||||
|
|
||||||
return nil unless token
|
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")
|
||||||
end
|
end
|
||||||
@@ -41,7 +42,7 @@ module Liquid
|
|||||||
def next_liquid_token
|
def next_liquid_token
|
||||||
# read until we find a \n
|
# read until we find a \n
|
||||||
start = @ss.pos
|
start = @ss.pos
|
||||||
if @ss.scan_until(/\n/).nil?
|
if @ss.scan_until(NEWLINE).nil?
|
||||||
token = @ss.rest
|
token = @ss.rest
|
||||||
@ss.terminate
|
@ss.terminate
|
||||||
return token
|
return token
|
||||||
@@ -106,9 +107,7 @@ module Liquid
|
|||||||
start = @ss.pos if start.nil?
|
start = @ss.pos if start.nil?
|
||||||
@ss.pos += 2
|
@ss.pos += 2
|
||||||
|
|
||||||
if @ss.scan_until(TAG_END).nil?
|
@ss.scan_until(TAG_END).nil?
|
||||||
raise_syntax_error("tag was not properly terminated")
|
|
||||||
end
|
|
||||||
|
|
||||||
@ss.string.byteslice(start, @ss.pos - start)
|
@ss.string.byteslice(start, @ss.pos - start)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user