未验证 提交 f03c27ca 编写于 作者: J Jeremy Daer

Merge pull request #24723 from lvl0nax/array_split_fix

Little perfomance fix for Array#split.
......@@ -100,17 +100,13 @@ def split(value = nil)
results
end
else
results, arr = [[]], self.dup
until arr.empty?
if (idx = arr.index(value))
results.last.concat(arr.shift(idx))
arr.shift
results << []
else
results.last.concat(arr.shift(arr.size))
end
arr = self.dup
result = []
while (idx = arr.index(value))
result << arr.shift(idx)
arr.shift
end
results
result << arr
end
end
end
......@@ -123,4 +123,12 @@ def test_split_with_edge_values
assert_equal [[], [2, 3, 4], []], a.split { |i| i == 1 || i == 5 }
assert_equal [1, 2, 3, 4, 5], a
end
def test_split_with_repeated_values
a = [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3]
assert_equal [[1, 2], [5, 5], [4, 6, 2, 1], []], a.split(3)
assert_equal [[1, 2, 3], [], [3, 4, 6, 2, 1, 3]], a.split(5)
assert_equal [[1, 2], [], [], [], [4, 6, 2, 1], []], a.split { |i| i == 3 || i == 5 }
assert_equal [1, 2, 3, 5, 5, 3, 4, 6, 2, 1, 3], a
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册