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[[nodiscard, gnu::pure]] auto levenshtein_distance(
135 string_view first, string_view second) -> size_t;
136
140[[nodiscard]] inline auto to_owning_string(
141 const string_view text) -> std::string
142{
143 return std::string { text };
144}
145
147
148/*
149 ___ ,--,
150 ,---, ,--.'|_ ,--, ,--.'|
151 ,---.'| | | :,' ,--.'| | | :
152 | | : : : ' : | |, : : ' .--.--.
153 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
154 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
155 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
156. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
157' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
158| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
159| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
160 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
161 `----' `----' `--`---' ---`-' `--" `--" `--"
162
163 */
164
165template <std::integral Int>
166[[nodiscard]] constexpr Int int_from_string(
167 const string_view text, Int defaultValue) noexcept
168{
169 std::from_chars(
170 text.data(),
171 std::next(text.data(), static_cast<ptrdiff_t>(text.length())),
172 defaultValue);
173
174 return defaultValue;
175}
176
177template <size_t MaxLen>
179 const std::integral auto value,
180 std::string& output)
181{
182 std::array<char, MaxLen + 1uz> buffer { };
183
184 const auto result = std::to_chars(
185 buffer.data(),
186 std::next(buffer.data(), static_cast<ptrdiff_t>(buffer.size())),
187 value);
188
189 // simply do nothing on failure
190 if (result.ec != std::errc { })
191 return;
192
193 output.append(
194 buffer.data(),
195 static_cast<size_t>(std::distance(buffer.data(), result.ptr)));
196}
197
198inline auto split_by_delim(string_view text, char delim)
199{
200 return text
201 | std::views::split(delim)
202 | std::views::transform([](const auto rng) { return string_view { rng }; });
203}
204
205inline auto lines_view(const string_view text)
206{
207 return split_by_delim(text, '\n');
208}
209
210inline auto words_view(const string_view text)
211{
212 return split_by_delim(text, ' ');
213}
214
215} // namespace util::strings
std::pair< string_view, string_view > StringViewPair
Definition Strings.hpp:52
auto words_view(string_view text)
Definition Strings.hpp:210
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:166
auto lines_view(string_view text)
Definition Strings.hpp:205
auto split_by_delim(string_view text, char delim)
Definition Strings.hpp:198
void write_integer(std::integral auto value, std::string &output)
Definition Strings.hpp:178
auto levenshtein_distance(string_view first, string_view second) -> size_t
auto to_owning_string(const string_view text) -> std::string
Definition Strings.hpp:140
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 >