10 #include "../Exceptions.h" 11 #include "../Numeric/IntegerTools.h" 22 return std::all_of(s.begin(), s.end(), [](
char c) {
23 return std::isspace(c);
32 result.reserve(s.size());
33 std::transform(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
34 return std::toupper(c);
44 result.reserve(s.size());
45 std::transform(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
46 return std::tolower(c);
61 std::remove_copy_if(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
62 return std::isspace(c);
72 return std::string(std::find_if(s.begin(), s.end(), [](
char c) {
73 return !std::isspace(c);
82 auto lastNonWhitespace = std::find_if(s.rbegin(), s.rend(), [](
char c) {
83 return !std::isspace(c);
85 return std::string(s.begin(), lastNonWhitespace.base());
93 auto isNotWhitespace = [](
char c) {
94 return !std::isspace(c);
96 auto firstNonWhitespace = std::find_if(s.begin(), s.end(), isNotWhitespace);
97 auto lastNonWhitespace = std::find_if(
98 s.rbegin(), std::reverse_iterator<std::string::const_iterator>(firstNonWhitespace), isNotWhitespace);
99 return std::string(firstNonWhitespace, lastNonWhitespace.base());
112 std::remove_copy_if(s.begin(), s.end(), std::back_inserter(result), [](
char c) {
123 auto lastNonNewline = std::find_if(s.rbegin(), s.rend(), [](
char c) {
126 return std::string(s.begin(), lastNonNewline.base());
140 std::size_t sepCount = 0;
141 std::size_t sciCount = 0;
145 for ( ; i < s.size(); ++i)
150 else if (c == scientificNotation)
153 if (i == s.size() - 1)
156 if (c ==
'-' || c ==
'+')
158 if (i == s.size() - 1)
165 if (sepCount > 1 || sciCount > 1)
178 std::size_t sciCount = 0;
182 for ( ; i < s.size(); ++i)
185 if (c == scientificNotation)
188 if (i == s.size() - 1)
195 if (i == s.size() - 1)
208 int toInt(
const std::string& s,
char scientificNotation)
211 throw Exception(
"TextTools::toInt(). Invalid number specification: " + s);
212 return fromString<int>(s);
217 double toDouble(
const std::string& s,
char dec,
char scientificNotation)
220 throw Exception(
"TextTools::toDouble(). Invalid number specification: " + s);
221 return fromString<double>(s);
226 std::string
resizeRight(
const std::string& s, std::size_t newSize,
char fill)
229 result.reserve(newSize);
230 if (newSize > s.size())
232 std::copy(s.begin(), s.end(), std::back_inserter(result));
233 std::fill_n(std::back_inserter(result), newSize - s.size(), fill);
237 std::copy_n(s.begin(), newSize, std::back_inserter(result));
244 std::string
resizeLeft(
const std::string& s, std::size_t newSize,
char fill)
247 result.reserve(newSize);
248 if (newSize > s.size())
250 std::fill_n(std::back_inserter(result), newSize - s.size(), fill);
251 std::copy(s.begin(), s.end(), std::back_inserter(result));
255 using diff_type =
typename std::iterator_traits<decltype(s.begin())>::difference_type;
256 std::copy(s.begin() +
static_cast<diff_type
>(s.size() - newSize), s.end(), std::back_inserter(result));
263 std::vector<std::string>
split(
const std::string& s, std::size_t n)
265 using diff_type =
typename std::iterator_traits<decltype(s.begin())>::difference_type;
266 std::vector<std::string> v;
271 for (std::size_t i = 0; i < nbCopiedChunks; ++i)
273 v.emplace_back(s.begin() +
static_cast<diff_type
>(i * n), s.begin() +
static_cast<diff_type
>((i + 1) * n));
275 if (v.size() < nbChunks)
276 v.emplace_back(s.begin() +
static_cast<diff_type
>(v.size() * n), s.end());
285 std::size_t blockDepth = 0;
286 for (std::size_t i = 0; i < s.size(); ++i)
289 if (c == blockBeginning)
293 else if (c == blockEnding)
297 std::string(
"TextTools::removeSubstrings(): unmatched block closing character at position ") +
301 else if (blockDepth == 0)
314 std::vector<std::string>& exceptionsBeginning,
315 std::vector<std::string>& exceptionsEnding)
320 std::size_t begPos = 0;
321 for (std::size_t i = 0; i < s.size(); i++)
324 if (current == blockBeginning)
327 for (std::size_t j = 0; j < exceptionsBeginning.size(); j++)
329 std::size_t pos = exceptionsBeginning[j].find(blockBeginning);
330 if (pos != std::string::npos)
332 std::size_t left = i - pos;
333 std::size_t right = i + exceptionsBeginning[j].length() - pos;
334 if ((right < s.length() - 1) && (
hasSubstring(s.substr(left, right), exceptionsBeginning[j])))
344 t += s.substr(begPos, i - begPos);
347 else if ((current == blockEnding) && (blockCount > 0))
349 for (std::size_t j = 0; j < exceptionsEnding.size(); j++)
351 std::size_t pos = exceptionsEnding[j].find(blockEnding);
352 if (pos != std::string::npos)
354 std::size_t left = i - pos;
355 std::size_t right = i + exceptionsEnding[j].length() - pos;
356 if ((right < s.length() - 1) && (
hasSubstring(s.substr(left, right), exceptionsEnding[j])))
367 else if (blockCount < 0)
368 throw Exception(
"TextTools::removeSubstrings(). " +
369 std::string(
"Ending block character without corresponding beginning one at position ") +
373 t += s.substr(begPos);
382 std::remove_copy(s.begin(), s.end(), std::back_inserter(result), c);
388 std::size_t
count(
const std::string& s,
const std::string& pattern)
390 std::size_t
count = 0;
391 auto it = std::search(s.begin(), s.end(), pattern.begin(), pattern.end());
392 while (it != s.end())
395 it = std::search(it + 1, s.end(), pattern.begin(), pattern.end());
402 bool startsWith(
const std::string& s,
const std::string& pattern)
404 if (s.size() < pattern.size())
406 return std::equal(pattern.begin(), pattern.end(), s.begin());
411 bool endsWith(
const std::string& s,
const std::string& pattern)
413 if (s.size() < pattern.size())
415 return std::equal(pattern.rbegin(), pattern.rend(), s.rbegin());
422 return std::search(s.begin(), s.end(), pattern.begin(), pattern.end()) != s.end();
427 void replaceAll(std::string& target,
const std::string& query,
const std::string& replacement)
429 using diff_type =
typename std::iterator_traits<decltype(target.begin())>::difference_type;
433 auto it = target.begin();
434 while (it != target.end())
437 auto nextPattern = std::search(it, target.end(), query.begin(), query.end());
438 std::copy(it, nextPattern, std::back_inserter(result));
439 if (nextPattern != target.end())
441 result += replacement;
442 it = nextPattern +
static_cast<diff_type
>(query.size());
449 target = std::move(result);
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)
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 isDecimalNumber(char c)
Tell is a given character describes a decimal number.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
std::size_t count(const std::string &s, const std::string &pattern)
Count the occurences of a given pattern in a string.
std::string removeLastNewLines(const std::string &s)
Remove all new line characters at the end of a string.
std::string removeChar(const std::string &s, char c)
Remove all occurences of a character in a string.
std::string removeSurroundingWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning and the end of a string.
std::string toLower(const std::string &s)
Make the string lowercase.
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
std::string removeFirstWhiteSpaces(const std::string &s)
Remove all white spaces characters at the beginning of a string.
bool isNewLineCharacter(char c)
Tell if a character is a new line character or not.
std::string resizeLeft(const std::string &s, std::size_t newSize, char fill)
std::string removeNewLines(const std::string &s)
Remove all new line characters in a string.
std::string removeLastWhiteSpaces(const std::string &s)
Remove all white spaces characters at the end of a string.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
bool hasSubstring(const std::string &s, const std::string &pattern)
Tell is a string contains a certain motif.
bool isWhiteSpaceCharacter(char c)
Tell if a character is a white space or not.
std::string toUpper(const std::string &s)
Make the string uppercase.
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::string toString(T t)
General template method to convert to 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 endsWith(const std::string &s, const std::string &pattern)
Tell is a string ends with a certain motif.
bool startsWith(const std::string &s, const std::string &pattern)
Tell is a string begins with 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::vector< std::string > split(const std::string &s, std::size_t n)