bpp-core  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Friends
TextTools.h
Go to the documentation of this file.
1 //
2 // File: TextTools.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Aug 8 12:57:50 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10  This software is a computer program whose purpose is to provide basal and
11  utilitary classes. This file belongs to the Bio++ Project.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #ifndef _TEXTTOOLS_H_
41 #define _TEXTTOOLS_H_
42 
43 #include "../Exceptions.h"
44 
45 
46 // From the STL:
47 #include <string>
48 #include <vector>
49 #include <sstream>
50 #include <iomanip>
51 
52 namespace bpp
53 {
54 
58  class TextTools
59  {
60  public:
61 
71  static bool isEmpty(const std::string& s);
72 
79  static std::string toUpper(const std::string& s);
80 
87  static std::string toLower(const std::string& s);
88 
95  static bool isWhiteSpaceCharacter(char c);
96 
103  static std::string removeWhiteSpaces (const std::string& s);
104 
111  static std::string removeFirstWhiteSpaces (const std::string& s);
112 
119  static std::string removeLastWhiteSpaces (const std::string& s);
120 
128  static std::string removeSurroundingWhiteSpaces(const std::string& s);
129 
136  static bool isNewLineCharacter(char c);
137 
144  static std::string removeNewLines (const std::string& s);
145 
152  static std::string removeLastNewLines(const std::string& s);
153 
160  static bool isDecimalNumber(char c);
161 
171  static bool isDecimalNumber(const std::string& s, char dec = '.', char scientificNotation = 'e');
172 
181  static bool isDecimalInteger(const std::string& s, char scientificNotation = 'e');
182 
189  template<class T> static std::string toString(T t)
190  {
191  std::ostringstream oss;
192  oss << t;
193  return oss.str();
194  }
195 
203  template<class T>
204  static std::string toString(T t, int precision)
205  {
206  std::ostringstream oss;
207  oss << std::setprecision(precision) << t;
208  return oss.str();
209  }
210 
217  template<class T> static T fromString(const std::string& s)
218  {
219  std::istringstream iss(s);
220  T obj;
221  iss >> obj;
222  return obj;
223  }
224 
231  static std::string toString(int i);
232 
239  static std::string toString(char c);
240 
248  static std::string toString(double d, int precision = 6);
249 
257  static int toInt(const std::string& s) throw (Exception);
258 
266  static double toDouble(const std::string& s) throw (Exception);
267 
274  template<class T>
275  static T to(const std::string& s)
276  {
277  std::istringstream iss(s);
278  T t;
279  iss >> t;
280  return t;
281  }
282 
292  static std::string resizeRight(const std::string& s, size_t newSize, char fill = ' ');
293 
303  static std::string resizeLeft(const std::string& s, size_t newSize, char fill = ' ');
304 
314  static std::vector<std::string> split(const std::string& s, size_t n);
315 
329  static std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding)
330  throw (Exception);
331 
349  static std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding, std::vector<std::string>& exceptionsBeginning, std::vector<std::string>& exceptionsEnding)
350  throw (Exception);
351 
359  static std::string removeChar(const std::string& s, char c);
360 
368  static unsigned int count(const std::string& s, const std::string& pattern);
369 
377  static bool startsWith(const std::string& s, const std::string& pattern);
378 
386  static bool endsWith(const std::string& s, const std::string& pattern);
387 
395  static bool hasSubstring(const std::string& s, const std::string& pattern);
396 
404  static void replaceAll(std::string& target, const std::string& query, const std::string& replacement);
405 
406  };
407 
408 } //end of namespace bpp.
409 
410 #endif //_TEXTTOOLS_H_
411