MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
45 class IterableSplitString {
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
89 Iterator &operator++() {
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
111 typedef Iterator iterator;
112 typedef Iterator const_iterator;
113
114 const_iterator begin() const {
115 return {s, separator};
116 }
117
118 constexpr const_iterator end() const {
119 return {nullptr};
120 }
121 };
122
123 #endif
StringView::Find
gcc_pure pointer_type Find(char ch) const
Definition: StringView.hxx:73
IterableSplitString::Iterator::operator*
constexpr StringView operator*() const
Definition: IterableSplitString.hxx:102
IterableSplitString::iterator
Iterator iterator
Definition: IterableSplitString.hxx:111
IterableSplitString::IterableSplitString
constexpr IterableSplitString(StringView _s, char _separator)
Definition: IterableSplitString.hxx:51
IterableSplitString::begin
const_iterator begin() const
Definition: IterableSplitString.hxx:114
IterableSplitString::end
constexpr const_iterator end() const
Definition: IterableSplitString.hxx:118
IterableSplitString
Split a string at a certain separator character into sub strings and allow iterating over the segment...
Definition: IterableSplitString.hxx:45
IterableSplitString::Iterator::operator++
Iterator & operator++()
Definition: IterableSplitString.hxx:89
ConstBuffer::size
size_type size
Definition: ConstBuffer.hxx:97
IterableSplitString::Iterator::operator!=
constexpr bool operator!=(Iterator other) const
Definition: IterableSplitString.hxx:98
IterableSplitString::Iterator::operator==
constexpr bool operator==(Iterator other) const
Definition: IterableSplitString.hxx:94
StringView
Definition: StringView.hxx:37
StringView.hxx
ConstBuffer::data
pointer_type data
Definition: ConstBuffer.hxx:96
IterableSplitString::Iterator
Definition: IterableSplitString.hxx:54
ConstBuffer::IsNull
constexpr bool IsNull() const
Definition: ConstBuffer.hxx:133
IterableSplitString::Iterator::operator->
constexpr const StringView * operator->() const
Definition: IterableSplitString.hxx:106
IterableSplitString::Iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: IterableSplitString.hxx:87
IterableSplitString::const_iterator
Iterator const_iterator
Definition: IterableSplitString.hxx:112