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.
5 // This module computes snippets of queries based on hits in the documents
6 // for display in history search results.
8 #ifndef CHROME_BROWSER_HISTORY_SNIPPET_H__
9 #define CHROME_BROWSER_HISTORY_SNIPPET_H__
13 #include "base/strings/string16.h"
17 // Each MatchPosition is the [begin, end) positions of a match within a
19 typedef std::pair<size_t, size_t> MatchPosition;
20 typedef std::vector<MatchPosition> MatchPositions;
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.
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.
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);
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);
48 // Given |matches|, the match positions within |document|, compute the snippet
50 // Note that |document| is UTF-8 and the offsets in |matches| are byte
52 void ComputeSnippet(const MatchPositions& matches,
53 const std::string& document);
55 const base::string16& text() const { return text_; }
56 const MatchPositions& matches() const { return matches_; }
58 // Efficiently swaps the contents of this snippet with the other.
59 void Swap(Snippet* other);
62 // The text of the snippet.
65 // The matches within text_.
66 MatchPositions matches_;
69 #endif // CHROME_BROWSER_HISTORY_SNIPPET_H__