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

string_slice.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0314cd14d8bc1bd3410ee61d527455edd1a0d8ab (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
63
64
65
66
67
68
69
70
71
72
73
74
#include "indexer/string_slice.hpp"

namespace search
{
JoinIterator::JoinIterator(StringSliceBase const & slice, Position position) : m_slice(slice)
{
  if (position == Position::Begin)
  {
    m_string = 0;
    m_offset = 0;
    Normalize();
  }
  else
  {
    m_string = GetMaxSize();
    m_offset = 0;
  }
}

// static
JoinIterator JoinIterator::Begin(StringSliceBase const & slice)
{
  return JoinIterator(slice, Position::Begin);
}

// static
JoinIterator JoinIterator::End(StringSliceBase const & slice)
{
  return JoinIterator(slice, Position::End);
}

JoinIterator & JoinIterator::operator++()
{
  ++m_offset;
  Normalize();
  return *this;
}

void JoinIterator::Normalize()
{
  while (m_string != GetMaxSize() && m_offset >= GetSize(m_string))
  {
    ++m_string;
    m_offset = 0;
  }
}

size_t JoinIterator::GetSize(size_t string) const
{
  if (string >= GetMaxSize())
    return 0;

  if (string & 1)
    return 1;

  return m_slice.Get(string >> 1).size();
}

JoinIterator::value_type JoinIterator::GetChar(size_t string, size_t offset) const
{
  if (string >= GetMaxSize())
    return 0;

  if (string & 1)
  {
    ASSERT_EQUAL(offset, 0, ());
    return ' ';
  }

  auto const & s = m_slice.Get(string >> 1);
  ASSERT_LESS(offset, s.size(), ());
  return s[offset];
}
}  // namespace search