bpp-core3  3.0.0
TextTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_TEXT_TEXTTOOLS_H
6 #define BPP_TEXT_TEXTTOOLS_H
7 
8 #include <iomanip>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
13 
14 namespace bpp
15 {
17 namespace TextTools
18 {
25 bool isEmpty(const std::string& s);
26 
31 std::string toUpper(const std::string& s);
32 
37 std::string toLower(const std::string& s);
38 
43 bool isWhiteSpaceCharacter(char c);
44 
49 std::string removeWhiteSpaces(const std::string& s);
50 
55 std::string removeFirstWhiteSpaces(const std::string& s);
56 
61 std::string removeLastWhiteSpaces(const std::string& s);
62 
67 std::string removeSurroundingWhiteSpaces(const std::string& s);
68 
73 bool isNewLineCharacter(char c);
74 
79 std::string removeNewLines(const std::string& s);
80 
85 std::string removeLastNewLines(const std::string& s);
86 
91 bool isDecimalNumber(char c);
92 
100 bool isDecimalNumber(const std::string& s, char dec = '.', char scientificNotation = 'e');
101 
108 bool isDecimalInteger(const std::string& s, char scientificNotation = 'e');
109 
114 template<class T>
115 std::string toString(T t)
116 {
117  std::ostringstream oss;
118  oss << t;
119  return oss.str();
120 }
121 
127 template<class T>
128 std::string toString(T t, int precision)
129 {
130  std::ostringstream oss;
131  oss << std::setprecision(precision) << t;
132  return oss.str();
133 }
134 
139 template<class T>
140 T fromString(const std::string& s)
141 {
142  std::istringstream iss(s);
143  T obj;
144  iss >> obj;
145  return obj;
146 }
147 
154 int toInt(const std::string& s, char scientificNotation = 'e');
155 
163 double toDouble(const std::string& s, char dec = '.', char scientificNotation = 'e');
164 
169 template<class T>
170 T to(const std::string& s)
171 {
172  std::istringstream iss(s);
173  T t;
174  iss >> t;
175  return t;
176 }
177 
186 std::string resizeRight(const std::string& s, size_t newSize, char fill = ' ');
187 
195 std::string resizeLeft(const std::string& s, size_t newSize, char fill = ' ');
196 
203 std::vector<std::string> split(const std::string& s, size_t n);
204 
215 std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding);
216 
231 std::string removeSubstrings(const std::string& s, char blockBeginning, char blockEnding,
232  std::vector<std::string>& exceptionsBeginning,
233  std::vector<std::string>& exceptionsEnding);
234 
242 std::string removeChar(const std::string& s, char c);
243 
251 std::size_t count(const std::string& s, const std::string& pattern);
252 
260 bool startsWith(const std::string& s, const std::string& pattern);
261 
269 bool endsWith(const std::string& s, const std::string& pattern);
270 
278 bool hasSubstring(const std::string& s, const std::string& pattern);
279 
287 void replaceAll(std::string& target, const std::string& query, const std::string& replacement);
288 } // namespace TextTools
289 } // namespace bpp
290 #endif // BPP_TEXT_TEXTTOOLS_H
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:226
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:427
bool isDecimalNumber(char c)
Tell is a given character describes a decimal number.
Definition: TextTools.cpp:131
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
Definition: TextTools.cpp:217
T to(const std::string &s)
Template to string conversion.
Definition: TextTools.h:170
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:388
std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
Definition: TextTools.cpp:120
std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
Definition: TextTools.cpp:379
std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
Definition: TextTools.cpp:90
std::string toLower(const std::string &s)
Make the string lowercase.
Definition: TextTools.cpp:41
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
Definition: TextTools.cpp:57
T fromString(const std::string &s)
General template method to convert from string.
Definition: TextTools.h:140
std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
Definition: TextTools.cpp:69
bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
Definition: TextTools.cpp:104
std::string resizeLeft(const std::string &s, std::size_t newSize, char fill)
Definition: TextTools.cpp:244
std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
Definition: TextTools.cpp:108
std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
Definition: TextTools.cpp:79
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
Definition: TextTools.cpp:208
bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
Definition: TextTools.cpp:420
bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.
Definition: TextTools.cpp:53
std::string toUpper(const std::string &s)
Make the string uppercase.
Definition: TextTools.cpp:29
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be &#39;empty&#39; if it is only made of white spaces...
Definition: TextTools.cpp:20
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:115
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:173
bool endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
Definition: TextTools.cpp:411
bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
Definition: TextTools.cpp:402
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:282
std::vector< std::string > split(const std::string &s, std::size_t n)
Definition: TextTools.cpp:263