13 #include <string_view>
15 #include <codeanalysis\warnings.h>
20 #define FMT_HEADER_ONLY
24 # include <codeanalysis\warnings.h>
29 VSWARNING(ALL_CODE_ANALYSIS_WARNINGS)
32 GNUWARNING("-Wstringop-overflow")
34 # pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
36 #include "../fmt/format.h"
40 using wstring = std::wstring;
41 using string = std::string;
47 BASE_EXPORT std::tuple<int32,bool> isInt32(
const string &in);
52 BASE_EXPORT std::tuple<bool,bool>
isBool(
const string &in,
const string &out=
"on,off,true,false,yes,no");
57 template <
typename T,
typename ... Args>
58 T toStringConv(
const string &in,Args...args,
bool te, std::tuple<T,bool> (*f)(
const string &,Args...)) {
59 auto [val, ok] = f(in,args...);
60 if (te==
true && ok==
false)
62 throw std::runtime_error(
"String conversion error.");
64 throw std::exception(
"String conversion error.");
68 inline int32 toInt32(
const string &in,
bool throwException =
false) {
return toStringConv<int32>(in, throwException, isInt32); }
69 inline uint32 toUInt32(
const string &in,
bool throwException=
false) {
return toStringConv<uint32>(in, throwException,
isUInt32); }
70 inline int64 toInt64(
const string &in,
bool throwException=
false) {
return toStringConv<int64>(in, throwException,
isInt64); }
71 inline uint64 toUInt64(
const string &in,
bool throwException=
false) {
return toStringConv<uint64>(in, throwException,
isUInt64); }
72 inline double toDouble(
const string &in,
bool throwException=
false) {
return toStringConv<double>(in, throwException,
isDouble); }
73 inline bool toBool(
const string &in,
const string &out =
"on,off,true,false,yes,no",
bool throwException =
false) {
return toStringConv<bool,const string &>(in, out, throwException, isBool); }
79 using std::vector<string>::vector;
80 enum class Empty { Keep, Skip };
83 StringList(std::initializer_list<string> list) : std::vector<string>(list) {}
85 StringList(
const std::vector<string> &v) : std::vector<string>(v) {}
87 StringList(std::vector<string> &&v) noexcept : std::vector<string>(std::move(v)) {}
89 const StringList &operator=(
const StringList &in) { std::vector<string>::operator=(in);
return *
this; }
91 const StringList &operator+=(
const string &s) { push_back(s);
return *
this; }
92 const StringList &operator+=(
const std::vector<string> &v) {
for (
auto &s : v) push_back(s);
return *
this; }
93 const StringList &operator<<(
const string &s) {
return operator+=(s); }
100 BASE_EXPORT string tostring(
const std::wstring &s);
101 BASE_EXPORT string tostring(
const std::u16string &s);
102 BASE_EXPORT string tostring(
const std::u32string &s);
103 BASE_EXPORT std::wstring towstring(
const string &s);
104 BASE_EXPORT std::wstring towstring(
const std::u16string &s);
105 BASE_EXPORT std::u16string tou16string(
const string &s);
106 BASE_EXPORT std::u16string tou16string(
const std::wstring &s);
107 BASE_EXPORT std::u16string tou16string(
const std::string_view &s);
108 BASE_EXPORT std::u32string tou32string(
const std::string &s);
109 BASE_EXPORT std::u32string tou32string(
const std::string_view &s);
112 BASE_EXPORT string capitalizeFirstLetter(
const string &s);
113 template <
typename T,
typename U> T join(
const std::vector<T> &s,
const U &sep);
114 BASE_EXPORT std::vector<string> split(
const string &s,
const string &sep,
bool keepEmptyParts=
false);
115 BASE_EXPORT std::vector<string> splitRegex(
const string &s,
const string ®ex,
bool keepEmptyParts=
false);
116 BASE_EXPORT string matchRegex(
const string &s,
const string ®ex,string::size_type start=0);
117 BASE_EXPORT string replaceRegex(
const string &s,
const string ®ex,
const string &after);
118 BASE_EXPORT string::size_type findRegex(
const string &s,
const string ®ex,string::size_type start=0);
119 BASE_EXPORT bool exactMatchRegex(
const string &s,
const string ®ex);
122 BASE_EXPORT string replaceAll(
const string &s,
const string &sub,
const string &newsub);
123 BASE_EXPORT string toBase64(
const std::vector<char> &in);
124 BASE_EXPORT std::vector<char> fromBase64(
const string &in);
125 BASE_EXPORT bool startsWith(
const string &in,
const string &check,
bool caseSensitive =
false);
126 BASE_EXPORT bool endsWith(
const string &in,
const string &check,
bool caseSensitive =
false);
127 BASE_EXPORT string clipLen(
const string &in,string::size_type length);
128 BASE_EXPORT string cleanupTypename(
const char *name);
129 BASE_EXPORT string convertPercentToBracket(
const string &s);
134 class Buffer :
public std::vector<char> {
136 using std::vector<char>::vector;
137 Buffer(
const std::vector<char> &v) : std::vector<char>(v) {}
138 Buffer(std::vector<char> &&v) : std::vector<char>(std::move(v)) {}
139 Buffer(
const char *c,
size_t len) : std::vector<char>(c, c+len) {}
140 explicit Buffer(
const string &s) : std::vector<char>(s.begin(),s.end()) {}
142 const Buffer &operator=(
const std::vector<char> &v) { std::vector<char>::operator=(v);
return *
this; }
143 const Buffer &operator=(std::vector<char> &&v) { std::vector<char>::operator=(std::move(v));
return *
this; }
145 void append(
const char *data, uint64 len) { insert(end(),data, data+len); }
146 void operator+=(
const string &s) { insert(end(),s.begin(),s.end()); }
147 string toString()
const {
return string(begin(), end()); }
148 const char *constData()
const {
return data(); }
152 BASE_EXPORT int32 caseInsensitiveCompare(
const string &s1,
const string &s2);
153 BASE_EXPORT int32 caseInsensitiveCompare(
const string &s1,
const std::string_view &s2);
154 inline int32 caseInsensitiveCompare(
const string &s1,
const char *str) {
return caseInsensitiveCompare(s1,std::string_view(str)); }
155 BASE_EXPORT uint64 caseInsensitiveHash(
const string &s);
156 BASE_EXPORT uint64 caseInsensitiveFind(
const string &s1,
const string &s2, string::size_type start = 0);
157 BASE_EXPORT string caseInsensitiveReplace(
const string &start,
const string &before,
const string &after);
158 BASE_EXPORT bool caseInsensitiveContains(
const std::vector<string> &all,
const string &s2);
159 BASE_EXPORT uint64 caseInsensitiveFind(
const std::vector<string> &all,
const string &s2);
160 BASE_EXPORT bool checkLeft(
const string &s,
const string &c);
161 inline bool equal(
const string &s,
const string &c) {
return caseInsensitiveCompare(s,c)==0; }
162 inline bool contains(
const string &s,
const string &s2) {
return caseInsensitiveFind(s,s2)!=string::npos; }
180 BASE_EXPORT string buildFormat(int64 width,
char notation=
'\0',
int precision=-1,
char fill=
' ');
188 inline string ts(
const T &t,
int width=0,
char notation =
'\0',
int precision = -1,
char fill =
' ');
194 template <
typename T>
195 std::tuple<T, bool> fsTest(
const string &) { static_assert(
sizeof(T{})==0);
return {T{},
false}; }
196 template<>
inline std::tuple<int32, bool> fsTest(
const string &in) {
return isInt32(in); }
197 template<>
inline std::tuple<uint32, bool> fsTest(
const string &in) {
return isUInt32(in); }
198 template<>
inline std::tuple<int64, bool> fsTest(
const string &in) {
return isInt64(in); }
199 template<>
inline std::tuple<uint64, bool> fsTest(
const string &in) {
return isUInt64(in); }
200 template<>
inline std::tuple<double, bool> fsTest(
const string &in) {
return isDouble(in); }
201 template<>
inline std::tuple<bool, bool> fsTest(
const string &in) {
return isBool(in); }
202 template<>
inline std::tuple<string, bool> fsTest(
const string &in) {
return {in,
true}; }
204 template <
typename T> T fs(
const string &, [[maybe_unused]]
bool throwException =
false) { static_assert(
sizeof(T{})==0);
return T{}; }
205 template<>
inline int32 fs(
const string &in,
bool throwException) {
return toInt32(in,throwException); }
206 template<>
inline uint32 fs(
const string &in,
bool throwException) {
return toUInt32(in,throwException); }
207 template<>
inline int64 fs(
const string &in,
bool throwException) {
return toInt64(in,throwException); }
208 template<>
inline uint64 fs(
const string &in,
bool throwException) {
return toUInt64(in,throwException); }
209 template<>
inline double fs(
const string &in,
bool throwException) {
return toDouble(in,throwException); }
210 template<>
inline bool fs(
const string &in,
bool throwException) {
return toBool(in,
"on,off,true,false,yes,no",throwException); }
211 template<>
inline string fs(
const string &in,
bool) {
return {in,
true}; }
217 bool operator()(
const string &left,
const string &right)
const {
218 return caseInsensitiveCompare(left,right) < 0;
223 bool operator()(
const string &left,
const string &right)
const {
224 return caseInsensitiveCompare(left,right) == 0;
229 uint64 operator()(
const string &in)
const {
230 return caseInsensitiveHash(in);
241 #include <unordered_map>
243 #include <unordered_set>
245 template <
typename T>
using StringMap = std::map<string, T, StringCILess>;
247 template <
typename T>
using StringMultiMap = std::multimap<string, T, StringCILess>;
249 template <
typename T>
using StringHashMap = std::unordered_map<string, T, StringCIHash, StringCIEqual>;
251 using StringSet = std::set<string, StringCILess>;
253 using StringHashSet = std::unordered_set<string, StringCIHash, StringCIEqual>;
256 inline string base::ts(
const T &t,
int width,
char notation,
int precision,
char fill) {
257 string s = fmt::format(buildFormat(width, notation, precision, fill), t);
259 return clipLen(s,std::abs(width));
267 inline string base::ts(
const double &dIn,
int width,
char notation,
int precision,
char fill) {
271 return fmt::format(buildFormat(width, notation, precision, fill), d);
274 template <
typename T,
typename U>
275 T join(
const std::vector<T> &s,
const U &sep) {
277 for (StringList::size_type i=0;i<s.size();++i) {
Base type definitions for the engine.
Definition: basestring.h:134
Definition: basestring.h:77
BASE_EXPORT std::tuple< uint32, bool > isUInt32(const string &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: basestring.cpp:47
bool isBool(const QString &in, bool *b, const QString &out)
Definition: baseqstring.cpp:231
bool toBool(const QString &in, bool *ok, const QString &out)
Definition: baseqstring.cpp:280
BASE_EXPORT std::tuple< int64, bool > isInt64(const string &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: basestring.cpp:56
#define BASE_EXPORT
Definition: basedef.h:24
BASE_EXPORT bool match(const string &keyword, const string &token)
**
Definition: basestring.cpp:460
BASE_EXPORT std::tuple< uint64, bool > isUInt64(const string &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: basestring.cpp:65
BASE_EXPORT std::tuple< double, bool > isDouble(const string &in)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: basestring.cpp:74
Definition: basestring.h:221
Definition: basestring.h:227
Definition: basestring.h:215
A overflow checked shorthand for static_cast<T>().