The Battle for Wesnoth  1.19.0-dev
ucs4_iterator_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <iterator> //input_iterator_tag
19 #include <utility> //pair
20 #include <cstddef> //ptrdiff_t
21 #include <cassert> //assert
22 
23 namespace ucs4
24 {
25  template<typename string_type, typename update_implementation>
27  {
28  public:
29  typedef std::input_iterator_tag iterator_category;
30  typedef char32_t value_type;
31  typedef ptrdiff_t difference_type;
32  typedef char32_t* pointer;
33  typedef char32_t& reference;
34 
35  iterator_base(const string_type& str)
36  : current_char(0)
37  , string_end(str.end())
38  , current_substr(str.begin(), str.begin())
39  {
40  update();
41  }
42 
43  iterator_base(typename string_type::const_iterator const& begin, typename string_type::const_iterator const& end)
44  : current_char(0)
45  , string_end(end)
47  {
48  update();
49  }
50 
51  static iterator_base begin(const string_type& str)
52  {
53  return iterator_base(str.begin(), str.end());
54  }
55 
56  static iterator_base end(const string_type& str)
57  {
58  return iterator_base(str.end(), str.end());
59  }
60 
61  bool operator==(const iterator_base& a) const
62  {
63  return current_substr.first == a.current_substr.first;
64  }
65 
66  bool operator!=(const iterator_base& a) const
67  {
68  return ! (*this == a);
69  }
70 
72  {
73  current_substr.first = current_substr.second;
74  update();
75  return *this;
76  }
77 
78  char32_t operator*() const
79  {
80  return current_char;
81  }
82 
83  bool next_is_end() const
84  {
85  if(current_substr.second == string_end)
86  return true;
87  return false;
88  }
89 
90  const std::pair<typename string_type::const_iterator, typename string_type::const_iterator>& substr() const
91  {
92  return current_substr;
93  }
94  private:
95  void update()
96  {
97  assert(current_substr.first == current_substr.second);
98  if(current_substr.first == string_end)
99  return;
101  }
102 
103  char32_t current_char;
104  typename string_type::const_iterator string_end;
105  std::pair<typename string_type::const_iterator, typename string_type::const_iterator> current_substr;
106  };
107 
108 }
static iterator_base end(const string_type &str)
std::pair< typename string_type::const_iterator, typename string_type::const_iterator > current_substr
static iterator_base begin(const string_type &str)
iterator_base(typename string_type::const_iterator const &begin, typename string_type::const_iterator const &end)
const std::pair< typename string_type::const_iterator, typename string_type::const_iterator > & substr() const
string_type::const_iterator string_end
char32_t operator*() const
bool operator!=(const iterator_base &a) const
bool operator==(const iterator_base &a) const
iterator_base(const string_type &str)
std::input_iterator_tag iterator_category
iterator_base & operator++()
void read(config &cfg, std::istream &in, abstract_validator *validator)
Definition: parser.cpp:627
#define a