TrueReality  v0.1.1912
StringUtils.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author David Guthrie
25 * @author John K. Grant
26 * @author William E. Johnson II
27 * @author Chris Osborn
28 * @author Maxim Serebrennik
29 */
30 #pragma once
31 
32 #include <trUtil/Export.h>
33 
34 #include <trUtil/UnaryFunction.h>
35 #include <trUtil/WarningUtils.h>
36 
37 #include <string>
38 #include <sstream>
39 #include <locale>
40 #include <vector>
41 #include <algorithm>
42 
49 {
50  class IsSpace;
51 
53  static const std::string STR_BLANK("");
54 
60  template <class Pred = IsSpace>
62  {
63  public:
64 
74  static void tokenize(std::vector<std::string>& tokens, const std::string& stringToParse, const Pred& predFxn = Pred());
75 
85  static void tokenize(std::vector<std::string>& tokens, const std::string& stringToParse, const std::string& delimiter);
86  };
87 
98  template <class Pred>
99  inline void StringTokenizer<Pred>::tokenize(std::vector<std::string>& tokens, const std::string& stringToParse, const Pred& predFxn)
100  {
101  //First clear the results std::vector
102  tokens.clear();
103  std::string::const_iterator it = stringToParse.begin();
104  std::string::const_iterator itTokenEnd = stringToParse.begin();
105  while (it != stringToParse.end())
106  {
107  //Eat separators
108  if (predFxn(*it))
109  {
110  it++;
111  }
112  else
113  {
114  //Find next token
115  itTokenEnd = std::find_if(it, stringToParse.end(), predFxn);
116  //Append token to result
117  if (it < itTokenEnd)
118  {
119  tokens.push_back(std::string(it, itTokenEnd));
120  }
121  it = itTokenEnd;
122  }
123  }
124  }
125 
132  class TR_UTIL_EXPORT IsSpace : public trUtil::UnaryFunction<char, bool>
133  {
135  public:
136  static const std::string DEFAULT_LOCALE_NAME;
137  IsSpace(const std::locale& loc = std::locale(DEFAULT_LOCALE_NAME.c_str())) : mLocale(loc) {}
138 
139  const std::locale& GetLocale() const { return mLocale; }
140  bool operator()(char c) const { return std::isspace(c, mLocale); }
142  private:
143  std::locale mLocale;
144  };
145 
151  class IsSlash : public trUtil::UnaryFunction<char, bool>
152  {
153  public:
154  bool operator()(char c) const { return c == '/'; }
155  };
156 
163  class IsDelimeter : public trUtil::UnaryFunction<char, bool>
164  {
165  public:
166  IsDelimeter(char delim) : mDelimeter(delim) { }
167  bool operator()(char c) const { return c == mDelimeter; }
168  private:
170  };
171 
181  TR_UTIL_EXPORT const std::string& Trim(std::string& toTrim);
182 
195  TR_UTIL_EXPORT int StrCompare(const std::string& one, const std::string& two, bool caseSensitive = true);
196 
204  TR_UTIL_EXPORT void ToLowerCase(std::string& str);
205 
215  TR_UTIL_EXPORT std::string ToLowerCase(const std::string& str);
216 
224  TR_UTIL_EXPORT void ToUpperCase(std::string& str);
225 
235  TR_UTIL_EXPORT std::string ToUpperCase(const std::string& str);
236 
244  {
253  StrCompareFunc(const std::string& mainString, bool caseSensitive = true)
254  : mString(mainString)
255  , mCaseSensitive(caseSensitive)
256  {
257  }
258 
259  bool operator() (const std::string& toCompare)
260  {
261  return StrCompare(toCompare, mString, mCaseSensitive) == 0;
262  }
263 
264  const std::string& mString;
266  };
267 
286  template<class VecType>
287  bool ParseVec(const std::string& value, VecType& vec, unsigned size, unsigned numberPrecision = 16)
288  {
289  bool result = true;
290  unsigned int i;
291 
292  if (value.empty() || value == "NULL")
293  {
294  for (i = 0; i < size; ++i)
295  {
296  vec[i] = 0.0;
297  }
298  }
299  else
300  {
301  std::istringstream iss(value);
302  iss.precision(numberPrecision);
303  for (i = 0; i < size && !iss.eof(); ++i)
304  {
305  iss >> vec[i];
306  }
307 
308  //did we run out of data?
309  if (i < size)
310  result = false;
311  }
312 
313  return result;
314  }
315 
333  template<typename T>
334  std::string ToString(const T& t, int precision = -1)
335  {
336  std::ostringstream ss;
337  if (precision > 0)
338  {
339  ss.precision(precision);
340  }
341  ss << t;
342  return ss.str();
343  }
344 
345 
361  template<typename T>
362  T FromString(const std::string& u)
363  {
364  T result = T();
365  std::istringstream is;
366  is.str(u);
367  is >> result;
368  return result;
369  }
370 
371 
383  template<>
384  bool TR_UTIL_EXPORT FromString<bool>(const std::string& u);
385 
396  bool TR_UTIL_EXPORT Match(const char* wildCards, const char* str);
397 
409  void TR_UTIL_EXPORT MakeIndexString(unsigned index, std::string& toFill, unsigned paddedLength = 4);
410 
426  bool TR_UTIL_EXPORT TakeToken(std::string& data, std::string& outToken, char openChar, char closeChar);
427 
438  void TR_UTIL_EXPORT FindAndReplace(std::string& modifiedString, const std::string& findWhat, const std::string& replaceWith);
439 }
bool operator()(char c) const
Definition: StringUtils.h:154
A functor for using std::find or whatever that will compare two strings case sensitive or insensitive...
Definition: StringUtils.h:243
A unary function created to have a compatibility to the old std::unary_function while still using c++...
Definition: UnaryFunction.h:41
A functor which tests if a character is whitespace.
Definition: StringUtils.h:132
#define TR_DISABLE_WARNING_START_MSVC(...)
Definition: WarningUtils.h:62
static const std::string DEFAULT_LOCALE_NAME
Definition: StringUtils.h:136
Generic string delimeter check function class.
Definition: StringUtils.h:163
The predicate should evaluate to true when applied to a separator.
Definition: StringUtils.h:61
bool TR_UTIL_EXPORT Match(const char *wildCards, const char *str)
Matches.
static const std::string STR_BLANK("")
Constant value for a blank String.
bool ParseVec(const std::string &value, VecType &vec, unsigned size, unsigned numberPrecision=16)
A templated function for taking any of the osg vector types and reading the data from a string...
Definition: StringUtils.h:287
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
bool operator()(char c) const
Definition: StringUtils.h:140
bool TR_UTIL_EXPORT TakeToken(std::string &data, std::string &outToken, char openChar, char closeChar)
Reads the next token fromm the given string data.
void TR_UTIL_EXPORT FindAndReplace(std::string &modifiedString, const std::string &findWhat, const std::string &replaceWith)
Finds all instances of the findWhat string in the string modifiedString and replaces them with the re...
static void tokenize(std::vector< std::string > &tokens, const std::string &stringToParse, const Pred &predFxn=Pred())
The predicate should evaluate to true when applied to a separator.
Definition: StringUtils.h:99
#define TR_DISABLE_WARNING_END
Definition: WarningUtils.h:65
bool TR_UTIL_EXPORT FromString< bool >(const std::string &u)
Special exception for bool where things like "True", "TRUE", and "true" should be accepted...
T FromString(const std::string &u)
Converts a string to a specified type.
Definition: StringUtils.h:362
TR_UTIL_EXPORT const std::string & Trim(std::string &toTrim)
Trims whitespace off the front and end of a string.
Definition: StringUtils.cpp:58
A class that represents date time utility.
StrCompareFunc(const std::string &mainString, bool caseSensitive=true)
Constructor.
Definition: StringUtils.h:253
bool operator()(char c) const
Definition: StringUtils.h:167
TR_UTIL_EXPORT void ToUpperCase(std::string &str)
Converts the whole string to upper case.
TR_UTIL_EXPORT int StrCompare(const std::string &one, const std::string &two, bool caseSensitive=true)
Compares strings like strcmp or stricmp or strcasecmp.
Definition: StringUtils.cpp:74
void TR_UTIL_EXPORT MakeIndexString(unsigned index, std::string &toFill, unsigned paddedLength=4)
Makes index string.
IsSpace(const std::locale &loc=std::locale(DEFAULT_LOCALE_NAME.c_str()))
Definition: StringUtils.h:137
Determines if the current character is a forward slash.
Definition: StringUtils.h:151
TR_UTIL_EXPORT void ToLowerCase(std::string &str)
Converts the whole string to lower case.
Definition: StringUtils.cpp:89
const std::locale & GetLocale() const
Definition: StringUtils.h:139