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

string_slice.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da06c2611148845c04518972faeab944abff1b05 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once

#include "base/string_utils.hpp"

#include "std/cstdint.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"

namespace search
{
class StringSliceBase
{
public:
  using TString = strings::UniString;

  virtual ~StringSliceBase() = default;

  virtual TString const & Get(size_t i) const = 0;
  virtual size_t Size() const = 0;
};

class StringSlice : public StringSliceBase
{
public:
  StringSlice(vector<TString> const & strings) : m_strings(strings) {}

  virtual TString const & Get(size_t i) const override { return m_strings[i]; }
  virtual size_t Size() const override { return m_strings.size(); }

private:
  vector<TString> const & m_strings;
};

// Allows to iterate over space-separated strings in StringSliceBase.
class JoinIterator
{
public:
  using difference_type = ptrdiff_t;
  using value_type = strings::UniChar;
  using pointer = strings::UniChar *;
  using reference = strings::UniChar &;
  using iterator_category = std::forward_iterator_tag;

  static JoinIterator Begin(StringSliceBase const & slice);
  static JoinIterator End(StringSliceBase const & slice);

  inline bool operator==(JoinIterator const & rhs) const
  {
    return &m_slice == &rhs.m_slice && m_string == rhs.m_string && m_offset == rhs.m_offset;
  }

  inline bool operator!=(JoinIterator const & rhs) const { return !(*this == rhs); }

  inline value_type operator*() const { return GetChar(m_string, m_offset); }

  JoinIterator & operator++();

private:
  enum class Position
  {
    Begin,
    End
  };

  JoinIterator(StringSliceBase const & slice, Position position);

  // Normalizes current position, i.e. moves to the next valid
  // character if current position is invalid, or to the end of the
  // whole sequence if there are no valid positions.
  void Normalize();

  size_t GetSize(size_t string) const;

  inline size_t GetMaxSize() const { return m_slice.Size() == 0 ? 0 : m_slice.Size() * 2 - 1; }

  value_type GetChar(size_t string, size_t offset) const;

  StringSliceBase const & m_slice;

  // Denotes the current string the iterator points to.  Even values
  // of |m_string| divided by two correspond to indices in
  // |m_slice|. Odd values correspond to intermediate space
  // characters.
  size_t m_string;

  // An index of the current character in the current string.
  size_t m_offset;
};
}  // namespace search