48 #include "../Exceptions.h"
49 #include "../Numeric/IntegerTools.h"
60 return std::all_of(s.begin(), s.end(), [](
char c) {
61 return std::isspace(c);
70 result.reserve(s.size());
71 std::transform(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
72 return std::toupper(c);
82 result.reserve(s.size());
83 std::transform(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
84 return std::tolower(c);
99 std::remove_copy_if(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
100 return std::isspace(c);
110 return std::string(std::find_if(s.begin(), s.end(), [](
char c) {
111 return !std::isspace(c);
120 auto lastNonWhitespace = std::find_if(s.rbegin(), s.rend(), [](
char c) {
121 return !std::isspace(c);
123 return std::string(s.begin(), lastNonWhitespace.base());
131 auto isNotWhitespace = [](
char c) {
132 return !std::isspace(c);
134 auto firstNonWhitespace = std::find_if(s.begin(), s.end(), isNotWhitespace);
135 auto lastNonWhitespace = std::find_if(
136 s.rbegin(), std::reverse_iterator<std::string::const_iterator>(firstNonWhitespace), isNotWhitespace);
137 return std::string(firstNonWhitespace, lastNonWhitespace.base());
150 std::remove_copy_if(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
151 return isNewLineCharacter(c);
161 auto lastNonNewline = std::find_if(s.rbegin(), s.rend(), [](
char c) {
162 return !isNewLineCharacter(c);
164 return std::string(s.begin(), lastNonNewline.base());
178 std::size_t sepCount = 0;
179 std::size_t sciCount = 0;
183 for ( ; i < s.size(); ++i)
188 else if (c == scientificNotation)
191 if (i == s.size() - 1)
194 if (c ==
'-' || c ==
'+')
196 if (i == s.size() - 1)
203 if (sepCount > 1 || sciCount > 1)
216 std::size_t sciCount = 0;
220 for ( ; i < s.size(); ++i)
223 if (c == scientificNotation)
226 if (i == s.size() - 1)
233 if (i == s.size() - 1)
246 int toInt(
const std::string& s,
char scientificNotation)
249 throw Exception(
"TextTools::toInt(). Invalid number specification: " + s);
250 return fromString<int>(s);
255 double toDouble(
const std::string& s,
char dec,
char scientificNotation)
258 throw Exception(
"TextTools::toDouble(). Invalid number specification: " + s);
259 return fromString<double>(s);
264 std::string
resizeRight(
const std::string& s, std::size_t newSize,
char fill)
267 result.reserve(newSize);
268 if (newSize > s.size())
270 std::copy(s.begin(), s.end(), std::back_inserter(result));
271 std::fill_n(std::back_inserter(result), newSize - s.size(), fill);
275 std::copy_n(s.begin(), newSize, std::back_inserter(result));
282 std::string
resizeLeft(
const std::string& s, std::size_t newSize,
char fill)
285 result.reserve(newSize);
286 if (newSize > s.size())
288 std::fill_n(std::back_inserter(result), newSize - s.size(), fill);
289 std::copy(s.begin(), s.end(), std::back_inserter(result));
293 using diff_type =
typename std::iterator_traits<decltype(s.begin())>::difference_type;
294 std::copy(s.begin() +
static_cast<diff_type
>(s.size() - newSize), s.end(), std::back_inserter(result));
301 std::vector<std::string>
split(
const std::string& s, std::size_t n)
303 using diff_type =
typename std::iterator_traits<decltype(s.begin())>::difference_type;
304 std::vector<std::string> v;
309 for (std::size_t i = 0; i < nbCopiedChunks; ++i)
311 v.emplace_back(s.begin() +
static_cast<diff_type
>(i * n), s.begin() +
static_cast<diff_type
>((i + 1) * n));
313 if (v.size() < nbChunks)
314 v.emplace_back(s.begin() +
static_cast<diff_type
>(v.size() * n), s.end());
323 std::size_t blockDepth = 0;
324 for (std::size_t i = 0; i < s.size(); ++i)
327 if (c == blockBeginning)
331 else if (c == blockEnding)
335 std::string(
"TextTools::removeSubstrings(): unmatched block closing character at position ") +
339 else if (blockDepth == 0)
352 std::vector<std::string>& exceptionsBeginning,
353 std::vector<std::string>& exceptionsEnding)
358 std::size_t begPos = 0;
359 for (std::size_t i = 0; i < s.size(); i++)
362 if (current == blockBeginning)
365 for (std::size_t j = 0; j < exceptionsBeginning.size(); j++)
367 std::size_t pos = exceptionsBeginning[j].find(blockBeginning);
368 if (pos != std::string::npos)
370 std::size_t left = i - pos;
371 std::size_t right = i + exceptionsBeginning[j].length() - pos;
372 if ((right < s.length() - 1) && (
hasSubstring(s.substr(left, right), exceptionsBeginning[j])))
382 t += s.substr(begPos, i - begPos);
385 else if ((current == blockEnding) && (blockCount > 0))
387 for (std::size_t j = 0; j < exceptionsEnding.size(); j++)
389 std::size_t pos = exceptionsEnding[j].find(blockEnding);
390 if (pos != std::string::npos)
392 std::size_t left = i - pos;
393 std::size_t right = i + exceptionsEnding[j].length() - pos;
394 if ((right < s.length() - 1) && (
hasSubstring(s.substr(left, right), exceptionsEnding[j])))
405 else if (blockCount < 0)
406 throw Exception(
"TextTools::removeSubstrings(). " +
407 std::string(
"Ending block character without corresponding beginning one at position ") +
411 t += s.substr(begPos);
420 std::remove_copy(s.begin(), s.end(), std::back_inserter(result), c);
426 std::size_t
count(
const std::string& s,
const std::string& pattern)
428 std::size_t
count = 0;
429 auto it = std::search(s.begin(), s.end(), pattern.begin(), pattern.end());
430 while (it != s.end())
433 it = std::search(it + 1, s.end(), pattern.begin(), pattern.end());
440 bool startsWith(
const std::string& s,
const std::string& pattern)
442 if (s.size() < pattern.size())
444 return std::equal(pattern.begin(), pattern.end(), s.begin());
449 bool endsWith(
const std::string& s,
const std::string& pattern)
451 if (s.size() < pattern.size())
453 return std::equal(pattern.rbegin(), pattern.rend(), s.rbegin());
460 return std::search(s.begin(), s.end(), pattern.begin(), pattern.end()) != s.end();
465 void replaceAll(std::string& target,
const std::string& query,
const std::string& replacement)
467 using diff_type =
typename std::iterator_traits<decltype(target.begin())>::difference_type;
471 auto it = target.begin();
472 while (it != target.end())
475 auto nextPattern = std::search(it, target.end(), query.begin(), query.end());
476 std::copy(it, nextPattern, std::back_inserter(result));
477 if (nextPattern != target.end())
479 result += replacement;
480 it = nextPattern +
static_cast<diff_type
>(query.size());
487 target = std::move(result);
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
std::string resizeLeft(const std::string &s, std::size_t newSize, char fill)
std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
std::string toUpper(const std::string &s)
Make the string uppercase.
bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.
bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
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...
std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
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.
std::vector< std::string > split(const std::string &s, std::size_t n)
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.
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...
bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with a certain motif.
std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
bool endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
std::string toLower(const std::string &s)
Make the string lowercase.
bool isDecimalNumber(char c)
Tell is a given character describes a decimal number.
std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
std::string toString(T t)
General template method to convert to a string.
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.