Remove wstrings from bookmarks, part 11.

This removes wstrings from BookmarkIndex.

BUG=23581
TEST=builds and passes tests

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57012 0039d316-1c4b-4281-b951-d872f2087c98
parent d11347da
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -8,6 +8,7 @@
#include <iterator>
#include "app/l10n_util.h"
#include "base/string16.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/history/history_database.h"
......@@ -26,7 +27,7 @@ BookmarkIndex::NodeSet::const_iterator BookmarkIndex::Match::nodes_end() const {
void BookmarkIndex::Add(const BookmarkNode* node) {
if (!node->is_url())
return;
std::vector<std::wstring> terms = ExtractQueryWords(node->GetTitle());
std::vector<string16> terms = ExtractQueryWords(node->GetTitleAsString16());
for (size_t i = 0; i < terms.size(); ++i)
RegisterNode(terms[i], node);
}
......@@ -35,16 +36,16 @@ void BookmarkIndex::Remove(const BookmarkNode* node) {
if (!node->is_url())
return;
std::vector<std::wstring> terms = ExtractQueryWords(node->GetTitle());
std::vector<string16> terms = ExtractQueryWords(node->GetTitleAsString16());
for (size_t i = 0; i < terms.size(); ++i)
UnregisterNode(terms[i], node);
}
void BookmarkIndex::GetBookmarksWithTitlesMatching(
const std::wstring& query,
const string16& query,
size_t max_count,
std::vector<bookmark_utils::TitleMatch>* results) {
std::vector<std::wstring> terms = ExtractQueryWords(query);
std::vector<string16> terms = ExtractQueryWords(query);
if (terms.empty())
return;
......@@ -62,7 +63,7 @@ void BookmarkIndex::GetBookmarksWithTitlesMatching(
// matches and so this shouldn't be performance critical.
QueryParser parser;
ScopedVector<QueryNode> query_nodes;
parser.ParseQuery(WideToUTF16(query), &query_nodes.get());
parser.ParseQuery(query, &query_nodes.get());
// The highest typed counts should be at the beginning of the results vector
// so that the best matches will always be included in the results. The loop
......@@ -115,21 +116,21 @@ void BookmarkIndex::AddMatchToResults(
// of QueryParser may filter it out. For example, the query
// ["thi"] will match the bookmark titled [Thinking], but since
// ["thi"] is quoted we don't want to do a prefix match.
if (parser->DoesQueryMatch(WideToUTF16(node->GetTitle()), query_nodes,
if (parser->DoesQueryMatch(node->GetTitleAsString16(), query_nodes,
&(title_match.match_positions))) {
title_match.node = node;
results->push_back(title_match);
}
}
bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const std::wstring& term,
bool BookmarkIndex::GetBookmarksWithTitleMatchingTerm(const string16& term,
bool first_term,
Matches* matches) {
Index::const_iterator i = index_.lower_bound(term);
if (i == index_.end())
return false;
if (!QueryParser::IsWordLongEnoughForPrefixSearch(WideToUTF16(term))) {
if (!QueryParser::IsWordLongEnoughForPrefixSearch(term)) {
// Term is too short for prefix match, compare using exact match.
if (i->first != term)
return false; // No bookmarks with this term.
......@@ -204,28 +205,17 @@ void BookmarkIndex::CombineMatches(const Index::const_iterator& index_i,
}
}
std::vector<std::wstring> BookmarkIndex::ExtractQueryWords(
const std::wstring& query) {
std::vector<string16> BookmarkIndex::ExtractQueryWords(const string16& query) {
std::vector<string16> terms;
if (query.empty())
return std::vector<std::wstring>();
return std::vector<string16>();
QueryParser parser;
// TODO: use ICU normalization.
parser.ExtractQueryWords(l10n_util::ToLower(WideToUTF16(query)), &terms);
// TODO(brettw) just remove this and return |terms| when this is converted
// to string16.
#if defined(WCHAR_T_IS_UTF32)
std::vector<std::wstring> wterms;
for (size_t i = 0; i < terms.size(); i++)
wterms.push_back(UTF16ToWide(terms[i]));
return wterms;
#else
parser.ExtractQueryWords(l10n_util::ToLower(query), &terms);
return terms;
#endif
}
void BookmarkIndex::RegisterNode(const std::wstring& term,
void BookmarkIndex::RegisterNode(const string16& term,
const BookmarkNode* node) {
if (std::find(index_[term].begin(), index_[term].end(), node) !=
index_[term].end()) {
......@@ -235,7 +225,7 @@ void BookmarkIndex::RegisterNode(const std::wstring& term,
index_[term].insert(node);
}
void BookmarkIndex::UnregisterNode(const std::wstring& term,
void BookmarkIndex::UnregisterNode(const string16& term,
const BookmarkNode* node) {
Index::iterator i = index_.find(term);
if (i == index_.end()) {
......
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -9,10 +9,10 @@
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/string16.h"
class BookmarkNode;
class Profile;
......@@ -47,13 +47,13 @@ class BookmarkIndex {
// Returns up to |max_count| of bookmarks containing the text |query|.
void GetBookmarksWithTitlesMatching(
const std::wstring& query,
const string16& query,
size_t max_count,
std::vector<bookmark_utils::TitleMatch>* results);
private:
typedef std::set<const BookmarkNode*> NodeSet;
typedef std::map<std::wstring, NodeSet> Index;
typedef std::map<string16, NodeSet> Index;
// Used when finding the set of bookmarks that match a query. Each match
// represents a set of terms (as an interator into the Index) matching the
......@@ -116,7 +116,7 @@ class BookmarkIndex {
// Populates |matches| for the specified term. If |first_term| is true, this
// is the first term in the query. Returns true if there is at least one node
// matching the term.
bool GetBookmarksWithTitleMatchingTerm(const std::wstring& term,
bool GetBookmarksWithTitleMatchingTerm(const string16& term,
bool first_term,
Matches* matches);
......@@ -142,13 +142,13 @@ class BookmarkIndex {
Matches* result);
// Returns the set of query words from |query|.
std::vector<std::wstring> ExtractQueryWords(const std::wstring& query);
std::vector<string16> ExtractQueryWords(const string16& query);
// Adds |node| to |index_|.
void RegisterNode(const std::wstring& term, const BookmarkNode* node);
void RegisterNode(const string16& term, const BookmarkNode* node);
// Removes |node| from |index_|.
void UnregisterNode(const std::wstring& term, const BookmarkNode* node);
void UnregisterNode(const string16& term, const BookmarkNode* node);
Index index_;
......
......@@ -23,39 +23,39 @@ class BookmarkIndexTest : public testing::Test {
public:
BookmarkIndexTest() : model_(new BookmarkModel(NULL)) {}
void AddBookmarksWithTitles(const wchar_t** titles, size_t count) {
std::vector<std::wstring> title_vector;
void AddBookmarksWithTitles(const char** titles, size_t count) {
std::vector<std::string> title_vector;
for (size_t i = 0; i < count; ++i)
title_vector.push_back(titles[i]);
AddBookmarksWithTitles(title_vector);
}
void AddBookmarksWithTitles(const std::vector<std::wstring>& titles) {
void AddBookmarksWithTitles(const std::vector<std::string>& titles) {
GURL url("about:blank");
for (size_t i = 0; i < titles.size(); ++i)
model_->AddURL(model_->other_node(), static_cast<int>(i),
WideToUTF16Hack(titles[i]), url);
ASCIIToUTF16(titles[i]), url);
}
void ExpectMatches(const std::wstring& query,
const wchar_t** expected_titles,
void ExpectMatches(const std::string& query,
const char** expected_titles,
size_t expected_count) {
std::vector<std::wstring> title_vector;
std::vector<std::string> title_vector;
for (size_t i = 0; i < expected_count; ++i)
title_vector.push_back(expected_titles[i]);
ExpectMatches(query, title_vector);
}
void ExpectMatches(const std::wstring& query,
const std::vector<std::wstring> expected_titles) {
void ExpectMatches(const std::string& query,
const std::vector<std::string> expected_titles) {
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(WideToUTF16Hack(query), 1000,
&matches);
model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16(query), 1000, &matches);
ASSERT_EQ(expected_titles.size(), matches.size());
for (size_t i = 0; i < expected_titles.size(); ++i) {
bool found = false;
for (size_t j = 0; j < matches.size(); ++j) {
if (std::wstring(expected_titles[i]) == matches[j].node->GetTitle()) {
if (ASCIIToUTF16(expected_titles[i]) ==
matches[j].node->GetTitleAsString16()) {
matches.erase(matches.begin() + j);
found = true;
break;
......@@ -68,7 +68,7 @@ class BookmarkIndexTest : public testing::Test {
void ExtractMatchPositions(const std::string& string,
Snippet::MatchPositions* matches) {
std::vector<std::string> match_strings;
SplitString(string, L':', &match_strings);
SplitString(string, ':', &match_strings);
for (size_t i = 0; i < match_strings.size(); ++i) {
std::vector<std::string> chunks;
SplitString(match_strings[i], ',', &chunks);
......@@ -82,11 +82,10 @@ class BookmarkIndexTest : public testing::Test {
}
}
void ExpectMatchPositions(const std::wstring& query,
void ExpectMatchPositions(const std::string& query,
const Snippet::MatchPositions& expected_positions) {
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(WideToUTF16Hack(query), 1000,
&matches);
model_->GetBookmarksWithTitlesMatching(ASCIIToUTF16(query), 1000, &matches);
ASSERT_EQ(1U, matches.size());
const bookmark_utils::TitleMatch& match = matches[0];
ASSERT_EQ(expected_positions.size(), match.match_positions.size());
......@@ -107,41 +106,41 @@ class BookmarkIndexTest : public testing::Test {
// all query paths.
TEST_F(BookmarkIndexTest, Tests) {
struct TestData {
const std::wstring input;
const std::wstring query;
const std::wstring expected;
const std::string input;
const std::string query;
const std::string expected;
} data[] = {
// Trivial test case of only one term, exact match.
{ L"a;b", L"A", L"a" },
{ "a;b", "A", "a" },
// Prefix match, one term.
{ L"abcd;abc;b", L"abc", L"abcd;abc" },
{ "abcd;abc;b", "abc", "abcd;abc" },
// Prefix match, multiple terms.
{ L"abcd cdef;abcd;abcd cdefg", L"abc cde", L"abcd cdef;abcd cdefg"},
{ "abcd cdef;abcd;abcd cdefg", "abc cde", "abcd cdef;abcd cdefg"},
// Exact and prefix match.
{ L"ab cdef;abcd;abcd cdefg", L"ab cdef", L"ab cdef"},
{ "ab cdef;abcd;abcd cdefg", "ab cdef", "ab cdef"},
// Exact and prefix match.
{ L"ab cdef ghij;ab;cde;cdef;ghi;cdef ab;ghij ab",
L"ab cde ghi",
L"ab cdef ghij"},
{ "ab cdef ghij;ab;cde;cdef;ghi;cdef ab;ghij ab",
"ab cde ghi",
"ab cdef ghij"},
// Title with term multiple times.
{ L"ab ab", L"ab", L"ab ab"},
{ "ab ab", "ab", "ab ab"},
// Make sure quotes don't do a prefix match.
{ L"think", L"\"thi\"", L""},
{ "think", "\"thi\"", ""},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
std::vector<std::wstring> titles;
SplitString(data[i].input, L';', &titles);
std::vector<std::string> titles;
SplitString(data[i].input, ';', &titles);
AddBookmarksWithTitles(titles);
std::vector<std::wstring> expected;
std::vector<std::string> expected;
if (!data[i].expected.empty())
SplitString(data[i].expected, L';', &expected);
SplitString(data[i].expected, ';', &expected);
ExpectMatches(data[i].query, expected);
......@@ -152,17 +151,17 @@ TEST_F(BookmarkIndexTest, Tests) {
// Makes sure match positions are updated appropriately.
TEST_F(BookmarkIndexTest, MatchPositions) {
struct TestData {
const std::wstring title;
const std::wstring query;
const std::string title;
const std::string query;
const std::string expected;
} data[] = {
// Trivial test case of only one term, exact match.
{ L"a", L"A", "0,1" },
{ L"foo bar", L"bar", "4,7" },
{ L"fooey bark", L"bar foo", "0,3:6,9"},
{ "a", "A", "0,1" },
{ "foo bar", "bar", "4,7" },
{ "fooey bark", "bar foo", "0,3:6,9"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
std::vector<std::wstring> titles;
std::vector<std::string> titles;
titles.push_back(data[i].title);
AddBookmarksWithTitles(titles);
......@@ -176,28 +175,28 @@ TEST_F(BookmarkIndexTest, MatchPositions) {
// Makes sure index is updated when a node is removed.
TEST_F(BookmarkIndexTest, Remove) {
const wchar_t* input[] = { L"a", L"b" };
const char* input[] = { "a", "b" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
// Remove the node and make sure we don't get back any results.
model_->Remove(model_->other_node(), 0);
ExpectMatches(L"A", NULL, 0U);
ExpectMatches("A", NULL, 0U);
}
// Makes sure index is updated when a node's title is changed.
TEST_F(BookmarkIndexTest, ChangeTitle) {
const wchar_t* input[] = { L"a", L"b" };
const char* input[] = { "a", "b" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
// Remove the node and make sure we don't get back any results.
const wchar_t* expected[] = { L"blah" };
const char* expected[] = { "blah" };
model_->SetTitle(model_->other_node()->GetChild(0), ASCIIToUTF16("blah"));
ExpectMatches(L"BlAh", expected, ARRAYSIZE_UNSAFE(expected));
ExpectMatches("BlAh", expected, ARRAYSIZE_UNSAFE(expected));
}
// Makes sure no more than max queries is returned.
TEST_F(BookmarkIndexTest, HonorMax) {
const wchar_t* input[] = { L"abcd", L"abcde" };
const char* input[] = { "abcd", "abcde" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
std::vector<bookmark_utils::TitleMatch> matches;
......
......@@ -474,8 +474,7 @@ void BookmarkModel::GetBookmarksWithTitlesMatching(
if (!loaded_)
return;
index_->GetBookmarksWithTitlesMatching(UTF16ToWideHack(text), max_count,
matches);
index_->GetBookmarksWithTitlesMatching(text, max_count, matches);
}
void BookmarkModel::ClearStore() {
......
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