libwordring
matcher.hpp
1 #pragma once
2 
3 #include <initializer_list>
4 #include <vector>
5 
6 namespace wordring
7 {
14  template <typename String>
16  {
17  public:
18  using string_type = String;
19  using character = typename string_type::value_type;
20 
21  enum class match_result : std::uint32_t
22  {
23  partial = 1,
24  succeed,
25  failed,
26  };
27 
29  {
30  container_entry() : m_failed(false) {}
31 
32  container_entry(string_type const& s) : m_s(s), m_failed(false) {}
33 
34  string_type m_s;
35  bool m_failed;
36  };
37 
38  using container = std::vector<container_entry>;
39 
40  public:
45  string_matcher(std::initializer_list<string_type> il)
46  : m_c(il.begin(), il.end())
47  {
48  }
49 
54  string_type const& data() const { return m_s; }
55 
56 
61  auto size() const { return m_s.size(); }
62 
65  void clear()
66  {
67  for (container_entry& entry : m_c) entry.m_failed = false;
68  m_s.clear();
69  }
70 
81  match_result push_back(character ch)
82  {
83  std::uint32_t pos = m_s.size();
84  std::uint32_t n = 0;
85 
86  m_s.push_back(ch);
87 
88  auto it1 = m_c.begin();
89  auto it2 = m_c.end();
90  while (it1 != it2)
91  {
92  if (it1->m_failed || it1->m_s.size() == pos || it1->m_s.operator[](pos) != ch)
93  {
94  ++n;
95  it1->m_failed = true;
96  }
97  else if (m_s.size() == it1->m_s.size()) return match_result::succeed;
98 
99  ++it1;
100  }
101 
102  return (n == m_c.size()) ? match_result::failed : match_result::partial;
103  }
104 
105  protected:
106  container m_c;
107  string_type m_s;
108  };
109 
110 }
wordring::string_matcher::container_entry
Definition: matcher.hpp:28
wordring
Definition: algorithm_.hpp:10
wordring::string_matcher::string_matcher
string_matcher(std::initializer_list< string_type > il)
複数の文字列を指定してマッチャーを構築する
Definition: matcher.hpp:45
wordring::string_matcher::size
auto size() const
これまでに入力された文字数を返す
Definition: matcher.hpp:61
wordring::string_matcher::data
string_type const & data() const
これまでに入力された文字のリストを返す
Definition: matcher.hpp:54
wordring::string_matcher::push_back
match_result push_back(character ch)
文字を追加する
Definition: matcher.hpp:81
wordring::string_matcher
プッシュ型文字列マッチャー
Definition: matcher.hpp:15
wordring::string_matcher::clear
void clear()
マッチャーを初期化する
Definition: matcher.hpp:65