bpp-core3  3.0.0
TextTools.h
Go to the documentation of this file.
1 //
2 // File: TextTools.h
3 // Authors:
4 // Julien Dutheil
5 // Francois Gindraud (2017)
6 // Created: 2003-08-08 12:57:50
7 // Last modified: 2017-06-26 00:00:00
8 //
9 
10 /*
11  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
12 
13  This software is a computer program whose purpose is to provide basal and
14  utilitary classes. This file belongs to the Bio++ Project.
15 
16  This software is governed by the CeCILL license under French law and
17  abiding by the rules of distribution of free software. You can use,
18  modify and/ or redistribute the software under the terms of the CeCILL
19  license as circulated by CEA, CNRS and INRIA at the following URL
20  "http://www.cecill.info".
21 
22  As a counterpart to the access to the source code and rights to copy,
23  modify and redistribute granted by the license, users are provided only
24  with a limited warranty and the software's author, the holder of the
25  economic rights, and the successive licensors have only limited
26  liability.
27 
28  In this respect, the user's attention is drawn to the risks associated
29  with loading, using, modifying and/or developing or reproducing the
30  software by the user in light of its specific status of free software,
31  that may mean that it is complicated to manipulate, and that also
32  therefore means that it is reserved for developers and experienced
33  professionals having in-depth computer knowledge. Users are therefore
34  encouraged to load and test the software's suitability as regards their
35  requirements in conditions enabling the security of their systems and/or
36  data to be ensured and, more generally, to use and operate it in the
37  same conditions as regards security.
38 
39  The fact that you are presently reading this means that you have had
40  knowledge of the CeCILL license and that you accept its terms.
41 */
42 
43 #ifndef BPP_TEXT_TEXTTOOLS_H
44 #define BPP_TEXT_TEXTTOOLS_H
45 
46 #include <iomanip>
47 #include <sstream>
48 #include <string>
49 #include <vector>
50 
51 
52 namespace bpp
53 {
55 namespace TextTools
56 {
63 bool isEmpty(const std::string& s);
64 
69 std::string toUpper(const std::string& s);
70 
75 std::string toLower(const std::string& s);
76 
81 bool isWhiteSpaceCharacter(char c);
82 
87 std::string removeWhiteSpaces(const std::string& s);
88 
93 std::string removeFirstWhiteSpaces(const std::string& s);
94 
99 std::string removeLastWhiteSpaces(const std::string& s);
100 
105 std::string removeSurroundingWhiteSpaces(const std::string& s);
106 
111 bool isNewLineCharacter(char c);
112 
117 std::string removeNewLines(const std::string& s);
118 
123 std::string removeLastNewLines(const std::string& s);
124 
129 bool isDecimalNumber(char c);
130 
138 bool isDecimalNumber(const std::string& s, char dec = '.', char scientificNotation = 'e');
139 
146 bool isDecimalInteger(const std::string& s, char scientificNotation = 'e');
147 
152 template<class T>
153 std::string toString(T t)
154 {
155  std::ostringstream oss;
156  oss << t;
157  return oss.str();
158 }
159 
165 template<class T>
166 std::string toString(T t, int precision)
167 {
168  std::ostringstream oss;
169  oss << std::setprecision(precision) << t;
170  return oss.str();
171 }
172 
177 template<class T>
178 T fromString(const std::string& s)
179 {
180  std::istringstream iss(s);
181  T obj;
182  iss >> obj;
183  return obj;
184 }
185 
192 int toInt(const std::string& s, char scientificNotation = 'e');
193 
201 double toDouble(const std::string& s, char dec = '.', char scientificNotation = 'e');
202 
207 template<class T>
208 T to(const std::string& s)
209 {
210  std::istringstream iss(s);
211  T t;
212  iss >> t;
213  return t;
214 }
215 
224 std::string resizeRight(const std::string& s, size_t newSize, char fill = ' ');
225 
233 std::string resizeLeft(const std::string& s, size_t newSize, char fill = ' ');
234 
241 std::vector<std::string> split(const std::string& s, size_t n);
242 
253 std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding);
254 
269 std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding,
270  std::vector<std::string>& exceptionsBeginning,
271  std::vector<std::string>& exceptionsEnding);
272 
280 std::string removeChar(const std::string& s, char c);
281 
289 std::size_t count(const std::string& s, const std::string& pattern);
290 
298 bool startsWith(const std::string& s, const std::string& pattern);
299 
307 bool endsWith(const std::string& s, const std::string& pattern);
308 
316 bool hasSubstring(const std::string& s, const std::string& pattern);
317 
325 void replaceAll(std::string& target, const std::string& query, const std::string& replacement);
326 } // namespace TextTools
327 } // namespace bpp
328 #endif // BPP_TEXT_TEXTTOOLS_H
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
Definition: TextTools.cpp:246
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
Definition: TextTools.cpp:95
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
Definition: TextTools.cpp:255
T to(const std::string &s)
Template to string conversion.
Definition: TextTools.h:208
std::string resizeLeft(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:282
std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
Definition: TextTools.cpp:128
std::string toUpper(const std::string &s)
Make the string uppercase.
Definition: TextTools.cpp:67
bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.
Definition: TextTools.cpp:91
bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
Definition: TextTools.cpp:458
std::string removeSubstrings(const std::string &s, char blockBeginning, char blockEnding)
Remove substrings from a string. All substrings beginning with blockBeginning and ending with blockEn...
Definition: TextTools.cpp:320
std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
Definition: TextTools.cpp:158
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces.
Definition: TextTools.cpp:58
std::vector< std::string > split(const std::string &s, std::size_t n)
Definition: TextTools.cpp:301
void replaceAll(std::string &target, const std::string &query, const std::string &replacement)
Replacement of all non-overlapping occurrences of a certain motif in a string.
Definition: TextTools.cpp:465
T fromString(const std::string &s)
General template method to convert from string.
Definition: TextTools.h:178
bool isDecimalInteger(const std::string &s, char scientificNotation)
Tell is a given character string describes a decimal integer. FIXME: for now, this parser will not re...
Definition: TextTools.cpp:211
bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
Definition: TextTools.cpp:440
std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
Definition: TextTools.cpp:417
bool endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
Definition: TextTools.cpp:449
std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
Definition: TextTools.cpp:146
std::string toLower(const std::string &s)
Make the string lowercase.
Definition: TextTools.cpp:79
bool isDecimalNumber(char c)
Tell is a given character describes a decimal number.
Definition: TextTools.cpp:169
std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
Definition: TextTools.cpp:107
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:264
std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
Definition: TextTools.cpp:117
bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
Definition: TextTools.cpp:142
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
Definition: TextTools.cpp:426