MPD  0.20.18
IterableSplitString.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2016 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef ITERABLE_SPLIT_STRING_HXX
31 #define ITERABLE_SPLIT_STRING_HXX
32 
33 #include "StringView.hxx"
34 
35 #include <iterator>
36 
46  StringView s;
47 
48  char separator;
49 
50 public:
51  constexpr IterableSplitString(StringView _s, char _separator)
52  :s(_s), separator(_separator) {}
53 
54  class Iterator final {
55  friend class IterableSplitString;
56 
57  StringView current, rest;
58 
59  char separator;
60 
61  Iterator(StringView _s, char _separator)
62  :rest(_s), separator(_separator) {
63  Next();
64  }
65 
66  constexpr Iterator(std::nullptr_t n)
67  :current(n), rest(n), separator(0) {}
68 
69  void Next() {
70  if (rest.IsNull())
71  current = nullptr;
72  else {
73  const char *i = rest.Find(separator);
74  if (i == nullptr) {
75  current = rest;
76  rest.data = nullptr;
77  } else {
78  current.data = rest.data;
79  current.size = i - current.data;
80  rest.size -= current.size + 1;
81  rest.data = i + 1;
82  }
83  }
84  }
85 
86  public:
87  typedef std::forward_iterator_tag iterator_category;
88 
90  Next();
91  return *this;
92  }
93 
94  constexpr bool operator==(Iterator other) const {
95  return current.data == other.current.data;
96  }
97 
98  constexpr bool operator!=(Iterator other) const {
99  return !(*this == other);
100  }
101 
102  constexpr StringView operator*() const {
103  return current;
104  }
105 
106  constexpr const StringView *operator->() const {
107  return &current;
108  }
109  };
110 
113 
115  return {s, separator};
116  }
117 
118  constexpr const_iterator end() const {
119  return {nullptr};
120  }
121 };
122 
123 #endif
constexpr IterableSplitString(StringView _s, char _separator)
constexpr bool operator==(Iterator other) const
constexpr bool operator!=(Iterator other) const
gcc_pure pointer_type Find(char ch) const noexcept
Definition: StringView.hxx:73
Split a string at a certain separator character into sub strings and allow iterating over the segment...
constexpr StringView operator*() const
size_type size
Definition: ConstBuffer.hxx:97
constexpr const_iterator end() const
constexpr bool IsNull() const
const_iterator begin() const
constexpr const StringView * operator->() const
pointer_type data
Definition: ConstBuffer.hxx:96
std::forward_iterator_tag iterator_category