Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Tokeniser.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_TOKENISER_HPP
18#define TOOLBOX_UTIL_TOKENISER_HPP
19
20#include <algorithm>
21#include <array>
22#include <string_view>
23
24namespace toolbox {
25inline namespace util {
26using sv = std::string_view;
27
28class Tokeniser {
29 public:
30 constexpr Tokeniser(std::string_view buf, std::string_view delims) noexcept
31 {
33 }
34 constexpr Tokeniser() noexcept { reset(sv{""}, sv{""}); }
35 constexpr ~Tokeniser() = default;
36
37 // Copy.
38 constexpr Tokeniser(const Tokeniser&) noexcept = default;
39 constexpr Tokeniser& operator=(const Tokeniser&) noexcept = default;
40
41 // Move.
44
45 constexpr void reset(std::string_view buf, std::string_view delims) noexcept
46 {
47 buf_ = buf;
48 delims_ = delims;
49 i_ = buf_.cbegin();
50 j_ = std::find_first_of(i_, buf_.cend(), delims_.cbegin(), delims_.cend());
51 }
53 constexpr std::size_t consumed() const noexcept { return i_ - buf_.cbegin(); }
55 constexpr bool empty() const noexcept { return i_ == buf_.cend(); }
57 constexpr bool has_delim() const noexcept { return j_ != buf_.cend(); }
58 constexpr std::string_view top() const noexcept
59 {
60 return buf_.substr(i_ - buf_.cbegin(), j_ - i_);
61 }
62 constexpr std::string_view next() noexcept
63 {
64 const auto tok = top();
65 pop();
66 return tok;
67 }
68 constexpr void pop() noexcept
69 {
70 if (j_ != buf_.cend()) {
71 i_ = j_ + 1;
72 j_ = std::find_first_of(i_, buf_.cend(), delims_.cbegin(), delims_.cend());
73 } else {
74 i_ = j_;
75 }
76 }
77
78 private:
79 std::string_view buf_;
80 std::string_view delims_;
81 // Assumption: std::string_view iterators are equivalent across copies of the std::string_view.
82 std::string_view::const_iterator i_, j_;
83};
84
85template <typename FnT>
86constexpr std::size_t parse_line(std::string_view buf, FnT fn)
87{
88 Tokeniser lines{buf, sv{"\n"}};
89 while (lines.has_delim()) {
90 fn(lines.top());
91 lines.pop();
92 }
93 return lines.consumed();
94}
95
96template <std::size_t N>
97using Row = std::array<std::string_view, N>;
98
99template <std::size_t N>
100constexpr void split(std::string_view line, std::string_view delims, Row<N>& row) noexcept
101{
103 for (auto& col : row) {
104 if (toks.empty()) {
105 break;
106 }
107 col = toks.top();
108 toks.pop();
109 }
110}
111
112} // namespace util
113} // namespace toolbox
114
115#endif // TOOLBOX_UTIL_TOKENISER_HPP
constexpr std::string_view next() noexcept
Definition Tokeniser.hpp:62
constexpr Tokeniser(std::string_view buf, std::string_view delims) noexcept
Definition Tokeniser.hpp:30
constexpr Tokeniser(const Tokeniser &) noexcept=default
constexpr std::size_t consumed() const noexcept
Returns total bytes consumed.
Definition Tokeniser.hpp:53
constexpr Tokeniser(Tokeniser &&) noexcept=default
constexpr Tokeniser & operator=(const Tokeniser &) noexcept=default
constexpr void reset(std::string_view buf, std::string_view delims) noexcept
Definition Tokeniser.hpp:45
constexpr bool has_delim() const noexcept
Returns true if a delimiter was found in the remaining data.
Definition Tokeniser.hpp:57
constexpr bool empty() const noexcept
Returns true if all bytes have been consumed.
Definition Tokeniser.hpp:55
constexpr Tokeniser() noexcept
Definition Tokeniser.hpp:34
constexpr void pop() noexcept
Definition Tokeniser.hpp:68
constexpr ~Tokeniser()=default
constexpr std::string_view top() const noexcept
Definition Tokeniser.hpp:58
STL namespace.
constexpr std::size_t parse_line(std::string_view buf, FnT fn)
Definition Tokeniser.hpp:86
constexpr void split(std::string_view line, std::string_view delims, Row< N > &row) noexcept
std::array< std::string_view, N > Row
Definition Tokeniser.hpp:97
std::string_view sv
Definition Tokeniser.hpp:26
constexpr auto bind() noexcept
Definition Slot.hpp:92