this->absolutePos_ += this->crtPos_ - this->crtBegin_;
this->crtBuf_->appendChain(std::move(buf));
- // Jump past the new links
- this->crtBuf_ = nextBuf;
- this->crtPos_ = this->crtBegin_ = this->crtBuf_->data();
- this->crtEnd_ = this->crtBuf_->tail();
+ if (nextBuf == this->buffer_) {
+ // We've just appended to the end of the buffer, so advance to the end.
+ this->crtBuf_ = this->buffer_->prev();
+ this->crtBegin_ = this->crtBuf_->data();
+ this->crtPos_ = this->crtEnd_ = this->crtBuf_->tail();
+ // This has already been accounted for, so remove it.
+ this->absolutePos_ -= this->crtEnd_ - this->crtBegin_;
+ } else {
+ // Jump past the new links
+ this->crtBuf_ = nextBuf;
+ this->crtPos_ = this->crtBegin_ = this->crtBuf_->data();
+ this->crtEnd_ = this->crtBuf_->tail();
+ }
}
}
// Check that nextBuf got set correctly
cursor.read<uint8_t>();
}
+ {
+ // Check that inserting at the end of the buffer keeps it at the end.
+ RWPrivateCursor cursor(iobuf1.get());
+ Cursor(iobuf1.get()).clone(cloned, 1);
+ EXPECT_EQ(1, cloned->countChainElements());
+ EXPECT_EQ(1, cloned->computeChainDataLength());
+
+ cursor.advanceToEnd();
+ EXPECT_EQ(17, cursor.getCurrentPosition());
+ cursor.insert(std::move(cloned));
+ EXPECT_EQ(18, cursor.getCurrentPosition());
+ EXPECT_EQ(0, cursor.totalLength());
+ EXPECT_EQ(12, iobuf1->countChainElements());
+ EXPECT_EQ(18, iobuf1->computeChainDataLength());
+ EXPECT_TRUE(cursor.isAtEnd());
+ }
}
TEST(IOBuf, cloneWithEmptyBufAtStart) {