Skip to content

Commit b5b3666

Browse files
committed
avoid new regex allocation in util functions
1 parent 9bb7fbf commit b5b3666

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/liquid/utils.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
module Liquid
44
module Utils
5+
DECIMAL_REGEX = /\A-?\d+\.\d+\z/
6+
UNIX_TIMESTAMP_REGEX = /\A\d+\z/
7+
58
def self.slice_collection(collection, from, to)
69
if (from != 0 || !to.nil?) && collection.respond_to?(:load_slice)
710
collection.load_slice(from, to)
@@ -52,7 +55,7 @@ def self.to_number(obj)
5255
when Numeric
5356
obj
5457
when String
55-
/\A-?\d+\.\d+\z/.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
58+
DECIMAL_REGEX.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
5659
else
5760
if obj.respond_to?(:to_number)
5861
obj.to_number
@@ -73,7 +76,7 @@ def self.to_date(obj)
7376
case obj
7477
when 'now', 'today'
7578
Time.now
76-
when /\A\d+\z/, Integer
79+
when UNIX_TIMESTAMP_REGEX, Integer
7780
Time.at(obj.to_i)
7881
when String
7982
Time.parse(obj)

0 commit comments

Comments
 (0)