// SPDX-License-Identifier: 0BSD


#pragma once

#include <iterator>
#include <string_view>


namespace vore {
	namespace {
		struct soft_tokenise {
			using iterator          = soft_tokenise;
			using iterator_category = std::input_iterator_tag;
			using difference_type   = void;
			using value_type        = std::string_view;
			using pointer           = std::string_view *;
			using reference         = std::string_view &;


			std::string_view remaining;
			std::string_view delim;
			std::string_view token = {};


			iterator & operator++() noexcept {
				this->remaining.remove_prefix(this->remaining.find_first_not_of(this->delim));
				if(auto len = this->remaining.find_first_of(this->delim); len != std::string_view::npos) {
					this->token = {this->remaining.data(), len};
					this->remaining.remove_prefix(len);
				} else
					this->token = {};
				return *this;
			}


			iterator begin() noexcept { return ++*this; }
			constexpr iterator end() const noexcept { return {}; }


			constexpr bool operator==(const iterator & rhs) const noexcept { return this->token.data() == rhs.token.data(); }

			constexpr std::string_view operator*() const noexcept { return this->token; }
		};
	}
}
