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