CARMA C++
StringUtils.h
Go to the documentation of this file.
1 
12 #ifndef CARMA_UTIL_STRINGUTILS_H
13 #define CARMA_UTIL_STRINGUTILS_H
14 
15 #include <string>
16 #include <set>
17 #include <vector>
18 #include <sstream>
19 
20 namespace carma {
21  namespace util {
22 
30  typedef enum {
31  MD5,
32  SHA1
33  } DigestType;
34 
46  template <typename T> ::std::string vectorToString
47  (const ::std::vector<T>& v, const ::std::string& delimiter=", ") {
48  ::std::ostringstream os;
49  int size = v.size();
50  for(int i=0; i<size; i++) {
51  os << v[i];
52  if(i < size-1) {
53  os << delimiter;
54  }
55  }
56  return os.str();
57  }
58 
59 
71  template <typename T> ::std::string setToString
72  (const ::std::set<T>& s, const ::std::string& delimiter=", ") {
73  ::std::ostringstream os;
74  int size = s.size();
75  typename ::std::set<T>::iterator iter;
76  int count = 0;
77  for(iter=s.begin(); iter != s.end(); iter++) {
78  os << *iter;
79  if(count < size-1) {
80  os << delimiter;
81  }
82  count++;
83  }
84  return os.str();
85  }
86 
87 
94 class StringUtils {
95 public:
96 
101  typedef enum sortTypeEnum {
106  } sortType;
107 
108 
112  StringUtils();
113 
117  virtual ~StringUtils();
118 
127  static ::std::string lowASCIIAlphaNumericToUpper(const ::std::string& str);
128 
134  static void toLower(::std::vector< ::std::string > & v);
135 
141  static void toUpper(::std::vector< ::std::string > & v);
142 
151  static ::std::string lowASCIIAlphaNumericToLower(const ::std::string& str);
152 
159  static ::std::string erase(const ::std::string& str,
160  const ::std::vector<char>& chars);
161 
167  static void trimInplace( ::std::string & str,
168  const ::std::string & chars );
169 
176  static ::std::string trim( const ::std::string & str,
177  const ::std::string & chars );
178 
179 
185  static void trimWhiteSpaceInplace( ::std::string & str );
186 
196  static ::std::string trimWhiteSpace( const ::std::string & str );
197 
198 
209  static ::std::string collapse(const ::std::string& str,
210  const ::std::vector<char>& chars);
211 
222  static ::std::string collapse(const ::std::string& str, char c);
223 
231  static ::std::string replace(const ::std::string& str,
232  const ::std::string& origStr,
233  const ::std::string& replStr);
234 
243  static void
244  tokenizeInplace( ::std::vector< ::std::string > & tokens,
245  const ::std::string & str,
246  const ::std::string & delimiterSet = " ",
247  bool skipNull = true );
248 
257  static ::std::vector< ::std::string >
258  tokenize( const ::std::string & str,
259  const ::std::string & delimiterSet = " ",
260  bool skipNull = true );
261 
272  static void
273  tokenizeNInplace( ::std::vector< ::std::string > & tokens,
274  const ::std::string & str,
275  const size_t maxTokens,
276  const ::std::string & delimiterSet = " ",
277  bool skipNull = true );
278 
289  static ::std::vector< ::std::string >
290  tokenizeN( const ::std::string & str,
291  const size_t maxTokens,
292  const ::std::string & delimiterSet = " ",
293  bool skipNull = true );
294 
295  static bool miniGlob(const char *pattern, const char *checkString);
296 
312  static int stringToInt(const ::std::string& str);
313 
319  static ::std::string hms(double angle, int precision=2);
320  static ::std::string dms(double angle, int precision=3);
321  static ::std::string shms(double angle, int precision=2);
322  static ::std::string sdms(double angle, int precision=3);
323  static ::std::string sexa(double angle, bool sign, bool hms, int precision);
324 
331  static ::std::string
332  computeMessageDigest( const ::std::string & str,
333  DigestType digestType );
334 
335 
336 
345  static bool equalsIgnoreCase(const std::string & aString,
346  const std::string & anotherString);
347 
360  static bool containsOnly( const ::std::string & str,
361  const ::std::string & allowedChars );
369  static ::std::string sort( const ::std::string & str,
370  const sortType direction = ASCENDING_SORT);
371 
377  static ::std::string uniq( const ::std::string & str);
378 
384  static ::std::string reallyUniq( const ::std::string & str);
385 
392  static bool isEmptyOrContainsWhiteSpace( const ::std::string & s);
393 };
394 
395 
396 
397 }}
398 #endif // CARMA_UTIL_STRINGUTILS_H
::std::string hms(double angle, int precision=2)
convert a number into sexadecimal HMS/DMS notation, with or without a sign [+/-]HH:MM:SS.ss or [+/-]DDD:MM:SS.sss (precision# digits)
static void trimWhiteSpaceInplace(::std::string &str)
Erase whitespace (&#39; &#39;, ,,) from beginning and end of string.
static void trimInplace(::std::string &str, const ::std::string &chars)
erase specified characters from the beginning and end of a string
virtual ~StringUtils()
Destructor.
::std::string uniq(const ::std::string &str)
::std::string lowASCIIAlphaNumericToLower(const ::std::string &str)
convert a string containing low ASCII alphanumeric and punctionation characters (unicode 0x20 to 0x7E...
static void toUpper(::std::vector< ::std::string > &v)
In place conversion of a vector of string to upper case.
static::std::vector< ::std::string > tokenize(const ::std::string &str, const ::std::string &delimiterSet=" ", bool skipNull=true)
split a string into substrings based on a delimiter character set
Common string functions Additional string-related functions can be added here.
Definition: StringUtils.h:94
static void toLower(::std::vector< ::std::string > &v)
In place conversion of a vector of string to lower case.
static bool isEmptyOrContainsWhiteSpace(const ::std::string &s)
static int stringToInt(const ::std::string &str)
convert a string representation of an integer to an int
static void tokenizeInplace(::std::vector< ::std::string > &tokens, const ::std::string &str, const ::std::string &delimiterSet=" ", bool skipNull=true)
split a string into substrings based on a delimiter character set
static::std::vector< ::std::string > tokenizeN(const ::std::string &str, const size_t maxTokens, const ::std::string &delimiterSet=" ", bool skipNull=true)
split a string into substrings based on a delimiter character set stopping if a maximum number of sub...
::std::string trim(const ::std::string &str, const ::std::string &chars)
erase specified characters from the beginning and end of a string
static void tokenizeNInplace(::std::vector< ::std::string > &tokens, const ::std::string &str, const size_t maxTokens, const ::std::string &delimiterSet=" ", bool skipNull=true)
split a string into substrings based on a delimiter character set stopping if a maximum number of sub...
DigestType
digest type &lt;ui&gt; MD5 md5 SHA1 sha1
Definition: StringUtils.h:30
static bool equalsIgnoreCase(const std::string &aString, const std::string &anotherString)
Compares two strings, ignoring case considerations.
::std::string lowASCIIAlphaNumericToUpper(const ::std::string &str)
convert a string containing low ASCII alphanumeric and punctionation characters (unicode 0x20 to 0x7E...
enum carma::util::StringUtils::sortTypeEnum sortType
Sort type for sort( std::string, sortType ) method.
::std::string collapse(const ::std::string &str, const ::std::vector< char > &chars)
collapse consecutive repeats of a single character in specified character list to a single character ...
static bool containsOnly(const ::std::string &str, const ::std::string &allowedChars)
Checks that the input string is made up of only the characters from a given list. ...
::std::string replace(const ::std::string &str, const ::std::string &origStr, const ::std::string &replStr)
replace a substring within a string with another substring
::std::string sort(const ::std::string &str, const sortType direction=ASCENDING_SORT)
::std::string reallyUniq(const ::std::string &str)
::std::string trimWhiteSpace(const ::std::string &str)
Erase whitespace (&#39; &#39;, ,,) from beginning and end of string.
::std::string computeMessageDigest(const ::std::string &str, DigestType digestType)
compute the specified message digest for the specified string
::std::string erase(const ::std::string &str, const ::std::vector< char > &chars)
erase specified characters from a string
StringUtils()
Default constructor.