Itasca C++ Interface
Loading...
Searching...
No Matches
baseqstring.h
1#pragma once
9#include "basetoqt.h"
10#include "basestring.h"
11
12//#include <xfunctional>
13
24BASE_EXPORT QString toQString(const QString &s,int width=0,char fill=' ');
25BASE_EXPORT QString toQString(const char *str,int width=0,char fill=' ');
26BASE_EXPORT QString toQString(const qint8 &v,int width=0,char fill=' ');
27BASE_EXPORT QString toQString(const quint8 &v,int width=0,char fill=' ');
28BASE_EXPORT QString toQString(const qint16 &v,int width=0,char fill=' ');
29BASE_EXPORT QString toQString(const quint16 &v,int width=0,char fill=' ');
30BASE_EXPORT QString toQString(const qint32 &v,int width=0,char fill=' ');
31BASE_EXPORT QString toQString(const quint32 &v,int width=0,char fill=' ');
32BASE_EXPORT QString toQString(const qint64 &v,int width=0,char fill=' ');
33BASE_EXPORT QString toQString(const quint64 &v,int width=0,char fill=' ');
34BASE_EXPORT QString toQString(const wstring &v,int width,char fill=' ');
35BASE_EXPORT QString toQString(const string &v, int width, char fill = ' ');
36BASE_EXPORT QString toQString(const float & v,int width=0,char format='g',int precision=-1,char fill=' ');
37BASE_EXPORT QString toQString(const double &v,int width=0,char format='g',int precision=-1,char fill=' ');
38BASE_EXPORT QString toQString(const SymTensor &v,int width=0,char format='g',int precision=-1,char fill=' ');
39BASE_EXPORT QString toQString(const QVariant &v,int width=0,char fill=' ',char format='g',int precision=-1);
41BASE_EXPORT QString toQString(const void *v,int width=0,char fill=' ');
44BASE_EXPORT QString toQString(bool b,const QString &out="on,off",int width=0,char fill=' ');
45
46
47// Template version of toQString, that uses the same argument list
48template <class T>
49QString toQStringT(const T &t,int width=0,char fill=' ',[[maybe_unused]] char format='g',
50 [[maybe_unused]] int precision=-1) {
51 return toQString(t,width,fill);
52}
53template <>
54inline QString toQStringT(const float &t,int width,char fill,char format,int precision) {
55 return toQString(t,width,format,precision,fill);
56}
57template <>
58inline QString toQStringT(const double &t,int width,char fill,char format,int precision) {
59 return toQString(t,width,format,precision,fill);
60}
61template <>
62inline QString toQStringT(const QVariant &t,int width,char fill,char format,int precision) {
63 return toQString(t,width,fill,format,precision);
64}
65template <>
66inline QString toQStringT(const bool &b,int width,char fill,[[maybe_unused]] char format,
67 [[maybe_unused]] int precision) {
68 return toQString(b,"on,off",width,fill);
69}
70
74template <class T> inline QString toQString(const Vector2<T> &v,int width=0,char fmt='g',int prec=-1,char fill=' ') {
75 QString s("(%1,%2)");
76 int w = (std::abs(width) - 3) / 2;
77 if (width<0) w = -1;
78 s = s.arg(toQStringT(v.x(),w,fill,fmt,prec));
79 s = s.arg(toQStringT(v.y(),w,fill,fmt,prec));
80 return s;
81}
85template <class T> inline QString toQString(const Vector3<T> &v,int width=0,char fmt='g',int prec=-1,char fill=' ') {
86 QString s("(%1,%2,%3)");
87 int w = (std::abs(width) - 4) / 3;
88 if (width<0) w = -1;
89 s = s.arg(toQStringT(v.x(),w,fill,fmt,prec));
90 s = s.arg(toQStringT(v.y(),w,fill,fmt,prec));
91 s = s.arg(toQStringT(v.z(),w,fill,fmt,prec));
92 return s;
93}
94
98template <class T> inline QString toQString(const AVector2<T> &v,int width=0,char fmt='g',int prec=-1,char fill=' ') {
99 QString s("(%1)");
100 int w = (std::abs(width) - 3) / 2;
101 if (width<0) w = -1;
102 s = s.arg(toQStringT(v.z(),w,fill,fmt,prec));
103 return s;
104}
108template <class T> inline QString toQString(const AVector3<T> &v,int width=0,char fmt='g',int prec=-1,char fill=' ') {
109 QString s("(%1,%2,%3)");
110 int w = (std::abs(width) - 4) / 3;
111 if (width<0) w = -1;
112 s = s.arg(toQStringT(v.x(),w,fill,fmt,prec));
113 s = s.arg(toQStringT(v.y(),w,fill,fmt,prec));
114 s = s.arg(toQStringT(v.z(),w,fill,fmt,prec));
115 return s;
116}
117
118
123BASE_EXPORT QString toBytes(const quint64 &ul);
124inline string tostring(const QString &s) { return s.toStdString(); }
125inline StringList toStringList(const QStringList &sl);
126inline QStringList toQStringList(const StringList &sl);
127inline StringList fromQStringList(const QStringList &sl);
128inline Buffer toBuffer(const QByteArray &ba);
129
131BASE_EXPORT QString readLine(QDataStream &ds,qint64 maxlen=0);
133BASE_EXPORT QString readWord(QDataStream &ds,qint64 maxlen=0,const QString &separators=" \t\r\n");
134// Converts the string This is a "test" to "This ia a \"test\""
135BASE_EXPORT QString enquoteString(const QString &s,QChar quote='"');
136
137
142BASE_EXPORT bool isInt(const QString &in,int *i=0);
143BASE_EXPORT bool isUInt(const QString &in,uint *u=0);
144BASE_EXPORT bool isLong(const QString &in,qint64 *l=0);
145BASE_EXPORT bool isULong(const QString &in,quint64 *ul=0);
146BASE_EXPORT bool isDouble(const QString &in,double *d=0);
153BASE_EXPORT bool isBool(const QString &in,bool *b=0,const QString &out="on,off,true,false,yes,no");
157BASE_EXPORT bool toBool(const QString &in,bool *ok=0,const QString &out="on,off,true,false,yes,no");
159//inline string toString(const wsString &in) { return in.toStdWString(); }
161//inline String toString(const std::wstring &in) { return in; }
163//inline String toString(const string &in) { return QString::fromUtf8(in.c_str()).toStdWString(); }
165inline QString toQString(const std::wstring &in) { return QString::fromStdWString(in); }
167inline QString toQString(const string &in) { return QString::fromStdString(in); }
168// compares two Strings
169//inline int compare(const String &s1,const String &s2,bool case_sensitive=false) {
170// if (case_sensitive) return s1.compare(s2);
171//#ifdef __LINUX
172// else return wcscasecmp(s1.c_str(),s2.c_str());
173//#else
174// else return _wcsicmp(s1.c_str(),s2.c_str());
175//#endif
176//}
177
178// std::hash interface for QStrings!
179//namespace std {
180// template <>
181// class hash<QString> {
182// public:
183// size_t operator()(const QString &s) const { return qHash(s); }
184// };
185//}
186
187// < and == functors for strings - to use in std containers when CI comparisons are wanted!
189public:
190 bool operator()(const QString &left, const QString &right) const {
191 return left.compare(right,Qt::CaseInsensitive) < 0;
192 }
193};
195public:
196 bool operator()(const QString &left, const QString &right) const {
197 return QString::localeAwareCompare(left,right) < 0;
198 }
199};
201public:
202 bool operator()(const QString &left, const QString &right) const {
203 return left.compare(right,Qt::CaseInsensitive) == 0;
204 }
205};
207public:
208 uint operator()(const QString &in) const {
209 uint h = 0;
210 int len = in.size();
211 auto *p = in.constData();
212 for (int i = 0; i<len; ++i) {
213 h = 31 * h + p[i].toUpper().unicode();
214 }
215 return h;
216 }
217};
218
219
220template <typename T> using QStringMap = std::map<QString, T, QStringCILess2>;
221
222template <typename T> using QStringHashMap = std::unordered_map<QString, T, QStringCIHash2, QStringCIEqual2>;
223
224using QStringSet = std::set<QString,QStringCILess2>;
225
226using QStringHashSet = std::unordered_set<QString, QStringCIHash2, QStringCIEqual2>;
227
228inline StringList toStringList(const QStringList &qsl) {
229 StringList sl;
230 for (auto &s : qsl)
231 sl.push_back(s.toStdString());
232 return sl;
233}
234
235inline QStringList toQStringList(const StringList &sl) {
236 QStringList qsl;
237 for (auto &s : sl)
238 qsl.push_back(QString::fromStdString(s));
239 return qsl;
240}
241
242inline StringList fromQStringList(const QStringList &qsl) {
243 StringList sl;
244 for (auto &qs : qsl)
245 sl.push_back(qs.toStdString());
246 return sl;
247}
248
249inline Buffer toBuffer(const QByteArray &ba) {
250 Buffer buf; buf.reserve(ba.size());
251 buf.insert(buf.end(),ba.begin(), ba.end());
252 return buf;
253}
254
255// This allows you to send QStrings to a fmt::format
256template <>
257struct fmt::formatter<QString> : public fmt::formatter<string> {
258 template <typename ParseContext>
259 constexpr auto parse(ParseContext &ctx) { return fmt::formatter<string>::parse(ctx); }
260
261 template <typename FormatContext>
262 auto format(QString const &val, FormatContext &ctx) {
263 return fmt::formatter<string>::format(val.toStdString(), ctx);
264 }
265};
267// EoF
QString helper functions, plus some additions.
Combines base interface with Qt – allows Qt to interact with other Base types transparently.
2D Angular vector class.
Definition avect.h:43
const T & z() const
Member access - returns the z component of the 2D angular vector.
Definition avect.h:70
3D Angular vector class.
Definition avect.h:176
Definition basestring.h:161
Definition basestring.h:89
A symmetric 2nd order tensor.
Definition symtensor.h:22
2D vector utility class.
Definition vect.h:34
constexpr const T & x() const
X component access.
Definition vect.h:58
constexpr const T & y() const
Y component access.
Definition vect.h:60
3D vector utility class.
Definition vect.h:163
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:186
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:184
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:188
BASE_EXPORT QString toQString(const QString &s, int width=0, char fill=' ')
Converts from a base type to a QString, using the width and fill character specified.
Definition baseqstring.cpp:5
BASE_EXPORT QString readWord(QDataStream &ds, qint64 maxlen=0, const QString &separators=" \t\r\n")
Reads a single whitespace delimited word from the QDataStream (since QTextStream is bugged),...
Definition baseqstring.cpp:338
BASE_EXPORT bool isLong(const QString &in, qint64 *l=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:257
BASE_EXPORT bool isUInt(const QString &in, uint *u=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:250
BASE_EXPORT bool isDouble(const QString &in, double *d=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:271
BASE_EXPORT bool isInt(const QString &in, int *i=0)
Definition baseqstring.cpp:243
BASE_EXPORT bool isBool(const QString &in, bool *b=0, const QString &out="on,off,true,false,yes,no")
Definition baseqstring.cpp:229
BASE_EXPORT bool toBool(const QString &in, bool *ok=0, const QString &out="on,off,true,false,yes,no")
Definition baseqstring.cpp:278
#define BASE_EXPORT
Definition basedef.h:24
BASE_EXPORT QString readLine(QDataStream &ds, qint64 maxlen=0)
Reads a single QString line from a QDataStream (since QTextStream is bugged), uses '\n' as an end-of-...
Definition baseqstring.cpp:326
BASE_EXPORT bool isULong(const QString &in, quint64 *ul=0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:264
BASE_EXPORT QString toBytes(const quint64 &ul)
Converts number ul into a memory size string.
Definition baseqstring.h:200
Definition baseqstring.h:206
Definition baseqstring.h:188
Definition baseqstring.h:194