snippet.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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
  3. // found in the LICENSE file.
  4. // This module computes snippets of queries based on hits in the documents
  5. // for display in history search results.
  6. #ifndef COMPONENTS_QUERY_PARSER_SNIPPET_H__
  7. #define COMPONENTS_QUERY_PARSER_SNIPPET_H__
  8. #include <stddef.h>
  9. #include <string>
  10. #include <vector>
  11. namespace query_parser {
  12. class Snippet {
  13. public:
  14. // Each MatchPosition is the [begin, end) positions of a match within a
  15. // string.
  16. typedef std::pair<size_t, size_t> MatchPosition;
  17. typedef std::vector<MatchPosition> MatchPositions;
  18. // Parses an offsets string as returned from a sqlite full text index. An
  19. // offsets string encodes information about why a row matched a text query.
  20. // The information is encoded in the string as a set of matches, where each
  21. // match consists of the column, term-number, location, and length of the
  22. // match. Each element of the match is separated by a space, as is each match
  23. // from other matches.
  24. //
  25. // This method adds the start and end of each match whose column is
  26. // column_num to match_positions. The pairs are ordered based on first,
  27. // with no overlapping elements.
  28. //
  29. // NOTE: the positions returned are in terms of UTF8 encoding. To convert the
  30. // offsets to UTF-16, use ConvertMatchPositionsToWide
  31. static void ExtractMatchPositions(const std::string& offsets_str,
  32. const std::string& column_num,
  33. MatchPositions* match_positions);
  34. // Converts match positions as returned from ExtractMatchPositions to be in
  35. // terms of a UTF-16 2-byte code unit.
  36. static void ConvertMatchPositionsToWide(
  37. const std::string& utf8_string,
  38. Snippet::MatchPositions* match_positions);
  39. Snippet();
  40. Snippet(const Snippet& other);
  41. Snippet(Snippet&& other) noexcept;
  42. ~Snippet();
  43. Snippet& operator=(const Snippet&);
  44. // Given |matches|, the match positions within |document|, compute the snippet
  45. // for the document.
  46. // Note that |document| is UTF-8 and the offsets in |matches| are byte
  47. // offsets.
  48. void ComputeSnippet(const MatchPositions& matches,
  49. const std::string& document);
  50. const std::u16string& text() const { return text_; }
  51. const MatchPositions& matches() const { return matches_; }
  52. // Efficiently swaps the contents of this snippet with the other.
  53. void Swap(Snippet* other);
  54. private:
  55. // The text of the snippet.
  56. std::u16string text_;
  57. // The matches within text_.
  58. MatchPositions matches_;
  59. };
  60. } // namespace query_parser
  61. #endif // COMPONENTS_QUERY_PARSER_SNIPPET_H__