OLD | NEW |
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 Loading... |
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 = base::StringToLowerASCII(document); | 99 const std::string document_folded = base::ToLowerASCII(document); |
100 | 100 |
101 // Manually construct match_positions of the document. | 101 // Manually construct match_positions of the document. |
102 Snippet::MatchPositions match_positions; | 102 Snippet::MatchPositions match_positions; |
103 match_positions.clear(); | 103 match_positions.clear(); |
104 for (const std::string& word : base::SplitString( | 104 for (const std::string& word : base::SplitString( |
105 query, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { | 105 query, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) { |
106 // Insert all instances of this word into match_pairs. | 106 // Insert all instances of this word into match_pairs. |
107 size_t ofs = 0; | 107 size_t ofs = 0; |
108 while ((ofs = document_folded.find(word, ofs)) != std::string::npos) { | 108 while ((ofs = document_folded.find(word, ofs)) != std::string::npos) { |
109 match_positions.push_back(std::make_pair(ofs, ofs + word.size())); | 109 match_positions.push_back(std::make_pair(ofs, ofs + word.size())); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches); | 242 Snippet::ExtractMatchPositions(data[i].offsets_string, "0", &matches); |
243 EXPECT_EQ(data[i].expected_match_count, matches.size()); | 243 EXPECT_EQ(data[i].expected_match_count, matches.size()); |
244 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) { |
245 EXPECT_EQ(data[i].expected_matches[2 * j], matches[j].first); | 245 EXPECT_EQ(data[i].expected_matches[2 * j], matches[j].first); |
246 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); |
247 } | 247 } |
248 } | 248 } |
249 } | 249 } |
250 | 250 |
251 } // namespace query_parser | 251 } // namespace query_parser |
OLD | NEW |