BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Strings.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
25
26#pragma once
27
28#include <array>
29#include <charconv>
30#include <concepts>
31#include <cstddef> // IWYU pragma: keep - for size_t
32#include <expected>
33#include <iterator>
34#include <ranges>
35#include <string>
36#include <string_view>
37#include <system_error>
38#include <utility>
39
43namespace util::strings {
44
45using std::ptrdiff_t;
46using std::size_t;
47using std::string_view;
48
52using StringViewPair = std::pair<string_view, string_view>;
53
56
60[[nodiscard]] auto trim(string_view text) -> string_view;
61
71[[nodiscard]] auto split_at_first_space(string_view input) -> StringViewPair;
72
77[[nodiscard]] auto split_at_first_space_or_newline(string_view input) -> StringViewPair;
78
86[[nodiscard]] auto find_matching_close_paren(string_view input)
87 -> std::expected<size_t, std::string>;
88
93template <std::integral Int>
94[[nodiscard]] constexpr auto int_from_string(
95 string_view text, Int defaultValue = 0) noexcept
96 -> Int;
97
107template <size_t MaxLen = 5uz>
108void write_integer(
109 std::integral auto value,
110 std::string& output);
111
117[[nodiscard]] auto split_by_delim(string_view text, char delim);
118
124[[nodiscard]] auto lines_view(string_view text);
125
131[[nodiscard]] auto words_view(string_view text);
132
134
135/*
136 ___ ,--,
137 ,---, ,--.'|_ ,--, ,--.'|
138 ,---.'| | | :,' ,--.'| | | :
139 | | : : : ' : | |, : : ' .--.--.
140 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
141 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
142 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
143. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
144' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
145| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
146| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
147 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
148 `----' `----' `--`---' ---`-' `--" `--" `--"
149
150 */
151
152template <std::integral Int>
153[[nodiscard]] constexpr Int int_from_string(
154 const string_view text, Int defaultValue) noexcept
155{
156 std::from_chars(
157 text.data(),
158 std::next(text.data(), static_cast<ptrdiff_t>(text.length())),
159 defaultValue);
160
161 return defaultValue;
162}
163
164template <size_t MaxLen>
166 const std::integral auto value,
167 std::string& output)
168{
169 std::array<char, MaxLen + 1uz> buffer { };
170
171 const auto result = std::to_chars(
172 buffer.data(),
173 std::next(buffer.data(), static_cast<ptrdiff_t>(buffer.size())),
174 value);
175
176 // simply do nothing on failure
177 if (result.ec != std::errc { })
178 return;
179
180 output.append(
181 buffer.data(),
182 static_cast<size_t>(std::distance(buffer.data(), result.ptr)));
183}
184
185inline auto split_by_delim(string_view text, char delim)
186{
187 return text
188 | std::views::split(delim)
189 | std::views::transform([](const auto rng) { return string_view { rng }; });
190}
191
192inline auto lines_view(const string_view text)
193{
194 return split_by_delim(text, '\n');
195}
196
197inline auto words_view(const string_view text)
198{
199 return split_by_delim(text, ' ');
200}
201
202} // namespace util::strings
std::pair< string_view, string_view > StringViewPair
Definition Strings.hpp:52
auto words_view(string_view text)
Definition Strings.hpp:197
auto trim(string_view text) -> string_view
auto split_at_first_space(string_view input) -> StringViewPair
constexpr auto int_from_string(string_view text, Int defaultValue=0) noexcept -> Int
Definition Strings.hpp:153
auto lines_view(string_view text)
Definition Strings.hpp:192
auto split_by_delim(string_view text, char delim)
Definition Strings.hpp:185
void write_integer(std::integral auto value, std::string &output)
Definition Strings.hpp:165
auto split_at_first_space_or_newline(string_view input) -> StringViewPair
auto find_matching_close_paren(string_view input) -> std::expected< size_t, std::string >