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/query_parser.h" | 5 #include "components/query_parser/query_parser.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 i != matches->end(); ) { | 39 i != matches->end(); ) { |
40 if (SnippetIntersects(mp, *i)) { | 40 if (SnippetIntersects(mp, *i)) { |
41 mp.second = std::max(mp.second, i->second); | 41 mp.second = std::max(mp.second, i->second); |
42 i = matches->erase(i); | 42 i = matches->erase(i); |
43 } else { | 43 } else { |
44 return; | 44 return; |
45 } | 45 } |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 // Sorts the match positions in |matches| by their first index, then coalesces | |
50 // any match positions that intersect each other. | |
51 void CoalseAndSortMatchPositions(Snippet::MatchPositions* matches) { | |
52 std::sort(matches->begin(), matches->end(), &CompareMatchPosition); | |
53 // WARNING: we don't use iterator here as CoalesceMatchesFrom may remove | |
54 // from matches. | |
55 for (size_t i = 0; i < matches->size(); ++i) | |
56 CoalesceMatchesFrom(i, matches); | |
57 } | |
58 | |
59 // Returns true if the character is considered a quote. | 49 // Returns true if the character is considered a quote. |
60 bool IsQueryQuote(wchar_t ch) { | 50 bool IsQueryQuote(wchar_t ch) { |
61 return ch == '"' || | 51 return ch == '"' || |
62 ch == 0xab || // left pointing double angle bracket | 52 ch == 0xab || // left pointing double angle bracket |
63 ch == 0xbb || // right pointing double angle bracket | 53 ch == 0xbb || // right pointing double angle bracket |
64 ch == 0x201c || // left double quotation mark | 54 ch == 0x201c || // left double quotation mark |
65 ch == 0x201d || // right double quotation mark | 55 ch == 0x201d || // right double quotation mark |
66 ch == 0x201e; // double low-9 quotation mark | 56 ch == 0x201e; // double low-9 quotation mark |
67 } | 57 } |
68 | 58 |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 if (!query_nodes[i]->HasMatchIn(query_words, &matches)) | 375 if (!query_nodes[i]->HasMatchIn(query_words, &matches)) |
386 return false; | 376 return false; |
387 } | 377 } |
388 if (lower_text.length() != text.length()) { | 378 if (lower_text.length() != text.length()) { |
389 // The lower case string differs from the original string. The matches are | 379 // The lower case string differs from the original string. The matches are |
390 // meaningless. | 380 // meaningless. |
391 // TODO(sky): we need a better way to align the positions so that we don't | 381 // TODO(sky): we need a better way to align the positions so that we don't |
392 // completely punt here. | 382 // completely punt here. |
393 match_positions->clear(); | 383 match_positions->clear(); |
394 } else { | 384 } else { |
395 CoalseAndSortMatchPositions(&matches); | 385 SortAndCoalesceMatchPositions(&matches); |
396 match_positions->swap(matches); | 386 match_positions->swap(matches); |
397 } | 387 } |
398 return true; | 388 return true; |
399 } | 389 } |
400 | 390 |
401 bool QueryParser::DoesQueryMatch(const std::vector<QueryWord>& query_words, | 391 bool QueryParser::DoesQueryMatch(const std::vector<QueryWord>& query_words, |
402 const std::vector<QueryNode*>& query_nodes) { | 392 const std::vector<QueryNode*>& query_nodes) { |
403 if (query_nodes.empty() || query_words.empty()) | 393 if (query_nodes.empty() || query_words.empty()) |
404 return false; | 394 return false; |
405 | 395 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 while (iter.Advance()) { | 451 while (iter.Advance()) { |
462 // Just found a span between 'prev' (inclusive) and 'pos' (exclusive). It | 452 // Just found a span between 'prev' (inclusive) and 'pos' (exclusive). It |
463 // is not necessarily a word, but could also be a sequence of punctuation | 453 // is not necessarily a word, but could also be a sequence of punctuation |
464 // or whitespace. | 454 // or whitespace. |
465 if (iter.IsWord()) { | 455 if (iter.IsWord()) { |
466 base::string16 word = iter.GetString(); | 456 base::string16 word = iter.GetString(); |
467 if (!word.empty()) { | 457 if (!word.empty()) { |
468 words->push_back(QueryWord()); | 458 words->push_back(QueryWord()); |
469 words->back().word = word; | 459 words->back().word = word; |
470 words->back().position = iter.prev(); | 460 words->back().position = iter.prev(); |
471 } | 461 } |
472 } | 462 } |
473 } | 463 } |
474 } | 464 } |
475 | 465 |
| 466 // static |
| 467 void QueryParser::SortAndCoalesceMatchPositions( |
| 468 Snippet::MatchPositions* matches) { |
| 469 std::sort(matches->begin(), matches->end(), &CompareMatchPosition); |
| 470 // WARNING: we don't use iterator here as CoalesceMatchesFrom may remove |
| 471 // from matches. |
| 472 for (size_t i = 0; i < matches->size(); ++i) |
| 473 CoalesceMatchesFrom(i, matches); |
| 474 } |
| 475 |
476 } // namespace query_parser | 476 } // namespace query_parser |
OLD | NEW |