Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StringUtils.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __STRING_UTILS_HH__
26 #define __STRING_UTILS_HH__
27 
28 // std
29 #include <string>
30 #include <vector>
31 #include <algorithm>
32 #include <iomanip>
33 #include <sstream>
34 
35 // BIAS
36 #include <BIAS_DeclSpec.hh>
37 #include <Base/Debug/Error.hh>
38 
39 namespace BIAS {
40 
41  /**
42  * @class StringUtils
43  * @brief Contains some useful methods for string manipulation.
44  * @ingroup g_utils
45  * @author rwulff 04/2010
46  */
47  class BIASUtils_EXPORT StringUtils {
48 
49  public:
50 
51  /**
52  * @brief Explodes the given string into tokens which are separated by the
53  * given separator string.
54  *
55  * Usage example:
56  *
57  * std::vector<std::string> retVec;
58  * StringUtils.Explode("token1 token2 token3", " ", retVec);
59  *
60  * retVec will now contain the three elements "token1", "token2" and
61  * "token3"
62  *
63  * @param str
64  * the string to explode
65  * @param seperator
66  * the string that separates each token from the next
67  * @param ret
68  * a vector that contains the single tokens
69  */
70  static
71  void Explode(const std::string &str,
72  const std::string &seperator,
73  std::vector<std::string> &ret);
74 
75  /**
76  * @brief Splits the given string into tokens which are separated by any of
77  * the given separator characters (similar to #StringUtils::Explode).
78  * @param str
79  * the string to split
80  * @param ret
81  * a vector that contains the single tokens
82  * @param seperator
83  * the characters that separate each token from the next
84  */
85  static
86  void SplitByChars(const std::string &str,
87  std::vector<std::string> &ret,
88  const std::string &charsToSplit = "\t\n ,;");
89 
90  /**
91  * @brief Trims the given string, i.e. removes all permutations of the
92  * given charsToTrim from the front and the end of the string.
93  *
94  * Usage example:
95  *
96  * std::string retStr;
97  * StringUtils::Trim("_/_string_/_", retStr, "/_");
98  *
99  * retStr will now be "string"
100  *
101  * @param str
102  * the string to trim
103  * @param ret
104  * the trimmed string
105  * @param charsToTrim
106  * the characters to remove from the front and the end of the string.
107  * the default is to remove all whitespace charakters (spaces, tabs
108  * and newlines). note that the order of characters is irrelevant
109  */
110  static
111  void Trim(const std::string &str,
112  std::string &ret,
113  const std::string &charsToTrim = " \t\n");
114 
115  /**
116  * \brief compare two strings ignoring their case
117  * \param[in] str1 first string to compare
118  * \param[in] str2 second string to compare
119  * \return true if strings are equal, false otherwise
120  */
121  static inline
122  bool CompareNoCase( const std::string& str1, const std::string& str2 ) {
123  std::string str1Cpy( str1 );
124  std::string str2Cpy( str2 );
125  std::transform( str1Cpy.begin(), str1Cpy.end(), str1Cpy.begin(), ::tolower );
126  std::transform( str2Cpy.begin(), str2Cpy.end(), str2Cpy.begin(), ::tolower );
127  return ( str1Cpy == str2Cpy );
128  }
129 
130  /**
131  * @deprecated call PadUintByNumChars() instead
132  */
133  static inline
134  std::string LeadingZeroString(int value, int num) {
135  BIASWARNONCE("deprecated, call PadUintByNumChars() instead")
136  return PadUintByNumChars(value, num);
137  }
138 
139  /**
140  * @brief Pads the given number with leading zeroes so that the string
141  * representation will use the number of characters determined by
142  * num (if it is not already bigger).
143  *
144  * @param value
145  * the value to pad
146  * @param num
147  * value is padded to use this number of characters
148  *
149  * @return the padded string
150  */
151  static inline
152  std::string PadUintByNumChars(unsigned int value, unsigned int num)
153  {
154  std::stringstream ss;
155  ss << std::setfill('0') << std::setw(num) << value;
156  return ss.str();
157  }
158 
159  /**
160  * @brief Pads the given value with leading zeroes so that the string
161  * representation will use the same number of characters as max.
162  *
163  * @param value
164  * the value to pad
165  * @param max
166  * value is padded to use the same number of characters as this one
167  *
168  * @return the padded string
169  */
170  static inline
171  std::string PadUintByMaxValue(unsigned int value, unsigned int max)
172  {
173  std::stringstream ss;
174 
175  ss << max;
176  const unsigned int lengthMax = ss.str().size();
177 
178  ss.str("");
179  ss << std::setw(lengthMax) << std::setfill('0') << value;
180  return ss.str();
181  }
182 
183  private:
184 
185  /**
186  * @brief Hidden default constructor.
187  */
188  StringUtils()
189  { }
190 
191  }; // end of class
192 } // end of namespace
193 
194 #endif // __STRING_UTILS_HH__
static std::string LeadingZeroString(int value, int num)
Definition: StringUtils.hh:134
static std::string PadUintByMaxValue(unsigned int value, unsigned int max)
Pads the given value with leading zeroes so that the string representation will use the same number o...
Definition: StringUtils.hh:171
static std::string PadUintByNumChars(unsigned int value, unsigned int num)
Pads the given number with leading zeroes so that the string representation will use the number of ch...
Definition: StringUtils.hh:152
static bool CompareNoCase(const std::string &str1, const std::string &str2)
compare two strings ignoring their case
Definition: StringUtils.hh:122
Contains some useful methods for string manipulation.
Definition: StringUtils.hh:47