Util/IterableSplitString: add template class BasicIterableSplitString

This commit is contained in:
Max Kellermann 2017-09-12 19:11:22 +02:00
parent 9e7df54ccd
commit f98bf39005

View File

@ -42,23 +42,28 @@
* *
* An empty input string returns one empty string. * An empty input string returns one empty string.
*/ */
class IterableSplitString { template<typename T>
StringView s; class BasicIterableSplitString {
typedef BasicStringView<T> StringView;
char separator; typedef typename StringView::value_type value_type;
StringView s;
value_type separator;
public: public:
constexpr IterableSplitString(StringView _s, char _separator) constexpr BasicIterableSplitString(StringView _s,
value_type _separator)
:s(_s), separator(_separator) {} :s(_s), separator(_separator) {}
class Iterator final { class Iterator final {
friend class IterableSplitString; friend class BasicIterableSplitString;
StringView current, rest; StringView current, rest;
char separator; value_type separator;
Iterator(StringView _s, char _separator) Iterator(StringView _s, value_type _separator)
:rest(_s), separator(_separator) { :rest(_s), separator(_separator) {
Next(); Next();
} }
@ -70,7 +75,7 @@ public:
if (rest.IsNull()) if (rest.IsNull())
current = nullptr; current = nullptr;
else { else {
const char *i = rest.Find(separator); const auto *i = rest.Find(separator);
if (i == nullptr) { if (i == nullptr) {
current = rest; current = rest;
rest.data = nullptr; rest.data = nullptr;
@ -120,4 +125,6 @@ public:
} }
}; };
using IterableSplitString = BasicIterableSplitString<char>;
#endif #endif