Skip to content
This repository has been archived by the owner on Sep 25, 2019. It is now read-only.

Commit

Permalink
Adding support for Reset in StringTokenizerT and HttpHeadersIterator.
Browse files Browse the repository at this point in the history
TEST=Run HeadersIterator_Reset and the Reset test for StringTokenizer
BUG=none

Review URL: http://codereview.chromium.org/276067

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29330 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
tommi@chromium.org committed Oct 16, 2009
1 parent 484b847 commit c1792d7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/string_tokenizer.h
Expand Up @@ -141,6 +141,11 @@ class StringTokenizerT {
return true;
}

// Start iterating through tokens from the beginning of the string.
void Reset() {
token_end_ = start_pos_;
}

// Returns true if token is a delimiter. When the tokenizer is constructed
// with the RETURN_DELIMS option, this method can be used to check if the
// returned token is actually a delimiter.
Expand All @@ -156,6 +161,7 @@ class StringTokenizerT {
void Init(const_iterator string_begin,
const_iterator string_end,
const str& delims) {
start_pos_ = string_begin;
token_end_ = string_begin;
end_ = string_end;
delims_ = delims;
Expand Down Expand Up @@ -195,6 +201,7 @@ class StringTokenizerT {
return true;
}

const_iterator start_pos_;
const_iterator token_begin_;
const_iterator token_end_;
const_iterator end_;
Expand Down
22 changes: 22 additions & 0 deletions base/string_tokenizer_unittest.cc
Expand Up @@ -30,6 +30,28 @@ TEST(StringTokenizerTest, Simple) {
EXPECT_FALSE(t.GetNext());
}

TEST(StringTokenizerTest, Reset) {
string input = "this is a test";
StringTokenizer t(input, " ");

for (int i = 0; i < 2; ++i) {
EXPECT_TRUE(t.GetNext());
EXPECT_EQ(string("this"), t.token());

EXPECT_TRUE(t.GetNext());
EXPECT_EQ(string("is"), t.token());

EXPECT_TRUE(t.GetNext());
EXPECT_EQ(string("a"), t.token());

EXPECT_TRUE(t.GetNext());
EXPECT_EQ(string("test"), t.token());

EXPECT_FALSE(t.GetNext());
t.Reset();
}
}

TEST(StringTokenizerTest, RetDelims) {
string input = "this is a test";
StringTokenizer t(input, " ");
Expand Down
4 changes: 4 additions & 0 deletions net/http/http_util.h
Expand Up @@ -167,6 +167,10 @@ class HttpUtil {
// current position will be at the end of the headers.
bool AdvanceTo(const char* lowercase_name);

void Reset() {
lines_.Reset();
}

std::string::const_iterator name_begin() const {
return name_begin_;
}
Expand Down
13 changes: 13 additions & 0 deletions net/http/http_util_unittest.cc
Expand Up @@ -105,6 +105,19 @@ TEST(HttpUtilTest, HeadersIterator_AdvanceTo) {
EXPECT_FALSE(it.GetNext()); // should be at end of headers
}

TEST(HttpUtilTest, HeadersIterator_Reset) {
std::string headers = "foo: 1\r\n: 2\r\n3\r\nbar: 4";
HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\r\n");
// Search past "foo".
EXPECT_TRUE(it.AdvanceTo("bar"));
// Now try advancing to "foo". This time it should fail since the iterator
// position is past it.
EXPECT_FALSE(it.AdvanceTo("foo"));
it.Reset();
// Now that we reset the iterator position, we should find 'foo'
EXPECT_TRUE(it.AdvanceTo("foo"));
}

TEST(HttpUtilTest, ValuesIterator) {
std::string values = " must-revalidate, no-cache=\"foo, bar\"\t, private ";

Expand Down

0 comments on commit c1792d7

Please sign in to comment.