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