nfx-stringutils 1.2.0
Cross-platform C++ string utilities library with validation and manipulation functions
Loading...
Searching...
No Matches
Splitter.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
31
32#pragma once
33
34#include <cstddef>
35#include <iterator>
36#include <string_view>
37
38namespace nfx::string
39{
40 //=====================================================================
41 // Splitter class
42 //=====================================================================
43
49 {
50 public:
51 //----------------------------------------------
52 // Forward declarations
53 //----------------------------------------------
54
55 class Iterator;
56
57 //----------------------------------------------
58 // Construction
59 //----------------------------------------------
60
68 template <typename String>
69 inline explicit Splitter( String&& str, char delimiter ) noexcept;
70
71 //----------------------------------------------
72 // Iteration
73 //----------------------------------------------
74
79 inline Iterator begin() const noexcept;
80
85 inline Iterator end() const noexcept;
86
87 //----------------------------------------------
88 // Splitter::Iterator class
89 //----------------------------------------------
90
95 {
96 public:
97 //-----------------------------
98 // Iterator traits
99 //-----------------------------
100
105 using iterator_category = std::forward_iterator_tag;
106
111 using value_type = std::string_view;
112
117 using difference_type = std::ptrdiff_t;
118
123 using pointer = const std::string_view*;
124
129 using reference = std::string_view;
130
131 //-----------------------------
132 // Construction
133 //-----------------------------
134
139 inline Iterator() noexcept = default;
140
146 inline explicit Iterator( const Splitter& splitter, bool at_end = false ) noexcept;
147
148 //-----------------------------
149 // Iterator operators
150 //-----------------------------
151
156 inline std::string_view operator*() const noexcept;
157
162 inline Iterator& operator++() noexcept;
163
168 inline Iterator operator++( int ) noexcept;
169
170 //-----------------------------
171 // Comparison operators
172 //-----------------------------
173
179 inline bool operator==( const Iterator& other ) const noexcept;
180
186 inline bool operator!=( const Iterator& other ) const noexcept;
187
188 private:
189 //-----------------------------
190 // Private member variables
191 //-----------------------------
192
193 const Splitter* m_splitter{ nullptr };
194 size_t m_start{};
195 size_t m_end{};
196 bool m_isAtEnd{ true };
197 };
198
199 private:
200 std::string_view m_str;
201 char m_delimiter;
202 };
203
204 //=====================================================================
205 // String splitting factory functions
206 //=====================================================================
207
219 template <typename String>
220 [[nodiscard]] inline Splitter splitView( String&& str, char delimiter ) noexcept;
221} // namespace nfx::string
222
223#include "nfx/detail/string/Splitter.inl"
Splitter splitView(String &&str, char delimiter) noexcept
Templated factory function for zero-copy string splitting.
Zero-allocation string splitting iterator for performance-critical paths.
Definition Splitter.h:49
Splitter(String &&str, char delimiter) noexcept
Constructs a Splitter for the given string and delimiter.
Iterator end() const noexcept
Returns end iterator for range-based loops.
Iterator begin() const noexcept
Returns iterator to first segment.
Forward iterator for string segments.
Definition Splitter.h:95
std::string_view reference
Reference type returned by dereferencing.
Definition Splitter.h:129
std::string_view value_type
Type of values returned by dereferencing the iterator.
Definition Splitter.h:111
const std::string_view * pointer
Pointer type to the value_type.
Definition Splitter.h:123
std::ptrdiff_t difference_type
Type for representing distances between iterators.
Definition Splitter.h:117
Iterator() noexcept=default
Default constructor.
std::forward_iterator_tag iterator_category
Iterator category tag.
Definition Splitter.h:105