OpenLexocad  28.0
StringTool.h
Go to the documentation of this file.
1 #pragma once
2 #include <Base/String.h>
3 #include <iomanip>
4 #include <sstream>
5 #include <QString> // for QString
6 
7 class QUuid;
8 
9 namespace Base
10 {
11 class GlobalId;
12 class LX_BASE_EXPORT StringTool
13 {
14 public:
15  template <typename T>
16  static std::string toStlString(const T& t)
17  {
18  std::stringstream ss;
19  ss << t;
20  return ss.str();
21  }
22 
23  template <typename T>
24  static std::string toStlString(const T& t, int precision)
25  {
26  std::stringstream ss;
27  ss.precision(precision);
28  ss << t;
29  return ss.str();
30  }
31 
32  template <typename T>
33  static std::string toStlString(const T& t, int fieldWidth, char fillChar)
34  {
35  std::stringstream ss;
36  ss << std::setfill(fillChar) << std::setw(fieldWidth) << t;
37  return ss.str();
38  }
39 
40  static std::string toStlString(const bool& b, int fieldWidth, char fillChar)
41  {
42  std::string str;
43  if (b)
44  str = std::string("true");
45  else
46  str = std::string("false");
47 
48  std::stringstream ss;
49  ss << std::setfill(fillChar) << std::setw(fieldWidth) << str;
50  return ss.str();
51  }
52 
53  static std::string toStlString(const bool& b) { return b ? "true" : "false"; }
54 
55  static std::string toStlString(const QString& str);
56 
57  template <typename T>
58  static Base::String toString(const T& t)
59  {
60  std::wstringstream ss;
61  ss << t;
62  return ss.str();
63  }
64 
65  template <typename T>
66  static Base::String toString(const T& t, int precision)
67  {
68  std::wstringstream ss;
69  ss.precision(precision);
70  ss << t;
71  return ss.str();
72  }
73 
74  template <typename T>
75  static Base::String toString(const T& t, int fieldWidth, wchar_t fillChar)
76  {
77  std::wstringstream ss;
78  ss << std::setfill(fillChar) << std::setw(fieldWidth) << t;
79  return ss.str();
80  }
81 
82 
83  static Base::String toString(const bool& b, int fieldWidth, wchar_t fillChar)
84  {
85  Base::String str;
86  if (b)
87  str = L"true";
88  else
89  str = L"false";
90 
91  std::wstringstream ss;
92  ss << std::setfill(fillChar) << std::setw(fieldWidth) << str._utf16string;
93  return ss.str();
94  }
95 
96 
97  static Base::String toString(const bool& b)
98  {
99  if (b)
100  return L"true";
101  else
102  return L"false";
103  }
104 
106  static std::string toUpper(const std::string& str);
108  static std::string toLower(const std::string& str);
110  static Base::String toUpper(const Base::String& str);
112  static Base::String toLower(const Base::String& str);
113 
115  static std::string trimLeft(const std::string& str);
117  static std::string trimRight(const std::string& str);
119  static std::string trim(const std::string& str);
121  static Base::String trimLeft(const Base::String& str);
123  static Base::String trimRight(const Base::String& str);
125  static Base::String trim(const Base::String& str);
126 
128  static std::string replace(const std::string& str, const std::string& src, const std::string& rpl);
130  static Base::String replace(const Base::String& str, const Base::String& src, const Base::String& rpl);
131 
133  static bool isEmpty(const std::string& str);
135  static bool isEmpty(const Base::String& str);
136 
138  static bool toBool(const std::string& str, bool* ok = 0);
140  static bool toBool(const char* str, bool* ok = 0);
142  static bool toBool(const Base::String& str, bool* ok = 0);
144  static double toDouble(const std::string& str, bool* ok = 0);
146  static double toDouble(const char* str, bool* ok = 0);
148  static double toDouble(const Base::String& str, bool* ok = 0);
150  static float toFloat(const std::string& str, bool* ok = 0);
152  static uint8_t toUInt8(const std::string& str, bool* ok);
153  static uint8_t toUInt8(const Base::String& str, bool* ok);
154  static uint32_t toUInt32(const std::string& str, bool* ok);
155  static uint32_t toUInt32(const Base::String& str, bool* ok);
156  static uint64_t toUInt64(const std::string& str, bool* ok);
157  static uint64_t toUInt64(const Base::String& str, bool* ok);
158  static int toInt(const std::string& str, bool* ok = 0);
159  static int toInt(const Base::String& str, bool* ok = 0);
161  static char toChar(const std::string& str, bool* ok = 0);
163  static Base::String toUtf16(const std::string& utf8string);
165  static std::string toUtf8(const Base::String& widestring);
167  static std::wstring toWString(const Base::String& str);
169  static Base::String toString(const std::string& str);
170  static std::string toStlString(const Base::String& str);
171 
172 
174  static std::string toLegal(const std::string& str);
176  static std::string createGuidString();
178  static bool toUuid(const std::string& ifcguid, QUuid& uuid);
180  static bool toGUID(const std::string& ifcguid, std::string& guid);
182  static bool toBase64String(const std::string& in, std::string& out);
184  static bool toBase64String(const QUuid& in, std::string& out);
186  static bool toBase64String(const Base::GlobalId& in, Base::String& out);
188  static bool isValidIfcBase64String(const std::string& ifcguid);
190  static QString toQString(const Base::String& str);
192  static Base::String toString(const QString& str);
193 
194  static std::string toMultiByteString(const Base::String& str);
195 };
196 
197 
198 } // namespace Base
Base::StringTool::toFloat
static float toFloat(const std::string &str, bool *ok=0)
Converts the string representation of a float to a float value. ok = 'false' if conversion fails.
Base::StringTool::replace
static std::string replace(const std::string &str, const std::string &src, const std::string &rpl)
Returns a copy of the string. Replaces given text with something else.
Base::StringTool::toQString
static QString toQString(const Base::String &str)
Converts a Base::String to a QString.
Base::StringTool::toBase64String
static bool toBase64String(const std::string &in, std::string &out)
Create a IFCGUID string (p.e. 10YjwXGv9FPhDS3ghximXO ) from a GUID string.
Base::StringTool::toGUID
static bool toGUID(const std::string &ifcguid, std::string &guid)
Create a GUID string (p.e. {53FD4419-39F9-4211-AD37-29DA1D3E9AF8} ) from base64 string.
Base::StringTool::toBase64String
static bool toBase64String(const QUuid &in, std::string &out)
Create a IFCGUID string (p.e. 10YjwXGv9FPhDS3ghximXO ) from a QUuid.
Base::StringTool::toUuid
static bool toUuid(const std::string &ifcguid, QUuid &uuid)
Creates a QUuid from an IFC GUID (base 64) string.
Base::StringTool::toUpper
static Base::String toUpper(const Base::String &str)
Returns a copy of string which is converted to upper case.
Base::StringTool::trim
static Base::String trim(const Base::String &str)
Returns a copy of string with leading and trailing spaces removed.
Base::StringTool::toUInt8
static uint8_t toUInt8(const Base::String &str, bool *ok)
Base::StringTool::toBool
static bool toBool(const char *str, bool *ok=0)
Converts the string representation of a bool to a boolean value. ok = 'false' if conversion fails.
Base::StringTool::toUpper
static std::string toUpper(const std::string &str)
Returns a copy of string which is converted to upper case.
Base::StringTool::toString
static Base::String toString(const T &t, int fieldWidth, wchar_t fillChar)
Definition: StringTool.h:75
Base::StringTool::toUInt8
static uint8_t toUInt8(const std::string &str, bool *ok)
Converts the string representation of an int to an int value. ok = 'false' if conversion fails.
Base::GlobalId
Definition: GlobalId.h:28
Base::StringTool::createGuidString
static std::string createGuidString()
Creates a compressed GUID string. This version uses a number system with base 64 to obtain a string w...
Base::StringTool::isEmpty
static bool isEmpty(const Base::String &str)
Checks if a string contains characters.
Base::StringTool::toLower
static Base::String toLower(const Base::String &str)
Returns a copy of string which is converted to lower case.
Base::StringTool::toString
static Base::String toString(const T &t)
Definition: StringTool.h:58
Base::StringTool::toBase64String
static bool toBase64String(const Base::GlobalId &in, Base::String &out)
Create a IFCGUID string (p.e. 10YjwXGv9FPhDS3ghximXO ) from a Base::GlobalId.
Base::StringTool::trimRight
static Base::String trimRight(const Base::String &str)
Returns a copy of string with trailing spaces removed.
Base::StringTool::toWString
static std::wstring toWString(const Base::String &str)
Returns Base::String as a std::wstring.
Base::StringTool::toUtf16
static Base::String toUtf16(const std::string &utf8string)
Converts a UTF-8 encoded string to a UTF-16 encoded string. Throws std::exception on failure.
Base::StringTool::toString
static Base::String toString(const T &t, int precision)
Definition: StringTool.h:66
Base::StringTool
Definition: StringTool.h:13
Base::StringTool::isValidIfcBase64String
static bool isValidIfcBase64String(const std::string &ifcguid)
Checks if the base64 string was generated from a valid GUID.
Base::StringTool::toBool
static bool toBool(const Base::String &str, bool *ok=0)
Converts the string representation of a bool to a boolean value. ok = 'false' if conversion fails.
Base::StringTool::toUtf8
static std::string toUtf8(const Base::String &widestring)
Converts a UTF-16 encoded string to a UTF-8 encoded string. Throws std::exception on failure.
Base::StringTool::toUInt64
static uint64_t toUInt64(const Base::String &str, bool *ok)
Base::StringTool::toString
static Base::String toString(const bool &b)
Definition: StringTool.h:97
Base::StringTool::toUInt32
static uint32_t toUInt32(const Base::String &str, bool *ok)
Base::StringTool::toDouble
static double toDouble(const char *str, bool *ok=0)
Converts the string representation of a double to a double value. ok = 'false' if conversion fails.
Base::StringTool::toStlString
static std::string toStlString(const bool &b, int fieldWidth, char fillChar)
Definition: StringTool.h:40
Base::StringTool::toDouble
static double toDouble(const Base::String &str, bool *ok=0)
Converts the string representation of a double to a double value. ok = 'false' if conversion fails.
Base::StringTool::toString
static Base::String toString(const bool &b, int fieldWidth, wchar_t fillChar)
Definition: StringTool.h:83
Base::StringTool::toString
static Base::String toString(const std::string &str)
Converts a std::string to a Base::String.
Base::StringTool::toInt
static int toInt(const Base::String &str, bool *ok=0)
Base::StringTool::toUInt64
static uint64_t toUInt64(const std::string &str, bool *ok)
String.h
Base::StringTool::isEmpty
static bool isEmpty(const std::string &str)
Checks if a string contains characters.
Base::StringTool::toLower
static std::string toLower(const std::string &str)
Returns a copy of string which is converted to lower case.
Base::StringTool::toUInt32
static uint32_t toUInt32(const std::string &str, bool *ok)
Base::StringTool::toStlString
static std::string toStlString(const T &t, int fieldWidth, char fillChar)
Definition: StringTool.h:33
Base::StringTool::trimLeft
static std::string trimLeft(const std::string &str)
Returns a copy of string with leading spaces removed.
Base::StringTool::toStlString
static std::string toStlString(const T &t)
Definition: StringTool.h:16
Base::StringTool::toStlString
static std::string toStlString(const T &t, int precision)
Definition: StringTool.h:24
Base::StringTool::toMultiByteString
static std::string toMultiByteString(const Base::String &str)
Base::StringTool::trimLeft
static Base::String trimLeft(const Base::String &str)
Returns a copy of string with leading spaces removed.
Base::StringTool::toLegal
static std::string toLegal(const std::string &str)
Replaces all characters from the string which are not letters or numbers with a '_'.
Base::StringTool::toChar
static char toChar(const std::string &str, bool *ok=0)
Converts the string representation of a char to a char value. ok = 'false' if conversion fails.
Base::String
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:18
Base::StringTool::toDouble
static double toDouble(const std::string &str, bool *ok=0)
Converts the string representation of a double to a double value. ok = 'false' if conversion fails.
Base::StringTool::trim
static std::string trim(const std::string &str)
Returns a copy of string with leading and trailing spaces removed.
Base::StringTool::replace
static Base::String replace(const Base::String &str, const Base::String &src, const Base::String &rpl)
Returns a copy of the string. Replaces given text with something else.
Base::StringTool::toString
static Base::String toString(const QString &str)
Converts a QString to a Base::String.
Base::StringTool::toStlString
static std::string toStlString(const Base::String &str)
Base::StringTool::toStlString
static std::string toStlString(const bool &b)
Definition: StringTool.h:53
Base::StringTool::toStlString
static std::string toStlString(const QString &str)
Base::StringTool::toBool
static bool toBool(const std::string &str, bool *ok=0)
Converts the string representation of a bool to a boolean value. ok = 'false' if conversion fails.
Base::StringTool::trimRight
static std::string trimRight(const std::string &str)
Returns a copy of string with trailing spaces removed.
Base::StringTool::toInt
static int toInt(const std::string &str, bool *ok=0)
Base
Definition: AbstractXMLReader.h:5