From: Mukund Sivaraman Date: Wed, 31 Oct 2012 05:23:31 +0000 (+0530) Subject: [2369] Add InputSource::compact() method X-Git-Tag: trac2487_base~18^2~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51e26dc96b447d4f60b6aa2c428bfcfe6e6a4d04;p=thirdparty%2Fkea.git [2369] Add InputSource::compact() method --- diff --git a/src/lib/dns/inputsource.cc b/src/lib/dns/inputsource.cc index 1da98b0b8d..b5b1e0858c 100644 --- a/src/lib/dns/inputsource.cc +++ b/src/lib/dns/inputsource.cc @@ -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 diff --git a/src/lib/dns/inputsource.h b/src/lib/dns/inputsource.h index d45fa2861a..ba2f77dab4 100644 --- a/src/lib/dns/inputsource.h +++ b/src/lib/dns/inputsource.h @@ -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_; diff --git a/src/lib/dns/tests/inputsource_unittest.cc b/src/lib/dns/tests/inputsource_unittest.cc index 097adedf9b..e2c386b429 100644 --- a/src/lib/dns/tests/inputsource_unittest.cc +++ b/src/lib/dns/tests/inputsource_unittest.cc @@ -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;