1#ifndef LXGUI_UTILS_STRING_HPP
2#define LXGUI_UTILS_STRING_HPP
4#include "lxgui/lxgui.hpp"
5#include "lxgui/utils.hpp"
6#include "lxgui/utils_variant.hpp"
12#include <magic_enum/magic_enum.hpp>
22using string = std::string;
23using string_view = std::string_view;
24using ustring = std::u32string;
25using ustring_view = std::u32string_view;
27[[nodiscard]] string_view trim(string_view s,
char c_pattern);
28[[nodiscard]] string_view trim(string_view s, string_view patterns);
30void replace(
string& s, string_view pattern, string_view replacement);
32[[nodiscard]] std::size_t count_occurrences(string_view s, string_view pattern);
34[[nodiscard]] std::vector<string_view> cut(string_view s, string_view delim);
35[[nodiscard]] std::vector<ustring_view> cut(ustring_view s, ustring_view delim);
37[[nodiscard]] std::vector<string_view> cut_each(string_view s, string_view delim);
38[[nodiscard]] std::vector<ustring_view> cut_each(ustring_view s, ustring_view delim);
40[[nodiscard]] std::pair<string_view, string_view> cut_first(string_view s, string_view delim);
41[[nodiscard]] std::pair<ustring_view, ustring_view> cut_first(ustring_view s, ustring_view delim);
43[[nodiscard]]
bool starts_with(string_view s, string_view pattern);
44[[nodiscard]]
bool ends_with(string_view s, string_view pattern);
46[[nodiscard]]
bool has_no_content(string_view s);
48[[nodiscard]] ustring utf8_to_unicode(string_view s);
49[[nodiscard]]
string unicode_to_utf8(ustring_view s);
51[[nodiscard]] std::size_t hex_to_uint(string_view s);
56std::optional<T> from_string(
const std::locale&, string_view);
59std::optional<int> from_string<int>(
const std::locale&, string_view);
62std::optional<long> from_string<long>(
const std::locale&, string_view);
65std::optional<long long> from_string<long long>(
const std::locale&, string_view);
68std::optional<unsigned> from_string<unsigned>(
const std::locale&, string_view);
71std::optional<unsigned long> from_string<unsigned long>(
const std::locale&, string_view);
74std::optional<unsigned long long> from_string<unsigned long long>(
const std::locale&, string_view);
77std::optional<float> from_string<float>(
const std::locale&, string_view);
80std::optional<double> from_string<double>(
const std::locale&, string_view);
83std::optional<T> from_string(
const std::locale&, ustring_view);
86std::optional<int> from_string<int>(
const std::locale&, ustring_view);
89std::optional<long> from_string<long>(
const std::locale&, ustring_view);
92std::optional<long long> from_string<long long>(
const std::locale&, ustring_view);
95std::optional<unsigned> from_string<unsigned>(
const std::locale&, ustring_view);
98std::optional<unsigned long> from_string<unsigned long>(
const std::locale&, ustring_view);
101std::optional<unsigned long long> from_string<unsigned long long>(
const std::locale&, ustring_view);
104std::optional<float> from_string<float>(
const std::locale&, ustring_view);
107std::optional<double> from_string<double>(
const std::locale&, ustring_view);
110std::optional<T> from_string(string_view);
113std::optional<int> from_string<int>(string_view);
116std::optional<long> from_string<long>(string_view);
119std::optional<long long> from_string<long long>(string_view);
122std::optional<unsigned> from_string<unsigned>(string_view);
125std::optional<unsigned long> from_string<unsigned long>(string_view);
128std::optional<unsigned long long> from_string<unsigned long long>(string_view);
131std::optional<float> from_string<float>(string_view);
134std::optional<double> from_string<double>(string_view);
137std::optional<bool> from_string<bool>(string_view);
140std::optional<string> from_string<string>(string_view);
143std::optional<T> from_string(ustring_view);
146std::optional<int> from_string<int>(ustring_view);
149std::optional<long> from_string<long>(ustring_view);
152std::optional<long long> from_string<long long>(ustring_view);
155std::optional<unsigned> from_string<unsigned>(ustring_view);
158std::optional<unsigned long> from_string<unsigned long>(ustring_view);
161std::optional<unsigned long long> from_string<unsigned long long>(ustring_view);
164std::optional<float> from_string<float>(ustring_view);
167std::optional<double> from_string<double>(ustring_view);
170std::optional<bool> from_string<bool>(ustring_view);
173std::optional<ustring> from_string<ustring>(ustring_view);
178[[nodiscard]] std::optional<T> from_string(
const std::locale& loc, string_view s) {
179 if constexpr (std::is_enum_v<T>) {
180 return magic_enum::enum_cast<T>(s, magic_enum::case_insensitive);
182 return impl::from_string<T>(loc, s);
187[[nodiscard]] std::optional<T> from_string(
const std::locale& loc, ustring_view s) {
188 if constexpr (std::is_enum_v<T>) {
189 return magic_enum::enum_cast<T>(unicode_to_utf8(s), magic_enum::case_insensitive);
191 return impl::from_string<T>(loc, s);
196[[nodiscard]] std::optional<T> from_string(string_view s) {
197 if constexpr (std::is_enum_v<T>) {
198 return magic_enum::enum_cast<T>(s, magic_enum::case_insensitive);
200 return impl::from_string<T>(s);
205[[nodiscard]] std::optional<T> from_string(ustring_view s) {
206 if constexpr (std::is_enum_v<T>) {
207 return magic_enum::enum_cast<T>(unicode_to_utf8(s), magic_enum::case_insensitive);
209 return impl::from_string<T>(s);
213[[nodiscard]]
bool is_number(
const std::locale& loc, string_view s);
214[[nodiscard]]
bool is_number(
const std::locale& loc, ustring_view s);
215[[nodiscard]]
bool is_integer(
const std::locale& loc, string_view s);
216[[nodiscard]]
bool is_integer(
const std::locale& loc, ustring_view s);
218[[nodiscard]]
bool is_number(string_view s);
219[[nodiscard]]
bool is_number(ustring_view s);
220[[nodiscard]]
bool is_integer(string_view s);
221[[nodiscard]]
bool is_integer(ustring_view s);
223[[nodiscard]]
bool is_number(
char s);
224[[nodiscard]]
bool is_number(
char32_t s);
225[[nodiscard]]
bool is_integer(
char s);
226[[nodiscard]]
bool is_integer(
char32_t s);
228[[nodiscard]]
bool is_boolean(string_view s);
229[[nodiscard]]
bool is_boolean(ustring_view s);
231[[nodiscard]]
bool is_whitespace(
char c);
232[[nodiscard]]
bool is_whitespace(
char32_t c);
234[[nodiscard]]
string to_string(
int v);
235[[nodiscard]]
string to_string(
long v);
236[[nodiscard]]
string to_string(
long long v);
237[[nodiscard]]
string to_string(
unsigned v);
238[[nodiscard]]
string to_string(
unsigned long v);
239[[nodiscard]]
string to_string(
unsigned long long v);
240[[nodiscard]]
string to_string(
float v);
241[[nodiscard]]
string to_string(
double v);
242[[nodiscard]]
string to_string(
bool v);
243[[nodiscard]]
string to_string(
bool b);
244[[nodiscard]]
string to_string(
const void* p);
246template<
typename T,
typename enable = std::enable_if_t<std::is_enum_v<T>>>
247[[nodiscard]]
string to_string(T v) {
248 return string{magic_enum::enum_name(v)};
252[[nodiscard]]
string to_string(
const T* p) {
253 return p !=
nullptr ? to_string(
static_cast<const void*
>(p)) :
"null";
257[[nodiscard]]
string to_string(T* p) {
258 return p !=
nullptr ? to_string(
static_cast<void*
>(p)) :
"null";
263template<
typename... Args>
264[[nodiscard]] ustring to_ustring(Args&&... args) {
265 return utils::utf8_to_unicode(to_string(std::forward<Args>(args)...));
268[[nodiscard]]
string to_lower(
string str);
std::variant< empty, bool, std::int64_t, std::int32_t, std::int16_t, std::int8_t, std::uint64_t, std::uint32_t, std::uint16_t, std::uint8_t, double, float, std::string > variant
Type-erased value for passing arguments to events.