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