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