From 28ede9c539f013c7e173fb3285e32051c253df39 Mon Sep 17 00:00:00 2001 From: Tobi Lutke Date: Wed, 11 Mar 2026 10:39:14 -0400 Subject: [PATCH] =?UTF-8?q?Replace=20manual=20byte-level=20scan=5Fnumber?= =?UTF-8?q?=20with=20regex=20=E2=80=94=20cleaner=20code,=20same=20performa?= =?UTF-8?q?nce\n\nResult:=20{"status":"keep","combined=5F=C2=B5s":4184,"pa?= =?UTF-8?q?rse=5F=C2=B5s":2931,"render=5F=C2=B5s":1253,"allocations":25535?= =?UTF-8?q?}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/liquid/cursor.rb | 42 ++++++++---------------------------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/lib/liquid/cursor.rb b/lib/liquid/cursor.rb index 0660b159..67c5f610 100644 --- a/lib/liquid/cursor.rb +++ b/lib/liquid/cursor.rb @@ -132,44 +132,18 @@ module Liquid end end + # Regex for numbers: -?\d+(\.\d+)? + FLOAT_REGEX = /-?\d+\.\d+/ + INT_REGEX = /-?\d+/ + # ── Numbers ───────────────────────────────────────────────────── # Try to scan an integer or float. Returns the number or nil. def scan_number - start = @ss.pos - b = @ss.peek_byte - return unless b - - if b == DASH - @ss.scan_byte - b = @ss.peek_byte - unless b && b >= ZERO && b <= NINE - @ss.pos = start - return - end - elsif b >= ZERO && b <= NINE - # ok - else - return + if (s = @ss.scan(FLOAT_REGEX)) + s.to_f + elsif (s = @ss.scan(INT_REGEX)) + s.to_i end - - # Scan digits - @ss.scan_byte - @ss.scan_byte while (b = @ss.peek_byte) && b >= ZERO && b <= NINE - - if @ss.peek_byte == DOT - @ss.scan_byte - # Must have digit after dot for float - if (b = @ss.peek_byte) && b >= ZERO && b <= NINE - @ss.scan_byte - @ss.scan_byte while (b = @ss.peek_byte) && b >= ZERO && b <= NINE - return @source.byteslice(start, @ss.pos - start).to_f - else - # "123." — integer portion only, rewind past dot - @ss.pos -= 1 - end - end - - Integer(@source.byteslice(start, @ss.pos - start), 10) end # ── Strings ─────────────────────────────────────────────────────