diff options
| author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-09 19:16:02 +0000 |
|---|---|---|
| committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-09 19:16:02 +0000 |
| commit | 7de99596feecaa24900a082a721ef8d4f57b47ee (patch) | |
| tree | bee4f2c6846d15bac3880e756edc22d6c2671182 /chrome/browser/history | |
| parent | 9a2e76b4ed08763e9bbecff875ad1a06a9e39f69 (diff) | |
| download | chromium_src-7de99596feecaa24900a082a721ef8d4f57b47ee.zip chromium_src-7de99596feecaa24900a082a721ef8d4f57b47ee.tar.gz chromium_src-7de99596feecaa24900a082a721ef8d4f57b47ee.tar.bz2 | |
Adds QueryParser::ExtractQueryWords. For consistent this should
probably be ParseQuery, but I couldn't bring myself to another
ParseQuery method. I'm going to need this for bookmark queries.
BUG=4065
TEST=none
Review URL: http://codereview.chromium.org/13296
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6604 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/history')
| -rw-r--r-- | chrome/browser/history/query_parser.cc | 50 | ||||
| -rw-r--r-- | chrome/browser/history/query_parser.h | 10 | ||||
| -rw-r--r-- | chrome/browser/history/query_parser_unittest.cc | 26 |
3 files changed, 71 insertions, 15 deletions
diff --git a/chrome/browser/history/query_parser.cc b/chrome/browser/history/query_parser.cc index ef40601..3ebaad9 100644 --- a/chrome/browser/history/query_parser.cc +++ b/chrome/browser/history/query_parser.cc @@ -97,6 +97,7 @@ class QueryNodeWord : public QueryNode { Snippet::MatchPositions* match_positions) const; virtual bool Matches(const std::wstring& word, bool exact) const; + virtual void AppendWords(std::vector<std::wstring>* words) const; private: std::wstring word_; @@ -124,6 +125,10 @@ bool QueryNodeWord::Matches(const std::wstring& word, bool exact) const { (word_.compare(0, word_.size(), word, 0, word_.size()) == 0); } +void QueryNodeWord::AppendWords(std::vector<std::wstring>* words) const { + words->push_back(word_); +} + int QueryNodeWord::AppendToSQLiteQuery(std::wstring* query) const { query->append(word_); @@ -162,6 +167,7 @@ class QueryNodeList : public QueryNode { NOTREACHED(); return false; } + virtual void AppendWords(std::vector<std::wstring>* words) const; protected: int AppendChildrenToString(std::wstring* query) const; @@ -175,6 +181,26 @@ QueryNodeList::~QueryNodeList() { delete *node; } +void QueryNodeList::RemoveEmptySubnodes() { + for (size_t i = 0; i < children_.size(); ++i) { + if (children_[i]->IsWord()) + continue; + + QueryNodeList* list_node = static_cast<QueryNodeList*>(children_[i]); + list_node->RemoveEmptySubnodes(); + if (list_node->children()->empty()) { + children_.erase(children_.begin() + i); + --i; + delete list_node; + } + } +} + +void QueryNodeList::AppendWords(std::vector<std::wstring>* words) const { + for (size_t i = 0; i < children_.size(); ++i) + children_[i]->AppendWords(words); +} + int QueryNodeList::AppendChildrenToString(std::wstring* query) const { int num_words = 0; for (QueryNodeVector::const_iterator node = children_.begin(); @@ -259,6 +285,15 @@ void QueryParser::ParseQuery(const std::wstring& query, nodes->swap(*root.children()); } + +void QueryParser::ExtractQueryWords(const std::wstring& query, + std::vector<std::wstring>* words) { + QueryNodeList root; + if (!ParseQueryImpl(query, &root)) + return; + root.AppendWords(words); +} + bool QueryParser::DoesQueryMatch(const std::wstring& text, const std::vector<QueryNode*>& query_nodes, Snippet::MatchPositions* match_positions) { @@ -345,18 +380,3 @@ void QueryParser::ExtractQueryWords(const std::wstring& text, } } } - -void QueryNodeList::RemoveEmptySubnodes() { - for (size_t i = 0; i < children_.size(); ++i) { - if (children_[i]->IsWord()) - continue; - - QueryNodeList* list_node = static_cast<QueryNodeList*>(children_[i]); - list_node->RemoveEmptySubnodes(); - if (list_node->children()->empty()) { - children_.erase(children_.begin() + i); - --i; - delete list_node; - } - } -} diff --git a/chrome/browser/history/query_parser.h b/chrome/browser/history/query_parser.h index d374ba4..ed7bcf2 100644 --- a/chrome/browser/history/query_parser.h +++ b/chrome/browser/history/query_parser.h @@ -48,6 +48,9 @@ class QueryNode { // giving the matching region. virtual bool HasMatchIn(const std::vector<QueryWord>& words, Snippet::MatchPositions* match_positions) const = 0; + + // Appends the words that make up this node in |words|. + virtual void AppendWords(std::vector<std::wstring>* words) const = 0; }; @@ -67,6 +70,13 @@ class QueryParser { void ParseQuery(const std::wstring& query, std::vector<QueryNode*>* nodes); + // Parses a query returning the words that make up the query. Any words in + // quotes are put in |words| without the quotes. For example, the query text + // "foo bar" results in two entries being added to words, one for foo and one + // for bar. + void ExtractQueryWords(const std::wstring& query, + std::vector<std::wstring>* words); + // Returns true if the string text matches the query nodes created by a call // to ParseQuery. If the query does match each of the matching positions in // the text is added to |match_positions|. diff --git a/chrome/browser/history/query_parser_unittest.cc b/chrome/browser/history/query_parser_unittest.cc index 50cb343..3f75989 100644 --- a/chrome/browser/history/query_parser_unittest.cc +++ b/chrome/browser/history/query_parser_unittest.cc @@ -131,3 +131,29 @@ TEST_F(QueryParserTest, ParseQueryNodesAndMatch) { } } } + +TEST_F(QueryParserTest, ExtractQueryWords) { + struct TestData2 { + const std::wstring text; + const std::wstring w1; + const std::wstring w2; + const std::wstring w3; + const size_t word_count; + } data[] = { + { L"foo", L"foo", L"", L"", 1 }, + { L"foo bar", L"foo", L"bar", L"", 2 }, + { L"\"foo bar\"", L"foo", L"bar", L"", 2 }, + { L"\"foo bar\" a", L"foo", L"bar", L"a", 3 }, + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { + std::vector<std::wstring> results; + QueryParser parser; + parser.ExtractQueryWords(data[i].text, &results); + ASSERT_EQ(data[i].word_count, results.size()); + EXPECT_EQ(data[i].w1, results[0]); + if (results.size() == 2) + EXPECT_EQ(data[i].w2, results[1]); + if (results.size() == 3) + EXPECT_EQ(data[i].w3, results[2]); + } +} |
