Itasca C++ Interface
Loading...
Searching...
No Matches
basestring.h
Go to the documentation of this file.
1#pragma once
7// MOO NOTE: a LOT LOT Of these functions should probably be changed to take a
8// string_view argument now that string will automatically convert....
9// also standardize on whether we use
10// caseInsensitiveFUNC() or
11// FUNC(...,bool caseSensitive=false) as a standard.
12// I'm leaning to the latter for conciseness
13#include "basedef.h"
14#include "to.h"
15#include <cctype>
16#include <cwctype>
17#include <format>
18#include <string>
19#include <vector>
20#include <string_view>
21#ifdef _WIN32
22#include <codeanalysis\warnings.h>
23#endif
24
25// Global namespace versions of std strings
26using wstring = std::wstring; // Old string class to avoid interface compatibility breaks
27using string = std::string; // Bring into global namespace like the other base types (uint32, etc)
28using string_view = std::string_view;
29//using String = std::wstring; // DEPRECATED - should not be used in new code.
30
31// String conversion routines.
32// These return a tuple, the first is the converted value and the second is whether the conversion was successful.
33// If not successful the first value is a default construction.
34BASE_EXPORT std::tuple<int32,bool> isInt32(const string_view &in);
35BASE_EXPORT std::tuple<uint32,bool> isUInt32(const string_view &in);
36BASE_EXPORT std::tuple<int64,bool> isInt64(const string_view &in);
37BASE_EXPORT std::tuple<uint64,bool> isUInt64(const string_view &in);
38BASE_EXPORT std::tuple<double,bool> isDouble(const string_view &in);
39BASE_EXPORT std::tuple<bool,bool> isBool(const string_view &in,const string_view &out="on,off,true,false,yes,no");
40
41// explicit string versions to avoid ambiguity
42inline std::tuple<int32,bool> isInt32(const string &in) { return isInt32(string_view(in)); }
43inline std::tuple<uint32,bool> isUInt32(const string &in) { return isUInt32(string_view(in)); }
44inline std::tuple<int64,bool> isInt64(const string &in) { return isInt64(string_view(in)); }
45inline std::tuple<uint64,bool> isUInt64(const string &in) { return isUInt64(string_view(in)); }
46inline std::tuple<double,bool> isDouble(const string &in) { return isDouble(string_view(in)); }
47inline std::tuple<bool,bool> isBool(const string &in,const string_view &out="on,off,true,false,yes,no") { return isBool(string_view(in),out); }
48inline std::tuple<int32,bool> isInt32v(const string_view &in) { return isInt32(string_view(in)); }
49inline std::tuple<uint32,bool> isUInt32v(const string_view &in) { return isUInt32(string_view(in)); }
50inline std::tuple<int64,bool> isInt64v(const string_view &in) { return isInt64(string_view(in)); }
51inline std::tuple<uint64,bool> isUInt64v(const string_view &in) { return isUInt64(string_view(in)); }
52inline std::tuple<double,bool> isDoublev(const string_view &in) { return isDouble(string_view(in)); }
53inline 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); }
54
55// String conversion routines - converts string directly, throwing out the success check.
56// No exception is thrown on failure - just returns a default constructed value.
57// This is a common template used just to avoid code repetition in the type-specific version
58template <typename T,typename ... Args>
59T toStringConv(const string_view &in,Args...args,bool te, std::tuple<T,bool> (*f)(const string_view &,Args...));
60inline int32 toInt32(const string_view &in, bool throwException = false) { return toStringConv<int32>(in, throwException, isInt32v); }
61inline uint32 toUInt32(const string_view &in,bool throwException=false) { return toStringConv<uint32>(in, throwException, isUInt32v); }
62inline int64 toInt64(const string_view &in,bool throwException=false) { return toStringConv<int64>(in, throwException, isInt64v); }
63inline uint64 toUInt64(const string_view &in,bool throwException=false) { return toStringConv<uint64>(in, throwException, isUInt64v); }
64inline double toDouble(const string_view &in,bool throwException=false) { return toStringConv<double>(in, throwException, isDoublev); }
65inline 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); }
66template <typename ... Args>
67string rformat(const string &s,Args...args) { return std::vformat(s,std::make_format_args(args...)); }
68
69
70// vector of strings - for convenience and to add some handy utility functions.
71class StringList : public std::vector<string> {
72 public:
73 using std::vector<string>::vector;
74 enum class Empty { Keep, Skip };
75
76 StringList() {}
77 StringList(std::initializer_list<string> list) : std::vector<string>(list) {}
78 StringList(const StringList &s) : std::vector<string>(s) {}
79 StringList(const std::vector<string> &v) : std::vector<string>(v) {}
80 StringList(StringList &&v) noexcept : std::vector<string>(std::move(v)) {}
81 StringList(std::vector<string> &&v) noexcept : std::vector<string>(std::move(v)) {}
82
83 const StringList &operator=(const StringList &in) { std::vector<string>::operator=(in); return *this; }
84
85 const StringList &operator+=(const string &s) { push_back(s); return *this; }
86 const StringList &operator+=(const std::vector<string> &v) { for (auto &s : v) push_back(s); return *this; }
87 const StringList &operator<<(const string &s) { return operator+=(s); }
88 const StringList &operator<<(const StringList &s) { return operator+=(s); }
89 StringList operator+(const StringList &s) const { StringList ret(*this); ret += s; return ret; }
90
91};
92BASE_EXPORT std::vector<string> toStringList(const std::vector<string_view> &v);
93BASE_EXPORT bool contains(const std::vector<string> &all, const string &s2,bool caseSensitivity=false);
94
95// String utility functions
96[[nodiscard]] BASE_EXPORT string tostring(const std::wstring &s); // Converts wstring to string
97[[nodiscard]] BASE_EXPORT string tostring(const std::u16string &s);
98[[nodiscard]] BASE_EXPORT string tostring(const std::u32string &s);
99[[nodiscard]] inline string tostring(const string_view &s) { return string(s.data(),s.size()); }
100[[nodiscard]] inline string tostring(const char *s) { return string(s); }
101[[nodiscard]] inline string tostring(const string & s) { return s; } // For template support
102[[nodiscard]] BASE_EXPORT std::wstring towstring(const string &s); // Converts string ot wstring
103[[nodiscard]] BASE_EXPORT std::wstring towstring(const std::u16string &s);
104[[nodiscard]] BASE_EXPORT std::u16string tou16string(const string &s); // Converts string to u16string
105[[nodiscard]] BASE_EXPORT std::u16string tou16string(const std::wstring &s); // Converts wstring to u16string
106[[nodiscard]] BASE_EXPORT std::u16string tou16string(const string_view &s); // Converts string to u32string
107[[nodiscard]] BASE_EXPORT std::u32string tou32string(const string &s); // Converts string to u32string
108[[nodiscard]] BASE_EXPORT std::u32string tou32string(const string_view &s); // Converts string to u32string
109[[nodiscard]] BASE_EXPORT string toUpper(const string_view &s); // All upper case
110[[nodiscard]] BASE_EXPORT string toLower(const string_view &s); // All lower case
111[[nodiscard]] BASE_EXPORT string capitalizeFirstLetter(const string_view &s);
112template <typename T,typename U> [[nodiscard]] T join(const std::vector<T> &s,const U &sep); // Note works on StringList
113[[nodiscard]] BASE_EXPORT std::vector<string_view> splitView(const string_view &s, const string_view &sep,bool keepEmptyParts=false); // NOTE case insensitive
114[[nodiscard]] inline std::vector<string> split(const string_view &s, const string_view &sep,bool keepEmptyParts=false) { return toStringList(splitView(s,sep,keepEmptyParts)); }
115[[nodiscard]] BASE_EXPORT std::vector<string_view> splitViewRegex(const string_view &s,const string_view &regex,bool keepEmptyParts=false); // NOTE Case insensitive
116[[nodiscard]] inline std::vector<string> splitRegex(const string_view &s,const string_view &regex,bool keepEmptyParts=false) { return toStringList(splitViewRegex(s,regex,keepEmptyParts)); }
117[[nodiscard]] BASE_EXPORT string_view matchViewRegex(const string_view &s,const string_view &regex,string::size_type start=0); // NOTE Case insensitive
118[[nodiscard]] inline string matchRegex(const string_view &s,const string_view &regex,string::size_type start=0) { return tostring(matchViewRegex(s,regex,start)); }
119[[nodiscard]] BASE_EXPORT string replaceRegex(const string &s,const string_view &regex,const string &after); // NOTE Case insensitive
120[[nodiscard]] BASE_EXPORT string::size_type find(const string_view &s1,const string_view &s2,string::size_type start=0,bool caseSensitive=false);
121[[nodiscard]] BASE_EXPORT string::size_type findRegex(const string_view &s,const string_view &regex,string::size_type start=0); // NOTE Case insensitive
122[[nodiscard]] BASE_EXPORT bool exactMatchRegex(const string_view &s,const string_view &regex); // NOTE Case insensitive
123[[nodiscard]] BASE_EXPORT string_view trimmed_view(const string_view &s); // Removes whitespace on front and back.
124[[nodiscard]] inline string trimmed(const string_view &s) { return tostring(trimmed_view(s)); }
125[[nodiscard]] BASE_EXPORT string simplified(const string_view &s); // As trimmed, but also reduces whitespace sequences inside to one space.
126[[nodiscard]] BASE_EXPORT string replace(string s, const string_view &sub,const string_view &newsub,bool caseSensitive=false); // Replace all instances of sub wiwth newsub in s
127[[nodiscard]] BASE_EXPORT string toBase64(const std::vector<char> &in);
128[[nodiscard]] BASE_EXPORT std::vector<char> fromBase64(const string &in);
129[[nodiscard]] BASE_EXPORT bool startsWith(const string_view &in,const string_view &check,bool caseSensitive = false);
130[[nodiscard]] BASE_EXPORT bool endsWith(const string_view &in, const string_view &check,bool caseSensitive = false);
131[[nodiscard]] BASE_EXPORT bool contains(const string_view &in,const string_view &check,bool caseSensitive = false);
132[[nodiscard]] BASE_EXPORT string clipLen(string in,string::size_type length);
133[[nodiscard]] BASE_EXPORT string cleanupTypename(const char *name);
134[[nodiscard]] BASE_EXPORT string remove(string s,char c);
135[[nodiscard]] inline string remove(string s,const string_view &sub,bool caseSensitive=false) { return replace(s,sub,{},caseSensitive); }
136[[nodiscard]] BASE_EXPORT int32 compare(const string_view &s1, const string_view &s2,bool caseSensitive=false);
137[[nodiscard]] BASE_EXPORT uint64 caseInsensitiveHash(const string_view &s);
138[[nodiscard]] BASE_EXPORT uint64 firstNonBlankCharacter(const string_view &s);
139[[nodiscard]] BASE_EXPORT bool wildcardMatch(const string_view &patter,const string_view &test);
140[[nodiscard]] BASE_EXPORT string formatBytes(uint64 bytes,uint32 precision=3);
141[[nodiscard]] BASE_EXPORT string_view substr(const string_view &s,string::size_type pos,string::size_type count=string::npos); // Same as s.substr() except returns empty string if pos > size()
142
143// A buffer class - thin wrapper around std::vector<char>
144// Useful in places where we would other use QByteArray.
145class Buffer : public std::vector<char> {
146 public:
147 using std::vector<char>::vector;
148 Buffer(const std::vector<char> &v) : std::vector<char>(v) {}
149 Buffer(std::vector<char> &&v) : std::vector<char>(std::move(v)) {}
150 Buffer(const char *c, size_t len) : std::vector<char>(c, c+len) {}
151 explicit Buffer(const string &s) : std::vector<char>(s.begin(),s.end()) {}
152
153 const Buffer &operator=(const std::vector<char> &v) { std::vector<char>::operator=(v); return *this; }
154 const Buffer &operator=(std::vector<char> &&v) { std::vector<char>::operator=(std::move(v)); return *this; }
155
156 void append(const char *data, uint64 len) { insert(end(),data, data+len); }
157 void operator+=(const string &s) { insert(end(),s.begin(),s.end()); }
158 string toString() const { return string(begin(), end()); }
159 string_view toStringView() const { return string_view(begin(),end()); }
160 const char *constData() const { return data(); }
161};
162
163// As string::compare but the comparison is case insensitive
164//inline int32 caseInsensitiveCompare(const string &s1, const char *str) { return caseInsensitiveCompare(s1,string_view(str)); }
165BASE_EXPORT bool checkLeft(const string_view &s,const string_view &c); // Checks if c matches start of s (case insensitive)
166inline bool equal(const string_view &s,const string_view &c,bool caseSensitive=false) { return compare(s,c,caseSensitive)==0; } // Handy shorthand
167
169//*
170//* Specifically, compares to length of token or keyword, whichever is shorter.\n
171//* If token is longer than keyword no match.\n
172//* If keyword contains the character '^', that character is disregarded for comparison purposes
173//* and token must have at least as many characters as precede the '^' character.\n
174//* If a token has a starting hyphen '-', then it is ignored for matching.
175//* If a token has an internal hyphen 'one-two', then the hyphen must be present in keyword and
176//* BOTH sides are checked using matching rules and any possible '^' character.
177//*/
178BASE_EXPORT bool match(const string &keyword,const string &token,bool forceAbbreviationsAllowed=false);
179BASE_EXPORT uint32 match(const StringList &keywords,const string &token,bool forceAbbreviationsAllowed=false); // 0 if no match, base 1 if match (ugh)
180BASE_EXPORT void matchSynonymsAllowed(bool b);
181BASE_EXPORT bool matchSynonymsAllowed();
182// Getkeyword pull the FIRST full alias keyword from the keyword list at the given index, or def if it doesn't exist
183BASE_EXPORT string getKeyword(const StringList &keywords,const uint64 index,const string &def={});
184inline string getKeyword(const string &keywords,const uint64 index,const string &def={}) { return getKeyword(split(keywords,","),index,def); }
185
186// Create a std::format specification using the parameters indicated {:fw.pn}
187// Note position indicator is not included (yet).
188BASE_EXPORT string buildFormat(int64 width, char notation='\0', int precision=-1,char fill=' ');
189
190namespace base {
191 // Thesee are type->string conversion functions, for convenience. less verbose than std::format
192 // and intended for use in formatted << output, but can be used in any string stuff.
193 // ts stands for To String, fs stands for From String.
194 // If you use these in text output I reccomend you put "using base::ts" at the start of the FUNCTION.
195 template <class T>
196 inline string ts(const T &t, int width=0, char notation = '\0', int precision = -1, char fill = ' ');
197
198
199 // Concise conversion from string to type. fsTest returns a tuple with the second
200 // value being a success boolean. fs just returns the type with an optional
201 // second argument indicating if an exception should be thrown on failure.
202 template <typename T>
203 std::tuple<T, bool> fsTest(const string_view &) { static_assert(sizeof(T{})==0); return {T{},false}; }
204 template<> inline std::tuple<int32, bool> fsTest(const string_view &in) { return isInt32v(in); }
205 template<> inline std::tuple<uint32, bool> fsTest(const string_view &in) { return isUInt32v(in); }
206 template<> inline std::tuple<int64, bool> fsTest(const string_view &in) { return isInt64v(in); }
207 template<> inline std::tuple<uint64, bool> fsTest(const string_view &in) { return isUInt64v(in); }
208 template<> inline std::tuple<double, bool> fsTest(const string_view &in) { return isDoublev(in); }
209 template<> inline std::tuple<bool, bool> fsTest(const string_view &in) { return isBoolv(in); }
210 template<> inline std::tuple<string, bool> fsTest(const string_view &in) { return {string(in),true}; }
211
212 template <typename T> T fs(const string_view &, [[maybe_unused]] bool throwException = false) { static_assert(sizeof(T{})==0); return T{}; }
213 template<> inline int32 fs(const string_view &in,bool throwException) { return toInt32(in,throwException); }
214 template<> inline uint32 fs(const string_view &in,bool throwException) { return toUInt32(in,throwException); }
215 template<> inline int64 fs(const string_view &in,bool throwException) { return toInt64(in,throwException); }
216 template<> inline uint64 fs(const string_view &in,bool throwException) { return toUInt64(in,throwException); }
217 template<> inline double fs(const string_view &in,bool throwException) { return toDouble(in,throwException); }
218 template<> inline bool fs(const string_view &in,bool throwException) { return toBool(in,"on,off,true,false,yes,no",throwException); }
219 template<> inline string fs(const string_view &in,bool) { return {string(in),true}; }
220}
221
222// < and == functors for strings - to use in std containers when CI comparisons are wanted!
224 public:
225 bool operator()(const string_view &left, const string_view &right) const { return compare(left,right) < 0; }
226};
228 public:
229 bool operator()(const string_view &left, const string_view &right) const { return compare(left,right) == 0; }
230};
232 public:
233 uint64 operator()(const string_view &in) const { return caseInsensitiveHash(in); }
234};
235
236//----------------------------------------------------
237// Implementations
238//----------------------------------------------------
239
240// Might want to move this to a dedicated header so not everybody has to include these containers
241
242#include <map>
243#include <unordered_map>
244#include <set>
245#include <unordered_set>
246
247template <typename T> using StringMap = std::map<string, T, StringCILess>;
248
249template <typename T> using StringMultiMap = std::multimap<string, T, StringCILess>;
250
251template <typename T> using StringHashMap = std::unordered_map<string, T, StringCIHash, StringCIEqual>;
252
253using StringSet = std::set<string, StringCILess>;
254
255using StringHashSet = std::unordered_set<string, StringCIHash, StringCIEqual>;
256
257template <class T>
258inline string base::ts(const T &t, int width, char notation, int precision, char fill) {
259 string s = rformat(buildFormat(width, notation, precision, fill), t);
260 if (width!=0)
261 return clipLen(s,std::abs(width));
262 return s;
263}
264
265// OK the fmt;:format library will output -0.0000 instead of 0.0000 if the value was
266// <0 but below limits. This is confusing and breaks former output checks.
267// So we need a template override to check for that and get rid of it (sigh)
268template <>
269inline string base::ts(const double &dIn, int width, char notation, int precision, char fill) {
270 double d(dIn);
271 if (d==0.0) // Resolve -0
272 d = 0.0;
273 return rformat(buildFormat(width, notation, precision, fill),d);
274}
275
276template <typename T,typename U>
277T join(const std::vector<T> &s,const U &sep) {
278 string ret;
279 for (StringList::size_type i=0;i<s.size();++i) {
280 if (i) ret += sep;
281 ret += s[i];
282 }
283 return ret;
284}
285
286//----------------------------------------------------
287// Implementations
288//----------------------------------------------------
289
290template <typename T,typename ... Args>
291T toStringConv(const string_view &in,Args...args,bool te, std::tuple<T,bool> (*f)(const string_view &,Args...)) {
292 auto [val, ok] = f(in,args...);
293 if (te==true && ok==false)
294#ifdef __LINUX
295 throw std::runtime_error("String conversion error.");
296#else
297 throw std::exception("String conversion error.");
298#endif
299 return val;
300}
301
302template <class ... Args>
303struct FormatCheck : public std::format_string<std::type_identity_t<Args>...> {
304 template <unsigned N>
305 consteval FormatCheck(const char (&s)[N]) : std::format_string<std::type_identity_t<Args>...>(s) {
306 for (auto i=&(s[0]);i!=&(s[N]);++i) {
307 if (*i=='\\') { // Check for \ escape
308 ++i;
309 if (i==&(s[N])) break;
310 } else if (*i=='%') { // Check for %
311 ++i;
312 if (i==&(s[N]))
313 break;
314 if (*i >= '1' and *i <= '9')
315 assert(0 && "%1, %2 etc. token replacement is no longer supported. Use std::format {} syntax.");
316 }
317 }
318 }
319};
320
321// This allows you to send StringLists to a std::format
322template <>
323struct std::formatter<StringList> : public std::formatter<string> {
324 template <typename ParseContext>
325 constexpr auto parse(ParseContext &ctx) { return std::formatter<string>::parse(ctx); }
326
327 template <typename FormatContext>
328 constexpr auto format(StringList const &val, FormatContext &ctx) const {
329 return std::formatter<string>::format(join(val,","), ctx);
330 }
331};
332
333// EoF
Base type definitions for the engine.
Definition basestring.h:145
Definition basestring.h:71
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
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:25
BASE_EXPORT bool match(const string &keyword, const string &token, bool forceAbbreviationsAllowed=false)
‍**
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:303
Definition basestring.h:227
Definition basestring.h:231
Definition basestring.h:223
A overflow checked shorthand for static_cast<T>().