libnick 2024.9.2
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
stringhelpers.h
Go to the documentation of this file.
1
23#ifndef STRINGHELPERS_H
24#define STRINGHELPERS_H
25
26#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
27
28#include <cstddef>
29#include <cstdint>
30#include <string>
31#include <type_traits>
32#include <vector>
33
35{
36 template<typename T>
37 concept StringImplicitlyConstructible = std::is_constructible_v<T, std::string> && std::is_convertible_v<std::string, T>;
38
44 std::vector<std::byte> decode(const std::string& base64);
50 std::string encode(const std::vector<std::byte>& bytes);
56 bool isValidUrl(const std::string& s);
64 std::string join(const std::vector<std::string>& values, const std::string& separator, bool separateLast = false);
70 std::string lower(std::string s);
75 std::string newUuid();
81 std::string newGuid();
88 std::string normalizeForFilename(const std::string& s, bool windowsOnly = false);
96 std::string replace(std::string s, const std::string& toReplace, const std::string& replace);
104 std::string replace(std::string s, char toReplace, char replace);
112 template<StringImplicitlyConstructible T = std::string>
113 std::vector<T> split(std::string s, const std::string& delimiter)
114 {
115 if(s.empty())
116 {
117 return { "" };
118 }
119 std::vector<T> splits;
120 if(delimiter.empty())
121 {
122 splits.push_back(s);
123 return splits;
124 }
125 size_t pos{ 0 };
126 while ((pos = s.find(delimiter)) != std::string::npos)
127 {
128 std::string split{ s.substr(0, pos) };
129 splits.push_back(split);
130 s.erase(0, pos + 1);
131 }
132 if (!s.empty())
133 {
134 splits.push_back(s);
135 }
136 return splits;
137 }
143 std::vector<std::string> splitArgs(std::string s);
149 std::string str(const std::wstring& s);
159 unsigned int stoui(const std::string& s, size_t* idx = nullptr, int base = 10);
165 std::string trim(const std::string& s);
172 std::string trim(const std::string& s, char delimiter);
178 std::string upper(std::string s);
184 std::wstring wstr(const std::string& s);
185}
186
187#endif // STRINGHELPERS_H
Definition stringhelpers.h:35
std::string str(const std::wstring &s)
Converts the wstring to a string.
std::string encode(const std::vector< std::byte > &bytes)
Converts a list of bytes into a base64 encoded string.
std::string newUuid()
Generates a new uuid value.
bool isValidUrl(const std::string &s)
Gets whether or not the provided string is a valid url.
std::vector< std::string > splitArgs(std::string s)
Splits a string based on argument delimiters.
std::wstring wstr(const std::string &s)
Converts the string to a wstring.
std::string lower(std::string s)
Gets a fully lowercase string from the provided string.
std::vector< std::byte > decode(const std::string &base64)
Converts a base64 encoded string into a list of bytes.
std::string normalizeForFilename(const std::string &s, bool windowsOnly=false)
Normalizes a string for use in a filename.
std::string trim(const std::string &s)
Trims whitespace form the beginning and end of a string.
std::string upper(std::string s)
Gets a fully uppercase string from the provided string.
std::string newGuid()
Generates a new guid value.
std::string join(const std::vector< std::string > &values, const std::string &separator, bool separateLast=false)
Concatenates the elements of a string list using the specified separator between each element.
std::vector< T > split(std::string s, const std::string &delimiter)
Splits a string based on a delimiter.
Definition stringhelpers.h:113
std::string replace(std::string s, const std::string &toReplace, const std::string &replace)
Replaces a substring within a string with a new string.
unsigned int stoui(const std::string &s, size_t *idx=nullptr, int base=10)
Converts a string to an unsigned int.