Avoid counting input lines twice in BlockLevelPass::execute()
authorC. Scott Ananian <cscott@cscott.net>
Wed, 20 Mar 2019 20:37:49 +0000 (16:37 -0400)
committerC. Scott Ananian <cscott@cscott.net>
Wed, 20 Mar 2019 21:35:14 +0000 (17:35 -0400)
commitad89079a44dc195d1d495dc0207364f03f870589
tree5df02c7fe74562fdcb7404abc0f22e89f73eaf61
parent73239ee9bdb2598ada26c20a1b0009c501b162bc
Avoid counting input lines twice in BlockLevelPass::execute()

In T208070 / I120ca25a77b7b933de4afddd1d458e36a95e26da we added a
check whether we were processing the last line of input, in order
to avoid emitting extra trailing newlines.  But if the number of
input lines is large, StringUtils::explode() will return an
iterator which doesn't implement Countable for efficiency.
I22eebb70af1b19d7c25241fc78bfcced4470e78a fixed this, but at the
cost of scanning the string twice: once just to count the number
of newlines before we begin to iterate over the lines.

This patch uses Iterator::valid() to determine if we're on the
last iteration without having to scan the string twice.

Bug: T208070
Bug: T218817
Change-Id: I41a45266d266195aa6002d3854e018cacf052ca6
includes/parser/BlockLevelPass.php