diff --git a/src/utils/PhutilRope.php b/src/utils/PhutilRope.php --- a/src/utils/PhutilRope.php +++ b/src/utils/PhutilRope.php @@ -83,29 +83,29 @@ * @param int Bytes to remove. * @return this */ - public function removeBytesFromHead($length) { - if ($length <= 0) { + public function removeBytesFromHead($remove) { + if ($remove <= 0) { throw new InvalidArgumentException( pht('Length must be larger than 0!')); } - $remaining_length = $length; + $remaining_bytes = $remove; foreach ($this->buffers as $key => $buf) { $len = strlen($buf); - if ($len <= $length) { + if ($len <= $remaining_bytes) { unset($this->buffers[$key]); - $remaining_length -= $len; - if (!$remaining_length) { + $remaining_bytes -= $len; + if (!$remaining_bytes) { break; } } else { - $this->buffers[$key] = substr($buf, $length); + $this->buffers[$key] = substr($buf, $remaining_bytes); break; } } - if ($length <= $this->length) { - $this->length -= $length; + if ($this->buffers) { + $this->length -= $remove; } else { $this->length = 0; } diff --git a/src/utils/__tests__/PhutilRopeTestCase.php b/src/utils/__tests__/PhutilRopeTestCase.php --- a/src/utils/__tests__/PhutilRopeTestCase.php +++ b/src/utils/__tests__/PhutilRopeTestCase.php @@ -21,4 +21,36 @@ $this->assertEqual('', $rope->getAsString()); } + public function testMoreRopeOperations() { + $rope = new PhutilRope(); + $rope->append('aaa'); + $rope->append('bbb'); + $rope->append('ccc'); + $rope->append('rrrrddddddddd'); + $rope->removeBytesFromHead(4); + + $string = $rope->getAsString(); + $this->assertEqual('bbcccrrrrddddddddd', $string); + $this->assertEqual(strlen($string), $rope->getByteLength()); + + $rope = new PhutilRope(); + $rope->append('aaa'); + $rope->append('bbb'); + $rope->removeBytesFromHead(6); + + $string = $rope->getAsString(); + $this->assertEqual('', $string); + $this->assertEqual(0, $rope->getByteLength()); + + + $rope = new PhutilRope(); + $rope->append('a'); + $rope->append('b'); + $rope->append('c'); + $rope->removeBytesFromHead(1024); + + $string = $rope->getAsString(); + $this->assertEqual('', $string); + $this->assertEqual(0, $rope->getByteLength()); + } }