Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: components/query_parser/snippet_unittest.cc

Issue 1234973004: Update SplitString calls in components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/query_parser/snippet.h" 5 #include "components/query_parser/snippet.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // sqlite's FTS matching. BuildSnippet returns the snippet for matching 89 // sqlite's FTS matching. BuildSnippet returns the snippet for matching
90 // |query| against |document|. Matches are surrounded by "**". 90 // |query| against |document|. Matches are surrounded by "**".
91 base::string16 BuildSnippet(const std::string& document, 91 base::string16 BuildSnippet(const std::string& document,
92 const std::string& query) { 92 const std::string& query) {
93 // This function assumes that |document| does not contain 93 // This function assumes that |document| does not contain
94 // any character for which lowercasing changes its length. Further, 94 // any character for which lowercasing changes its length. Further,
95 // it's assumed that lowercasing only the ASCII-portion works for 95 // it's assumed that lowercasing only the ASCII-portion works for
96 // |document|. We need to add more test cases and change this function 96 // |document|. We need to add more test cases and change this function
97 // to be more generic depending on how we deal with 'folding for match' 97 // to be more generic depending on how we deal with 'folding for match'
98 // in history. 98 // in history.
99 const std::string document_folded = 99 const std::string document_folded = base::StringToLowerASCII(document);
100 base::StringToLowerASCII(std::string(document));
101
102 std::vector<std::string> query_words;
103 base::SplitString(query, ' ', &query_words);
104 100
105 // Manually construct match_positions of the document. 101 // Manually construct match_positions of the document.
106 Snippet::MatchPositions match_positions; 102 Snippet::MatchPositions match_positions;
107 match_positions.clear(); 103 match_positions.clear();
108 for (std::vector<std::string>::iterator qw = query_words.begin(); 104 for (const std::string& word : base::SplitString(
109 qw != query_words.end(); ++qw) { 105 query, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
110 // Insert all instances of this word into match_pairs. 106 // Insert all instances of this word into match_pairs.
111 size_t ofs = 0; 107 size_t ofs = 0;
112 while ((ofs = document_folded.find(*qw, ofs)) != std::string::npos) { 108 while ((ofs = document_folded.find(word, ofs)) != std::string::npos) {
113 match_positions.push_back(std::make_pair(ofs, ofs + qw->size())); 109 match_positions.push_back(std::make_pair(ofs, ofs + word.size()));
114 ofs += qw->size(); 110 ofs += word.size();
115 } 111 }
116 } 112 }
117 // Sort match_positions in order of increasing offset. 113 // Sort match_positions in order of increasing offset.
118 std::sort(match_positions.begin(), match_positions.end(), ComparePair1st); 114 std::sort(match_positions.begin(), match_positions.end(), ComparePair1st);
119 115
120 // Compute the snippet. 116 // Compute the snippet.
121 Snippet snippet; 117 Snippet snippet;
122 snippet.ComputeSnippet(match_positions, document); 118 snippet.ComputeSnippet(match_positions, document);
123 119
124 // Now "highlight" all matches in the snippet with **. 120 // Now "highlight" all matches in the snippet with **.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches); 242 Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches);
247 EXPECT_EQ(data[i].expected_match_count, matches.size()); 243 EXPECT_EQ(data[i].expected_match_count, matches.size());
248 for (size_t j = 0; j < data[i].expected_match_count; ++j) { 244 for (size_t j = 0; j < data[i].expected_match_count; ++j) {
249 EXPECT_EQ(data[i].expected_matches[2 * j], matches[j].first); 245 EXPECT_EQ(data[i].expected_matches[2 * j], matches[j].first);
250 EXPECT_EQ(data[i].expected_matches[2 * j + 1], matches[j].second); 246 EXPECT_EQ(data[i].expected_matches[2 * j + 1], matches[j].second);
251 } 247 }
252 } 248 }
253 } 249 }
254 250
255 } // namespace query_parser 251 } // namespace query_parser
OLDNEW
« no previous file with comments | « components/query_parser/snippet.cc ('k') | components/search_engines/default_search_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698