libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
stringhelpers.h
Go to the documentation of this file.
1
22
23#ifndef STRINGHELPERS_H
24#define STRINGHELPERS_H
25
26#include <cstddef>
27#include <cstdint>
28#include <string>
29#include <type_traits>
30#include <vector>
31
33{
34 template<typename T>
35 concept StringImplicitlyConstructible = std::is_constructible_v<T, std::string> && std::is_convertible_v<std::string, T>;
36
42 std::vector<std::byte> decode(const std::string& base64) noexcept;
48 std::string encode(const std::vector<std::byte>& bytes) noexcept;
54 bool isValidUrl(const std::string& s) noexcept;
62 std::string join(const std::vector<std::string>& values, const std::string& separator, bool separateLast = false) noexcept;
68 std::string lower(std::string s) noexcept;
73 std::string newUuid() noexcept;
79 std::string newGuid() noexcept;
86 std::string normalizeForFilename(const std::string& s, bool windowsOnly = false) noexcept;
92 std::string quote(const std::string& s) noexcept;
100 std::string replace(std::string s, const std::string& toReplace, const std::string& replace) noexcept;
108 std::string replace(std::string s, char toReplace, char replace) noexcept;
114 std::vector<std::string> splitArgs(std::string s) noexcept;
120 std::string str(const std::wstring& s) noexcept;
130 unsigned int stoui(const std::string& s, size_t* idx = nullptr, int base = 10) noexcept;
136 std::string trim(const std::string& s) noexcept;
143 std::string trim(const std::string& s, char delimiter) noexcept;
149 std::string upper(std::string s) noexcept;
155 std::wstring wstr(const std::string& s) noexcept;
164 template<StringImplicitlyConstructible T = std::string>
165 std::vector<T> split(const std::string& s, const std::string& delimiter, bool includeEmpty = true) noexcept
166 {
167 std::vector<T> splits;
168 size_t last{ 0 };
169 size_t next{ 0 };
170 while((next = s.find(delimiter, last)) != std::string::npos)
171 {
172 std::string token{ s.substr(last, next - last) };
173 if(includeEmpty || !trim(token).empty())
174 {
175 splits.push_back(token);
176 }
177 last = next + delimiter.length();
178
179 }
180 std::string finalToken{ s.substr(last) };
181 if(includeEmpty || !trim(finalToken).empty())
182 {
183 splits.push_back(finalToken);
184 }
185 return splits;
186 }
187
195 template<StringImplicitlyConstructible T = std::string>
196 std::vector<T> split(const std::string& s, char delimiter, bool includeEmpty = true) noexcept
197 {
198 return split<T>(s, std::string(1, delimiter), includeEmpty);
199 }
200}
201
202#endif // STRINGHELPERS_H
Definition stringhelpers.h:33
unsigned int stoui(const std::string &s, size_t *idx=nullptr, int base=10) noexcept
Converts a string to an unsigned int.
std::vector< std::byte > decode(const std::string &base64) noexcept
Converts a base64 encoded string into a list of bytes.
std::string replace(std::string s, const std::string &toReplace, const std::string &replace) noexcept
Replaces a substring within a string with a new string.
std::string trim(const std::string &s) noexcept
Trims whitespace form the beginning and end of a string.
std::string normalizeForFilename(const std::string &s, bool windowsOnly=false) noexcept
Normalizes a string for use in a filename.
std::string newUuid() noexcept
Generates a new uuid value.
std::string str(const std::wstring &s) noexcept
Converts the wstring to a string.
std::string lower(std::string s) noexcept
Gets a fully lowercase string from the provided string.
bool isValidUrl(const std::string &s) noexcept
Gets whether or not the provided string is a valid url.
std::string newGuid() noexcept
Generates a new guid value.
std::string join(const std::vector< std::string > &values, const std::string &separator, bool separateLast=false) noexcept
Concatenates the elements of a string list using the specified separator between each element.
std::vector< std::string > splitArgs(std::string s) noexcept
Splits a string based on argument delimiters.
std::string encode(const std::vector< std::byte > &bytes) noexcept
Converts a list of bytes into a base64 encoded string.
std::string upper(std::string s) noexcept
Gets a fully uppercase string from the provided string.
std::string quote(const std::string &s) noexcept
Quotes a string for use in a command line.
std::wstring wstr(const std::string &s) noexcept
Converts the string to a wstring.
std::vector< T > split(const std::string &s, const std::string &delimiter, bool includeEmpty=true) noexcept
Splits a string based on a delimiter.
Definition stringhelpers.h:165