Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
StringUtils.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21 
27 #ifndef STRINGUTILS
28 #define STRINGUTILS
29 
30 #include <string>
31 #include <sstream>
32 #include <iomanip>
33 #include <map>
34 #include <vector>
35 #include <limits>
36 #include <cmath>
37 #include "CoreLibraryDllGlobal.h"
39 
41 namespace core_lib
42 {
44 namespace string_utils
45 {
46 
55 void CORE_LIBRARY_DLL_SHARED_API PackStdString(std::string& line);
56 
64 {
66  trimmed,
69 };
70 
85 void CORE_LIBRARY_DLL_SHARED_API SplitString(std::string& subStr1, std::string& subStr2,
86  const std::string& toSplit, const std::string& delim,
87  eSplitStringResult option);
88 
95 {
97  normal,
99  fixed,
101  scientific
102 };
103 
114 template <typename T>
115 std::string FormatFloatString(const T value, const int precision = 15,
117 {
118  std::ostringstream ss;
119 
120  switch (formatting)
121  {
123  ss << std::fixed;
124  break;
126  ss << std::scientific;
127  break;
129  // do nothing
130  break;
131  }
132 
133  if (precision >= 0)
134  {
135  ss << std::setprecision(precision);
136  }
137 
138  ss << value;
139  return ss.str();
140 }
141 
151 template <typename T> std::string AutoFormatFloatString(const T value, int decimalPlaces = 1)
152 {
153  std::string formattedValue;
154 
155  if (decimalPlaces < 0)
156  {
157  decimalPlaces = 0;
158  }
159 
160  const T min = std::pow(T(10), T(-1) * decimalPlaces);
161  const T absVal = std::fabs(value);
162 
163  if (absVal < std::numeric_limits<T>::epsilon()) // 0.0 case
164  {
165  formattedValue = FormatFloatString(value, 1 + decimalPlaces);
166  }
167  else if ((absVal < min) || (absVal >= T(100000))) // very small and very big values
168  {
169  formattedValue = FormatFloatString(value, decimalPlaces, eFloatStringFormat::scientific);
170  }
171  else // everything in between
172  {
173  if (absVal < T(1))
174  {
175  formattedValue = FormatFloatString(value, decimalPlaces);
176  }
177  else if (absVal < T(10))
178  {
179  formattedValue = FormatFloatString(value, 1 + decimalPlaces);
180  }
181  else if (absVal < T(100))
182  {
183  formattedValue = FormatFloatString(value, 2 + decimalPlaces);
184  }
185  else if (absVal < T(1000))
186  {
187  formattedValue = FormatFloatString(value, 3 + decimalPlaces);
188  }
189  else if (absVal < T(10000))
190  {
191  formattedValue = FormatFloatString(value, 4 + decimalPlaces);
192  }
193  else
194  {
195  formattedValue = FormatFloatString(value, 5 + decimalPlaces);
196  }
197  }
198 
199  return formattedValue;
200 }
201 
209 std::wstring CORE_LIBRARY_DLL_SHARED_API RemoveIllegalChars(
210  const std::wstring& text, const std::wstring& illegalChars = L"~#%&*{}\\:<>?/+|\"",
211  wchar_t replacementChar = L'_');
212 
213 std::string CORE_LIBRARY_DLL_SHARED_API
214  RemoveIllegalChars(const std::string& text, const std::string& illegalChars = "~#%&*{}\\:<>?/+|\"",
215  char replacementChar = '_');
216 
222 std::wstring CORE_LIBRARY_DLL_SHARED_API StringToWString(const std::string& text);
223 
229 std::string CORE_LIBRARY_DLL_SHARED_API WStringToString(const std::wstring& text);
230 
236 bool CORE_LIBRARY_DLL_SHARED_API IsAlphaNumeric(const std::wstring& text);
237 
238 bool CORE_LIBRARY_DLL_SHARED_API IsAlphaNumeric(const std::string& text);
239 
248 template <typename Iter>
249 std::string MakeHexString(Iter first, Iter last, bool useUppercase, bool insertSpaces)
250 {
251  std::ostringstream ss;
252  ss << std::hex << std::setfill('0');
253 
254  if (useUppercase)
255  {
256  ss << std::uppercase;
257  }
258 
259  while (first != last)
260  {
261  ss << std::setw(2) << static_cast<int>(*first++);
262 
263  if (insertSpaces && first != last)
264  {
265  ss << " ";
266  }
267  }
268 
269  return ss.str();
270 }
271 
279 std::vector<std::string> CORE_LIBRARY_DLL_SHARED_API TokeniseString(std::string const& text,
280  std::string const& separator,
281  bool keepEmptyTokens = false);
282 
290 std::string CORE_LIBRARY_DLL_SHARED_API
291  ReplaceTokens(std::string const& text, std::map<std::string, std::string> const& tokenMap);
292 
293 } // namespace string_utils
294 } // namespace core_lib
295 
296 #endif // STRINGUTILS
eFloatStringFormat
Format float options enumeration.
Definition: StringUtils.h:94
std::string AutoFormatFloatString(const T value, int decimalPlaces=1)
Convert a floating point value to a string representation using the most suitable formatting...
Definition: StringUtils.h:151
std::string FormatFloatString(const T value, const int precision=15, eFloatStringFormat formatting=eFloatStringFormat::normal)
Convert a floating point value to a formatted string representation.
Definition: StringUtils.h:115
bool CORE_LIBRARY_DLL_SHARED_API IsAlphaNumeric(const std::wstring &text)
Return a flag to indicate whether string contains only alphanumeric chars.
Definition: StringUtils.cpp:137
Trim the results, removing pre/pro-ceeding spaces.
The core_lib namespace.
Definition: AsioDefines.h:59
std::wstring CORE_LIBRARY_DLL_SHARED_API RemoveIllegalChars(const std::wstring &text, const std::wstring &illegalChars=L"~#%&*{}<>?/+|\, wchar_t replacementChar=L '_')
Return a string with any illegal chars replaced with replacement char.
Definition: StringUtils.cpp:94
eSplitStringResult
Split string options enumeration.
Definition: StringUtils.h:63
void CORE_LIBRARY_DLL_SHARED_API SplitString(std::string &subStr1, std::string &subStr2, const std::string &toSplit, const std::string &delim, eSplitStringResult option)
Split a string into two parts given delimiters.
Definition: StringUtils.cpp:61
File containing declaration of DLL import/export control defines.
std::wstring CORE_LIBRARY_DLL_SHARED_API StringToWString(const std::string &text)
Convert a std::string to std::wstring.
Definition: StringUtils.cpp:122
void CORE_LIBRARY_DLL_SHARED_API PackStdString(std::string &line)
Tidy a string obtained from getline function.
Definition: StringUtils.cpp:47
File containing platform specific definitions.
std::string CORE_LIBRARY_DLL_SHARED_API ReplaceTokens(std::string const &text, std::map< std::string, std::string > const &tokenMap)
Gieven a string containing tokens, replace tokens with specific string values.
Definition: StringUtils.cpp:173
std::string MakeHexString(Iter first, Iter last, bool useUppercase, bool insertSpaces)
Convert a range of data that is convertible to ints to a string of hex values.
Definition: StringUtils.h:249
std::vector< std::string > CORE_LIBRARY_DLL_SHARED_API TokeniseString(std::string const &text, std::string const &separator, bool keepEmptyTokens=false)
Tokenise a string separated by a separator substring and split it into tokens.
Definition: StringUtils.cpp:154
std::string CORE_LIBRARY_DLL_SHARED_API WStringToString(const std::wstring &text)
Convert a std::wstring to std::string.
Definition: StringUtils.cpp:128