Skip to content

Commit d824de7

Browse files
committed
Adapt slice filter to work on non-64 bit platforms
1 parent dd37353 commit d824de7

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

lib/liquid/standardfilters.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ module StandardFilters
88
MAX_I32 = (1 << 31) - 1
99
private_constant :MAX_I32
1010

11-
MIN_I64 = -(1 << 63)
12-
MAX_I64 = (1 << 63) - 1
13-
I64_RANGE = MIN_I64..MAX_I64
14-
private_constant :MIN_I64, :MAX_I64, :I64_RANGE
11+
supports_64bit_indices = begin
12+
[][1 << 33, 1 << 33]
13+
true
14+
rescue RangeError
15+
false
16+
end
17+
18+
INDEX_RANGE = if supports_64bit_indices
19+
(-(1 << 63))..((1 << 63) - 1)
20+
else
21+
(-(1 << 31))..((1 << 31) - 1)
22+
end
23+
private_constant :INDEX_RANGE
1524

1625
HTML_ESCAPE = {
1726
'&' => '&amp;',
@@ -214,11 +223,11 @@ def slice(input, offset, length = nil)
214223
Utils.to_s(input).slice(offset, length) || ''
215224
end
216225
rescue RangeError
217-
if I64_RANGE.cover?(length) && I64_RANGE.cover?(offset)
226+
if INDEX_RANGE.cover?(length) && INDEX_RANGE.cover?(offset)
218227
raise # unexpected error
219228
end
220-
offset = offset.clamp(I64_RANGE)
221-
length = length.clamp(I64_RANGE)
229+
offset = offset.clamp(INDEX_RANGE)
230+
length = length.clamp(INDEX_RANGE)
222231
retry
223232
end
224233
end

0 commit comments

Comments
 (0)