Welcome to mirror list, hosted at ThFree Co, Russian Federation.

highlighting.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6580cdcf6b803a8c6d160bfe9134606bee3813e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once

#include "indexer/search_delimiters.hpp"

#include "search/common.hpp"
#include "search/result.hpp"

#include "base/string_utils.hpp"

#include <string>
#include <utility>

namespace search
{
template <typename LowTokensIterType, typename F>
void SearchStringTokensIntersectionRanges(std::string const & s, LowTokensIterType itLowBeg,
                                          LowTokensIterType itLowEnd, F f)
{
  // split input query by tokens and prefix
  search::Delimiters delimsTest;
  size_t pos = 0;

  strings::UniString const str = strings::MakeUniString(s);
  size_t const strLen = str.size();
  while (pos < strLen)
  {
    // skip delimeters
    while (pos < strLen && delimsTest(str[pos]))
      ++pos;

    size_t const beg = pos;

    // find token
    while (pos < strLen && !delimsTest(str[pos]))
      ++pos;

    strings::UniString subStr;
    subStr.assign(str.begin() + beg, str.begin() + pos);
    size_t maxCount = 0;
    std::pair<uint16_t, uint16_t> result(0, 0);

    for (LowTokensIterType itLow = itLowBeg; itLow != itLowEnd; ++itLow)
    {
      size_t const cnt = strings::CountNormLowerSymbols(subStr, *itLow);

      if (cnt > maxCount)
      {
        maxCount = cnt;
        result.first = beg;
        result.second = cnt;
      }
    }

    if (result.second != 0)
      f(result);
  }
}

// Adds to |res| the ranges that match the query tokens and, therefore, should be highlighted.
// The query is passed in |tokens| and |prefix|.
void HighlightResult(QueryTokens const & tokens, strings::UniString const & prefix, Result & res);
}  // namespace search