Itasca C++ Interface
Loading...
Searching...
No Matches
istring.h
1#pragma once
2#define ISTRING_H
3
4#include "basestring.h"
5#include "to.h"
6
7// A string class that attempts to add QString compatibility
8class QByteArray;
9class QString;
10class QVariant;
11class IStringList;
12class QDebug;
13class QChar;
14class IString : public string {
15
16 public:
17 using string::string;
18 enum class Empty { Keep, Skip };
19 enum class Case { Insensitive, Sensitive };
20
21 IString(const IString &is) : string(is) { }
22 IString(IString &&is) : string(std::move(is)) { }
23 IString(const string &s) : string(s) { }
24 IString(string &&s) : string(std::move(s)) { }
25 IString(const char *in) : string(in) { }
26 explicit IString(int i,char c) : string(i,c) { }
27 IString(const Buffer &in) : string(in.begin(),in.end()) { }
28 explicit IString(char c) : string(1,c) { }
29
30 IString &operator=(const IString &s) { string::operator=(s); return *this; }
31 IString &operator=(IString &&s) { string::operator=(std::move(s)); return *this; }
32 IString &operator=(const char *str) { string::operator=(str); return *this; }
33 IString &operator=(const string &s) { string::operator=(s); return *this; }
34 IString &operator=(string &&s) { string::operator=(std::move(s)); return *this; }
35 IString &operator=(char c) { string::operator=({c}); return *this; }
36
37 // Qt conversion (remove from this class someday)
38 BASE_EXPORT IString(const QByteArray &in);
39 BASE_EXPORT IString(const QString &in);
40 BASE_EXPORT const IString &operator=(const QString &);
41 BASE_EXPORT operator QString() const;
42 BASE_EXPORT operator QVariant() const;
43
44 IString operator+(const IString &s) const { IString out(*this); out += s; return out; }
45 IString operator+(const string &s) const { IString out(*this); out += s; return out; }
46 IString operator+(const char *s) const { IString out(*this); out += s; return out; }
47 BASE_EXPORT IString operator+(const QString &s) const;
48 IString operator+=(const IString &s) { string::operator+=(s); return *this; }
49 IString operator+=(const char *str) { string::operator+=(str); return *this; }
50 IString operator+=(const string &s) { string::operator+=(s); return *this; }
51 IString operator+=(char c) { string::operator+=(c); return *this; }
52 BASE_EXPORT IString operator+=(const QString &s);
53
54 int size() const { return to<int>(string::size()); }
55 int length() const { return size(); }
56 IString mid(int start,int end) const { return substr(to<size_type>(start),to<size_type>(end)); }
57 IString mid(int start) const { size_type s = to<size_type>(start); return s<string::size() ? substr(to<size_type>(start)) : string{}; }
58 IString left(int pos) const { return substr(0,to<size_type>(pos)); }
59 IString right(int pos) const { if ((pos<0) || (pos>=to<int>(size()))) return *this; return substr(size()-to<size_type>(pos),size()); }
60 int indexOf(char c,int start=0) const { auto pos = find(c,start); if (pos==string::npos) return -1; return to<int>(pos); }
61 int indexOf(const IString &s,int start=0) { auto pos = find(s,start); if (pos==string::npos) return -1; return to<int>(pos); }
62 int lastIndexOf(char c) const { auto pos = rfind(c); if (pos==string::npos) return -1; return to<int>(pos); }
63 int lastIndexOf(const IString &s) const { auto pos = rfind(s); if (pos==string::npos) return -1; return to<int>(pos); }
64 BASE_EXPORT IStringList split(const string &sep,Empty empty=Empty::Keep) const;
65 BASE_EXPORT IStringList split(char sep,Empty empty=Empty::Keep) const;
66 IString simplified() const { return ::simplified(*this); }
67 string toStdString() const { return *this; }
68 std::wstring toStdWString() const { return towstring(*this); }
69 bool isEmpty() const { return size()==0; }
70 int compare(const IString &s,Case c=Case::Sensitive) const { return compare(*this,s,c); }
71 BASE_EXPORT int localeAwareCompare(const IString &s) const;
72 IString toLower() const { return ::toLower(*this); }
73 IString toUpper() const { return ::toUpper(*this); }
74 int32 toInt(bool *ok=nullptr) const { auto [i,ok2] = ::isInt32(*this); if (ok) *ok = ok2; return i; }
75 uint32 toUInt(bool *ok=nullptr) const { auto [u,ok2] = ::isUInt32(*this); if (ok) *ok = ok2; return u; }
76 uint64 toULongLong(bool *ok=nullptr) const { auto [u,ok2] = ::isUInt64(*this); if (ok) *ok = ok2; return u; }
77 int64 toLongLong(bool *ok=nullptr) const { auto [i,ok2] = ::isInt64(*this); if (ok) *ok = ok2; return i; }
78 double toDouble(bool *ok=nullptr) const { auto [d,ok2] = ::isDouble(*this); if (ok) *ok = ok2; return d; }
79 BASE_EXPORT QByteArray toUtf8() const;
80 BASE_EXPORT QByteArray toLatin1() const;
81 IString toString() const { return *this; }
82 BASE_EXPORT bool contains(const IString &s,Case c=Case::Sensitive) const;
83 BASE_EXPORT bool startsWith(const IString &s,Case c=Case::Sensitive) const;
84 BASE_EXPORT bool endsWith(const IString &s,Case c=Case::Sensitive) const;
85 BASE_EXPORT int count(const IString &c) const;
86 BASE_EXPORT IString leftJustified(int width,char fill=' ',bool truncate=false) const;
87 BASE_EXPORT IString rightJustified(int width,char fill=' ',bool truncate=false) const;
88 IString trimmed() const { return ::trimmed(*this); }
89
90 BASE_EXPORT IString arg(const IString &s,int width=0,char fill=' ') const;
91 IString arg(float f,int width=0,char format='g',int precision=0,char fill=' ') const { return arg(base::ts(f,width,format,precision,fill)); }
92 IString arg(double d,int width=0,char format='g',int precision=0,char fill=' ') const;
93 IString arg(uint32 i,int width=0,[[maybe_unused]] int base=10,char fill=' ') const { return arg(base::ts(i,width,'\0',-1,fill)); }
94 IString arg(uint64 i,int width=0,[[maybe_unused]] int base=10,char fill=' ') const { return arg(base::ts(i,width,'\0',-1,fill)); }
95 IString arg(int32 i,int width=0,[[maybe_unused]] int base=10,char fill=' ') const { return arg(base::ts(i,width,'\0',-1,fill)); }
96 IString arg(int64 i,int width=0,[[maybe_unused]] int base=10,char fill=' ') const { return arg(base::ts(i,width,'\0',-1,fill)); }
97 BASE_EXPORT IString arg(const IString &arg,const IString &arg2,const IString &arg3={},const IString &arg4={}) const;
98
99 BASE_EXPORT void chop(int pos);
100 BASE_EXPORT void truncate(int pos);
101 BASE_EXPORT IString &replace(const IString &before,const IString &after,Case c=Case::Sensitive);
102 BASE_EXPORT IString &replace(int pos,int n,char after);
103 BASE_EXPORT IString &replace(int pos,int n,const IString &after);
104 BASE_EXPORT IString &remove(const IString &val);
105 BASE_EXPORT IString &remove(int pos,int len);
106 BASE_EXPORT IString &remove(char c);
107 BASE_EXPORT IString &remove(QChar c);
108 BASE_EXPORT void prepend(char c);
109 BASE_EXPORT IString &insert(int position,char c);
110 IString &insert(int position,const IString &s) { string::insert(position,s); return *this; }
111
112 inline static IString fromStdString(const string &s) { return IString(s); }
113 inline static IString fromStdU16String(const std::u16string &s) { return tostring(s); }
114 inline static IString fromStdWString(const std::wstring &s) { return tostring(s); }
115 inline static IString fromUtf8(const char *str) { return str; }
116 inline static IString fromUtf8(const char *str,size_t size) { return string(str,size); }
117 inline static IString fromLatin1(const char *str) { return str; }
118 BASE_EXPORT static int compare(const IString &first,const IString &second,Case c=Case::Sensitive);
119 BASE_EXPORT static IString number(double d,char format='g',int precision=6);
120};
121inline IString operator+(const char *str,const IString &is) { return IString(str) + is; }
122template <typename ... Args> IString build(const IString &in,Args &&...args);
123template <typename T> IString toIString(const T &t,int width=0,char format='\0',int precision=-1,char fill=' ') { return base::ts(t,width,format,precision,fill); }
124BASE_EXPORT IString toIString(const QString &v,int width=0,char fill=' ');
125inline IString toIString(const IString &v,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
126inline IString toIString(const char *v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
127inline IString toIString(const int8 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
128inline IString toIString(const uint8 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
129inline IString toIString(const int16 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
130inline IString toIString(const uint16 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
131inline IString toIString(const int32 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
132inline IString toIString(const uint32 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
133inline IString toIString(const int64 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
134inline IString toIString(const uint64 &v ,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
135inline IString toIString(const string &v ,int width ,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
136inline IString toIString(const void *v,int width=0,char fill=' ') { return base::ts(v,width,'\0',-1,fill); }
137BASE_EXPORT IString toIString(bool b,const IString &out="on,off",int width=0,char fill=' ');
138inline IString toIString(const string &s) { return IString(s); }
139
140inline string tostring(const IString &s) { return s; }
141//inline IString toIString(bool b,const IString &values="true,false") { auto sl = split(values,","); return b ? sl[0] : sl[1]; }
142//inline IString toIString(bool b,const char *values) { return toIString(b,IString(values)); }
143
144inline bool isInt(const IString &s,int32 *i=nullptr) { auto [i2,ok] = ::isInt32(string_view(s)); if (i && ok) *i = i2; return ok; }
145inline bool isDouble(const IString &s,double *d=nullptr) { auto [d2,ok] = ::isDouble(string_view(s)); if (d && ok) *d = d2; return ok; }
146inline bool isUInt(const IString &s,uint32 *u=nullptr) { auto [u2,ok] = ::isUInt32(string_view(s)); if (u && ok) *u = u2; return ok; }
147inline bool isULong(const IString &s,uint64 *u=nullptr) { auto [u2,ok] = ::isUInt64(string_view(s)); if (u && ok) *u = u2; return ok; }
148inline bool isLong(const IString &s,int64 *i=nullptr) { auto [i2,ok] = ::isInt64(string_view(s)); if (i && ok) *i = i2; return ok; }
149
150BASE_EXPORT QDebug operator<<(QDebug dbg,const IString &s);
151
157BASE_EXPORT IString clip(const IString &in,int length);
158BASE_EXPORT IString enquoteString(const IString &s,char quote='"'); // Converts the string This is a "test" to "This ia a \"test\""
169BASE_EXPORT bool match(const IString &keyword,const IString &token);
170
171//------------------------------------------------------
172// Implementations
173//------------------------------------------------------
174template <typename ... Args>
175IString build(const IString &in,Args &&...args) {
176 return fmt::format(convertPercentToBracket(in),args...);
177}
178
179template <typename T> using IStringMap = std::map<IString, T, StringCILess>;
180template <typename T> using IStringHashMap = std::unordered_map<IString, T, StringCIHash, StringCIEqual>;
181using IStringSet = std::set<IString,StringCILess>;
182
183template <>
184struct std::hash<IString> {
185 size_t operator()(const IString &in) const { std::hash<string> h; return h(in); }
186};
187
188// EoF
QString helper functions, plus some additions.
Definition basestring.h:159
Definition istring.h:14
Definition istringlist.h:10
bool isLong(const QString &in, qint64 *l)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:257
bool isUInt(const QString &in, uint *u)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:250
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
bool isInt(const QString &in, int *i)
Definition baseqstring.cpp:243
std::tuple< uint64, bool > isUInt64(const string_view &s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:52
std::tuple< uint32, bool > isUInt32(const string_view &s)
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
bool match(const string &keyword, const string &token)
‍**
Definition basestring.cpp:462
POPWARNING QDataStream & operator<<(QDataStream &ds, const Orientation2 &o)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basetoqt.cpp:107
bool isULong(const QString &in, quint64 *ul)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition baseqstring.cpp:264
std::tuple< int64, bool > isInt64(const string_view &s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basestring.cpp:43
A overflow checked shorthand for static_cast<T>().