21#include <codeanalysis\warnings.h>
26#define FMT_HEADER_ONLY
30# include <codeanalysis\warnings.h>
35VSWARNING(ALL_CODE_ANALYSIS_WARNINGS)
38GNUWARNING("-Wstringop-overflow")
40# pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
42#include "../fmt/format.h"
46using wstring = std::wstring;
47using string = std::string;
48using string_view = std::string_view;
54BASE_EXPORT std::tuple<int32,bool> isInt32(
const string_view &in);
59BASE_EXPORT std::tuple<bool,bool>
isBool(
const string_view &in,
const string_view &out=
"on,off,true,false,yes,no");
62inline std::tuple<int32,bool> isInt32(
const string &in) {
return isInt32(string_view(in)); }
63inline std::tuple<uint32,bool>
isUInt32(
const string &in) {
return isUInt32(string_view(in)); }
64inline std::tuple<int64,bool>
isInt64(
const string &in) {
return isInt64(string_view(in)); }
65inline std::tuple<uint64,bool>
isUInt64(
const string &in) {
return isUInt64(string_view(in)); }
66inline std::tuple<double,bool>
isDouble(
const string &in) {
return isDouble(string_view(in)); }
67inline std::tuple<bool,bool>
isBool(
const string &in,
const string_view &out=
"on,off,true,false,yes,no") {
return isBool(string_view(in),out); }
68inline std::tuple<int32,bool> isInt32v(
const string_view &in) {
return isInt32(string_view(in)); }
69inline std::tuple<uint32,bool> isUInt32v(
const string_view &in) {
return isUInt32(string_view(in)); }
70inline std::tuple<int64,bool> isInt64v(
const string_view &in) {
return isInt64(string_view(in)); }
71inline std::tuple<uint64,bool> isUInt64v(
const string_view &in) {
return isUInt64(string_view(in)); }
72inline std::tuple<double,bool> isDoublev(
const string_view &in) {
return isDouble(string_view(in)); }
73inline std::tuple<bool,bool> isBoolv(
const string_view &in,
const string_view &out=
"on,off,true,false,yes,no") {
return isBool(string_view(in),out); }
78template <
typename T,
typename ... Args>
79T toStringConv(
const string_view &in,Args...args,
bool te, std::tuple<T,bool> (*f)(
const string_view &,Args...));
80inline int32 toInt32(
const string_view &in,
bool throwException =
false) {
return toStringConv<int32>(in, throwException, isInt32v); }
81inline uint32 toUInt32(
const string_view &in,
bool throwException=
false) {
return toStringConv<uint32>(in, throwException, isUInt32v); }
82inline int64 toInt64(
const string_view &in,
bool throwException=
false) {
return toStringConv<int64>(in, throwException, isInt64v); }
83inline uint64 toUInt64(
const string_view &in,
bool throwException=
false) {
return toStringConv<uint64>(in, throwException, isUInt64v); }
84inline double toDouble(
const string_view &in,
bool throwException=
false) {
return toStringConv<double>(in, throwException, isDoublev); }
85inline bool toBool(
const string_view &in,
const string_view &out =
"on,off,true,false,yes,no",
bool throwException =
false) {
return toStringConv<bool,const string_view &>(in, out, throwException, isBool); }
91 using std::vector<
string>::vector;
92 enum class Empty { Keep, Skip };
95 StringList(std::initializer_list<string> list) : std::vector<string>(list) {}
97 StringList(
const std::vector<string> &v) : std::vector<string>(v) {}
99 StringList(std::vector<string> &&v) noexcept : std::vector<string>(std::move(v)) {}
101 const StringList &operator=(
const StringList &in) { std::vector<string>::operator=(in);
return *
this; }
103 const StringList &operator+=(
const string &s) { push_back(s);
return *
this; }
104 const StringList &operator+=(
const std::vector<string> &v) {
for (
auto &s : v) push_back(s);
return *
this; }
105 const StringList &operator<<(
const string &s) {
return operator+=(s); }
110BASE_EXPORT std::vector<string> toStringList(
const std::vector<string_view> &v);
111BASE_EXPORT bool contains(
const std::vector<string> &all,
const string &s2,
bool caseSensitivity=
false);
114[[nodiscard]]
BASE_EXPORT string tostring(
const std::wstring &s);
115[[nodiscard]]
BASE_EXPORT string tostring(
const std::u16string &s);
116[[nodiscard]]
BASE_EXPORT string tostring(
const std::u32string &s);
117[[nodiscard]]
inline string tostring(
const string_view &s) {
return string(s.data(),s.size()); }
118[[nodiscard]]
BASE_EXPORT std::wstring towstring(
const string &s);
119[[nodiscard]]
BASE_EXPORT std::wstring towstring(
const std::u16string &s);
120[[nodiscard]]
BASE_EXPORT std::u16string tou16string(
const string &s);
121[[nodiscard]]
BASE_EXPORT std::u16string tou16string(
const std::wstring &s);
122[[nodiscard]]
BASE_EXPORT std::u16string tou16string(
const string_view &s);
123[[nodiscard]]
BASE_EXPORT std::u32string tou32string(
const string &s);
124[[nodiscard]]
BASE_EXPORT std::u32string tou32string(
const string_view &s);
125[[nodiscard]]
BASE_EXPORT string toUpper(
const string_view &s);
126[[nodiscard]]
BASE_EXPORT string toLower(
const string_view &s);
127[[nodiscard]]
BASE_EXPORT string capitalizeFirstLetter(
const string_view &s);
128template <
typename T,
typename U> [[nodiscard]] T join(
const std::vector<T> &s,
const U &sep);
129[[nodiscard]]
BASE_EXPORT std::vector<string_view> splitView(
const string_view &s,
const string_view &sep,
bool keepEmptyParts=
false);
130[[nodiscard]]
inline std::vector<string> split(
const string_view &s,
const string_view &sep,
bool keepEmptyParts=
false) {
return toStringList(splitView(s,sep,keepEmptyParts)); }
131[[nodiscard]]
BASE_EXPORT std::vector<string_view> splitViewRegex(
const string_view &s,
const string_view ®ex,
bool keepEmptyParts=
false);
132[[nodiscard]]
inline std::vector<string> splitRegex(
const string_view &s,
const string_view ®ex,
bool keepEmptyParts=
false) {
return toStringList(splitViewRegex(s,regex,keepEmptyParts)); }
133[[nodiscard]]
BASE_EXPORT string_view matchViewRegex(
const string_view &s,
const string_view ®ex,string::size_type start=0);
134[[nodiscard]]
inline string matchRegex(
const string_view &s,
const string_view ®ex,string::size_type start=0) {
return tostring(matchViewRegex(s,regex,start)); }
135[[nodiscard]]
BASE_EXPORT string replaceRegex(
const string &s,
const string_view ®ex,
const string &after);
136[[nodiscard]]
BASE_EXPORT string::size_type find(
const string_view &s1,
const string_view &s2,string::size_type start=0,
bool caseSensitive=
false);
137[[nodiscard]]
BASE_EXPORT string::size_type findRegex(
const string_view &s,
const string_view ®ex,string::size_type start=0);
138[[nodiscard]]
BASE_EXPORT bool exactMatchRegex(
const string_view &s,
const string_view ®ex);
139[[nodiscard]]
BASE_EXPORT string_view trimmed_view(
const string_view &s);
140[[nodiscard]]
inline string trimmed(
const string_view &s) {
return tostring(trimmed_view(s)); }
141[[nodiscard]]
BASE_EXPORT string simplified(
const string_view &s);
142[[nodiscard]]
BASE_EXPORT string replace(
string s,
const string_view &sub,
const string_view &newsub,
bool caseSensitive=
false);
143[[nodiscard]]
BASE_EXPORT string toBase64(
const std::vector<char> &in);
144[[nodiscard]]
BASE_EXPORT std::vector<char> fromBase64(
const string &in);
145[[nodiscard]]
BASE_EXPORT bool startsWith(
const string_view &in,
const string_view &check,
bool caseSensitive =
false);
146[[nodiscard]]
BASE_EXPORT bool endsWith(
const string_view &in,
const string_view &check,
bool caseSensitive =
false);
147[[nodiscard]]
BASE_EXPORT bool contains(
const string_view &in,
const string_view &check,
bool caseSensitive =
false);
148[[nodiscard]]
BASE_EXPORT string clipLen(
string in,string::size_type length);
149[[nodiscard]]
BASE_EXPORT string cleanupTypename(
const char *name);
150[[nodiscard]]
BASE_EXPORT string convertPercentToBracket(
const string_view &s);
151[[nodiscard]]
BASE_EXPORT string remove(
string s,
char c);
152[[nodiscard]]
inline string remove(
string s,
const string_view &sub,
bool caseSensitive=
false) {
return replace(s,sub,{},caseSensitive); }
153[[nodiscard]]
BASE_EXPORT int32 compare(
const string_view &s1,
const string_view &s2,
bool caseSensitive=
false);
154[[nodiscard]]
BASE_EXPORT uint64 caseInsensitiveHash(
const string_view &s);
155[[nodiscard]]
BASE_EXPORT uint64 firstNonBlankCharacter(
const string_view &s);
156[[nodiscard]]
BASE_EXPORT bool wildcardMatch(
const string_view &patter,
const string_view &test);
157[[nodiscard]]
BASE_EXPORT string formatBytes(uint64 bytes,uint32 precision=3);
163 using std::vector<
char>::vector;
164 Buffer(
const std::vector<char> &v) : std::vector<char>(v) {}
165 Buffer(std::vector<char> &&v) : std::vector<char>(std::move(v)) {}
166 Buffer(
const char *c,
size_t len) : std::vector<char>(c, c+len) {}
167 explicit Buffer(
const string &s) : std::vector<char>(s.begin(),s.end()) {}
169 const Buffer &operator=(
const std::vector<char> &v) { std::vector<char>::operator=(v);
return *
this; }
170 const Buffer &operator=(std::vector<char> &&v) { std::vector<char>::operator=(std::move(v));
return *
this; }
172 void append(
const char *data, uint64 len) { insert(end(),data, data+len); }
173 void operator+=(
const string &s) { insert(end(),s.begin(),s.end()); }
174 string toString()
const {
return string(begin(), end()); }
175 const char *constData()
const {
return data(); }
180BASE_EXPORT bool checkLeft(
const string_view &s,
const string_view &c);
181inline bool equal(
const string_view &s,
const string_view &c,
bool caseSensitive=
false) {
return compare(s,c,caseSensitive)==0; }
199BASE_EXPORT string buildFormat(int64 width,
char notation=
'\0',
int precision=-1,
char fill=
' ');
207 inline string ts(
const T &t,
int width=0,
char notation =
'\0',
int precision = -1,
char fill =
' ');
213 template <
typename T>
214 std::tuple<T, bool> fsTest(
const string &) {
static_assert(
sizeof(T{})==0);
return {T{},
false}; }
215 template<>
inline std::tuple<int32, bool> fsTest(
const string &in) {
return isInt32(in); }
216 template<>
inline std::tuple<uint32, bool> fsTest(
const string &in) {
return isUInt32(in); }
217 template<>
inline std::tuple<int64, bool> fsTest(
const string &in) {
return isInt64(in); }
218 template<>
inline std::tuple<uint64, bool> fsTest(
const string &in) {
return isUInt64(in); }
219 template<>
inline std::tuple<double, bool> fsTest(
const string &in) {
return isDouble(in); }
220 template<>
inline std::tuple<bool, bool> fsTest(
const string &in) {
return isBool(in); }
221 template<>
inline std::tuple<string, bool> fsTest(
const string &in) {
return {in,
true}; }
223 template <
typename T> T fs(
const string &, [[maybe_unused]]
bool throwException =
false) {
static_assert(
sizeof(T{})==0);
return T{}; }
224 template<>
inline int32 fs(
const string &in,
bool throwException) {
return toInt32(in,throwException); }
225 template<>
inline uint32 fs(
const string &in,
bool throwException) {
return toUInt32(in,throwException); }
226 template<>
inline int64 fs(
const string &in,
bool throwException) {
return toInt64(in,throwException); }
227 template<>
inline uint64 fs(
const string &in,
bool throwException) {
return toUInt64(in,throwException); }
228 template<>
inline double fs(
const string &in,
bool throwException) {
return toDouble(in,throwException); }
229 template<>
inline bool fs(
const string &in,
bool throwException) {
return toBool(in,
"on,off,true,false,yes,no",throwException); }
230 template<>
inline string fs(
const string &in,
bool) {
return {in,
true}; }
236 bool operator()(
const string &left,
const string &right)
const {
return compare(left,right) < 0; }
240 bool operator()(
const string &left,
const string &right)
const {
return compare(left,right) == 0; }
244 uint64 operator()(
const string &in)
const {
return caseInsensitiveHash(in); }
254#include <unordered_map>
256#include <unordered_set>
258template <
typename T>
using StringMap = std::map<string, T, StringCILess>;
260template <
typename T>
using StringMultiMap = std::multimap<string, T, StringCILess>;
262template <
typename T>
using StringHashMap = std::unordered_map<string, T, StringCIHash, StringCIEqual>;
264using StringSet = std::set<string, StringCILess>;
266using StringHashSet = std::unordered_set<string, StringCIHash, StringCIEqual>;
269inline string base::ts(
const T &t,
int width,
char notation,
int precision,
char fill) {
270 string s = fmt::format(buildFormat(width, notation, precision, fill), t);
272 return clipLen(s,std::abs(width));
280inline string base::ts(
const double &dIn,
int width,
char notation,
int precision,
char fill) {
284 return fmt::format(buildFormat(width, notation, precision, fill), d);
287template <
typename T,
typename U>
288T join(
const std::vector<T> &s,
const U &sep) {
290 for (StringList::size_type i=0;i<s.size();++i) {
301template <
typename T,
typename ... Args>
302T toStringConv(
const string_view &in,Args...args,
bool te, std::tuple<T,bool> (*f)(
const string_view &,Args...)) {
303 auto [val, ok] = f(in,args...);
304 if (te==
true && ok==
false)
306 throw std::runtime_error(
"String conversion error.");
308 throw std::exception(
"String conversion error.");
Base type definitions for the engine.
Definition basestring.h:161
Definition basestring.h:89
bool isDouble(const QString &in, double *d)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:271
BASE_EXPORT std::tuple< double, bool > isDouble(const string_view &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:61
BASE_EXPORT std::tuple< uint64, bool > isUInt64(const string_view &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:52
bool isBool(const QString &in, bool *b, const QString &out)
Definition baseqstring.cpp:229
bool toBool(const QString &in, bool *ok, const QString &out)
Definition baseqstring.cpp:278
BASE_EXPORT std::tuple< uint32, bool > isUInt32(const string_view &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:34
#define BASE_EXPORT
Definition basedef.h:24
BASE_EXPORT bool match(const string &keyword, const string &token)
**
Definition basestring.cpp:462
BASE_EXPORT std::tuple< int64, bool > isInt64(const string_view &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:43
Definition basestring.h:238
Definition basestring.h:242
Definition basestring.h:234
A overflow checked shorthand for static_cast<T>().