Commit 51e26dc9 authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

[2369] Add InputSource::compact() method

parent 9843e33b
......@@ -102,6 +102,17 @@ InputSource::ungetAll() {
at_eof_ = false;
}
void
InputSource::compact() {
if (buffer_pos_ == buffer_.size()) {
buffer_.clear();
} else {
buffer_.erase(buffer_.begin() + buffer_pos_);
}
buffer_pos_ = 0;
}
} // namespace master_lexer_internal
} // namespace dns
} // namespace isc
......@@ -92,6 +92,11 @@ public:
/// it sets the current line number to the line number saved then.
void ungetAll();
/// Removes buffered content before the current location in the
/// \c InputSource. It's not possible to \c ungetChar() after this,
/// unless we read more data using \c getChar().
void compact();
private:
bool at_eof_;
size_t line_;
......
......@@ -123,6 +123,96 @@ TEST_F(InputSourceTest, ungetAll) {
EXPECT_FALSE(source_.atEOF());
}
TEST_F(InputSourceTest, compact) {
// Compact at the start
source_.compact();
// Ungetting here must throw.
EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
for (size_t i = 0; i < str_length_; i++) {
EXPECT_EQ(str_[i], source_.getChar());
EXPECT_FALSE(source_.atEOF());
}
// At this point, we still have not reached EOF.
EXPECT_FALSE(source_.atEOF());
// This should cause EOF to be set.
EXPECT_EQ(-1, source_.getChar());
// Now, EOF should be set.
EXPECT_TRUE(source_.atEOF());
EXPECT_EQ(4, source_.getCurrentLine());
// Compact again
source_.compact();
// We are still at EOF.
EXPECT_TRUE(source_.atEOF());
EXPECT_EQ(4, source_.getCurrentLine());
// Skip the EOF.
source_.ungetChar();
// Ungetting here must throw.
EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
EXPECT_EQ(-1, source_.getChar());
EXPECT_TRUE(source_.atEOF());
}
TEST_F(InputSourceTest, compactDuring) {
// First, skip to line 2.
while (!source_.atEOF() &&
(source_.getCurrentLine() != 2)) {
source_.getChar();
}
EXPECT_FALSE(source_.atEOF());
size_t line = source_.getCurrentLine();
EXPECT_EQ(2, line);
source_.saveLine();
source_.compact();
// Ungetting here must throw.
EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
for (size_t i = 15; i < str_length_; i++) {
EXPECT_EQ(str_[i], source_.getChar());
EXPECT_FALSE(source_.atEOF());
}
// At this point, we still have not reached EOF.
EXPECT_FALSE(source_.atEOF());
// This should cause EOF to be set.
EXPECT_EQ(-1, source_.getChar());
// Now, EOF should be set.
EXPECT_TRUE(source_.atEOF());
// Now, ungetAll() and check where it goes back.
source_.ungetAll();
// Ungetting here must throw.
EXPECT_THROW(source_.ungetChar(), InputSource::UngetError);
for (size_t i = 15; i < str_length_; i++) {
EXPECT_EQ(str_[i], source_.getChar());
EXPECT_FALSE(source_.atEOF());
}
// At this point, we still have not reached EOF.
EXPECT_FALSE(source_.atEOF());
// This should cause EOF to be set.
EXPECT_EQ(-1, source_.getChar());
// Now, EOF should be set.
EXPECT_TRUE(source_.atEOF());
}
// Test line counters.
TEST_F(InputSourceTest, lines) {
size_t line = 1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment