15#include <OpenMS/OpenMSConfig.h>
53 static float toFloat(
const std::string& s);
59 static bool extractDouble(
const char*& begin,
const char* end,
double& target);
63 static bool extractInt(
const char*& begin,
const char* end,
int& target);
78 OPENMS_DLLAPI
void appendToStr(
unsigned int i, std::string& target);
79 OPENMS_DLLAPI
void appendToStr(
short int i, std::string& target);
80 OPENMS_DLLAPI
void appendToStr(
short unsigned int i, std::string& target);
81 OPENMS_DLLAPI
void appendToStr(
long int i, std::string& target);
82 OPENMS_DLLAPI
void appendToStr(
long unsigned int i, std::string& target);
83 OPENMS_DLLAPI
void appendToStr(
long long unsigned int i, std::string& target);
84 OPENMS_DLLAPI
void appendToStr(
long long signed int i, std::string& target);
88 OPENMS_DLLAPI
void appendToStr(
double d, std::string& target);
90 OPENMS_DLLAPI
void appendToStr(
long double ld, std::string& target);
103 inline std::string
toStr(
unsigned int i)
105 inline std::string
toStr(
short int i)
107 inline std::string
toStr(
short unsigned int i)
109 inline std::string
toStr(
long int i)
111 inline std::string
toStr(
long unsigned int i)
113 inline std::string
toStr(
long long unsigned int i)
115 inline std::string
toStr(
long long signed int i)
119 OPENMS_DLLAPI std::string
toStr(
float f,
bool full_precision =
true);
121 OPENMS_DLLAPI std::string
toStr(
double d,
bool full_precision =
true);
123 OPENMS_DLLAPI std::string
toStr(
long double ld,
bool full_precision =
true);
126 {
return std::string(1, c); }
128 inline std::string
toStr(
const std::string& s)
131 inline std::string
toStr(
const char* s)
132 {
return std::string(s); }
161 inline std::string
toStr(std::string_view sv) {
return std::string(sv); }
173 inline bool extractDouble(
const char*& begin,
const char* end,
double& target)
176 inline bool extractInt(
const char*& begin,
const char* end,
int& target)
180 inline bool extractDouble(std::string::const_iterator& begin,
const std::string::const_iterator& end,
double& target)
182 const char*
const p_start = &(*begin);
183 const char* p = p_start;
184 const char* e = &(*end);
186 begin += (p - p_start);
190 inline bool extractDouble(std::string::iterator& begin,
const std::string::iterator& end,
double& target)
192 const char*
const p_start = &(*begin);
193 const char* p = p_start;
194 const char* e = &(*end);
196 begin += (p - p_start);
200 inline bool extractInt(std::string::const_iterator& begin,
const std::string::const_iterator& end,
int& target)
202 const char*
const p_start = &(*begin);
203 const char* p = p_start;
204 const char* e = &(*end);
206 begin += (p - p_start);
210 inline bool extractInt(std::string::iterator& begin,
const std::string::iterator& end,
int& target)
212 const char*
const p_start = &(*begin);
213 const char* p = p_start;
214 const char* e = &(*end);
216 begin += (p - p_start);
231 auto pos =
skipWhitespace(data.data(), data.data() + data.size());
232 return static_cast<int>(pos - data.data());
242 return static_cast<int>(pos - data.data());
251 [[maybe_unused]] OPENMS_DLLAPI std::string
number(
double d,
UInt n);
266 if (
prefix.size() > s.size())
return false;
267 if (
prefix.empty())
return true;
271 {
return !s.empty() && s.front() == c; }
273 inline bool hasSuffix(
const std::string& s,
const std::string& sfx)
275 if (sfx.size() > s.size())
return false;
276 if (sfx.empty())
return true;
277 return s.compare(s.size() - sfx.size(), sfx.size(), sfx) == 0;
280 {
return !s.empty() && s.back() == c; }
284 return s.find(sub) != std::string::npos;
287 {
return s.find(c) != std::string::npos; }
289 inline bool has(
const std::string& s,
Byte byte)
291 return s.find(
static_cast<char>(
byte)) != std::string::npos;
293 inline bool has(
const std::string& s,
char c)
294 {
return s.find(c) != std::string::npos; }
301 inline std::string
prefix(
const std::string& s,
size_t length)
303 if (length > s.size())
305 return s.substr(0, length);
308 inline std::string
suffix(
const std::string& s,
size_t length)
310 if (length > s.size())
312 return s.substr(s.size() - length, length);
315 inline std::string
prefix(
const std::string& s,
Int length)
319 return prefix(s,
static_cast<size_t>(length));
322 inline std::string
suffix(
const std::string& s,
Int length)
326 return suffix(s,
static_cast<size_t>(length));
334 inline std::string
prefix(
const std::string& s,
char delim)
336 return s.substr(0, s.find(delim));
344 inline std::string
suffix(
const std::string& s,
char delim)
346 size_t pos = s.rfind(delim);
347 return pos == std::string::npos ? s : s.substr(pos + 1);
351 inline std::string
substr(
const std::string& s,
size_t pos = 0,
size_t n = std::string::npos)
353 size_t begin = std::min(pos, s.size());
354 return s.substr(begin, n);
357 inline std::string
substr(std::string_view s,
size_t pos = 0,
size_t n = std::string::npos)
359 size_t begin = std::min(pos, s.size());
360 return std::string(s.substr(begin, n));
366 inline bool hasSuffix(std::string_view s,
const std::string& sfx)
367 {
return s.size() >= sfx.size() && s.substr(s.size() - sfx.size()) == sfx; }
369 {
return s.find(sub) != std::string_view::npos; }
372 inline std::string
chop(
const std::string& s,
Size n)
374 size_t end = (n < s.size()) ? s.size() - n : 0;
375 return std::string(s.begin(), s.begin() + end);
383 inline std::string&
trim(std::string& s)
385 auto begin = s.begin();
386 while (begin != s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
388 if (begin == s.end()) { s.clear();
return s; }
389 auto end = s.end() - 1;
390 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
393 if (begin != s.begin() || end != s.end())
394 s.assign(begin, end);
399 inline std::string
trimmed(std::string s) {
return std::move(
trim(s)); }
403 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::toupper(c); });
409 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::tolower(c); });
419 if (!s.empty()) s[0] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(s[0])));
425 std::reverse(s.begin(), s.end());
432 result.reserve(s.size());
433 bool last_ws =
false;
436 if (c ==
' ' || c ==
'\n' || c ==
'\t' || c ==
'\r')
438 if (!last_ws) result +=
' ';
454 s.insert(s.begin(), size - s.size(), c);
461 s.append(size - s.size(), c);
465 inline std::string&
substitute(std::string& s,
char from,
char to)
467 std::replace(s.begin(), s.end(), from, to);
472 inline std::string&
substitute(std::string& s,
const std::string& from,
const std::string& to)
474 if (from.empty())
return s;
476 result.reserve(s.size());
479 while ((pos = s.find(from, start)) != std::string::npos)
481 result.append(s, start, pos - start);
483 start = pos + from.size();
485 result.append(s, start, std::string::npos);
491 inline std::string
substituted(std::string s,
const std::string& from,
const std::string& to) {
return std::move(
substitute(s, from, to)); }
493 inline std::string&
remove(std::string& s,
char what)
495 s.erase(std::remove(s.begin(), s.end(), what), s.end());
505 if (s.empty() || s.back() != end) s.push_back(end);
515 auto it = s.cbegin() + start;
516 auto dest = s.begin() + start;
517 auto it_end = s.cend();
518 bool has_spaces =
false;
522 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
528 if (has_spaces) *dest = *it;
532 if (has_spaces) s.resize(
static_cast<size_t>(dest - s.begin()));
538 return s.size() >= 2 && s.front() == q && s.back() == q;
545 substitute(s, std::string(
"\\"), std::string(
"\\\\"));
546 substitute(s, std::string(1, q), std::string(
"\\") + q);
550 substitute(s, std::string(1, q), std::string(2, q));
552 s.insert(s.begin(), q);
561 "'" + s +
"' does not have the expected format of a quoted string");
566 substitute(s, std::string(
"\\") + q, std::string(1, q));
567 substitute(s, std::string(
"\\\\"), std::string(
"\\"));
571 substitute(s, std::string(2, q), std::string(1, q));
584 inline bool split(
const std::string& s,
char splitter,
585 std::vector<std::string>& substrings,
586 bool quote_protect =
false)
589 if (s.empty())
return false;
591 size_t nsplits =
static_cast<size_t>(std::count(s.begin(), s.end(), splitter));
593 if (!quote_protect && nsplits == 0)
595 substrings.push_back(s);
599 substrings.reserve(nsplits + 1);
604 auto begin = s.cbegin();
605 auto end = s.cbegin();
606 for (; end != s.cend(); ++end)
608 if (*end ==
'"') ++quote_count;
609 if ((quote_count % 2 == 0) && (*end == splitter))
611 std::string block(begin, end);
613 bool has_start = block.size() >= 1 && block.front() ==
'"';
614 bool has_end = block.size() >= 1 && block.back() ==
'"';
615 if (block.size() >= 2 && (has_start ^ has_end))
617 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
618 if (block.size() >= 2 && has_start && has_end)
619 block = block.substr(1, block.size() - 2);
620 substrings.push_back(std::move(block));
624 if (substrings.empty()) { substrings.push_back(s);
return false; }
625 std::string block(begin, end);
627 bool has_start = block.size() >= 1 && block.front() ==
'"';
628 bool has_end = block.size() >= 1 && block.back() ==
'"';
629 if (block.size() >= 2 && (has_start ^ has_end))
631 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
632 if (block.size() >= 2 && has_start && has_end)
633 block = block.substr(1, block.size() - 2);
634 substrings.push_back(std::move(block));
638 auto begin = s.cbegin();
639 for (
auto it = s.cbegin(); it != s.cend(); ++it)
643 substrings.emplace_back(begin, it);
647 substrings.emplace_back(begin, s.cend());
654 inline bool split(
const std::string& s,
const std::string& splitter,
655 std::vector<std::string>& substrings)
658 if (s.empty())
return false;
659 if (splitter.empty())
661 substrings.resize(s.size());
662 for (
size_t i = 0; i < s.size(); ++i) substrings[i] = std::string(1, s[i]);
665 size_t len = splitter.size();
667 size_t pos = s.find(splitter);
668 while (pos != std::string::npos)
670 substrings.push_back(s.substr(start, pos - start));
672 pos = s.find(splitter, start);
674 substrings.push_back(s.substr(start));
675 return substrings.size() > 1;
680 inline bool split_quoted(
const std::string& s,
const std::string& splitter,
681 std::vector<std::string>& substrings,
685 if (s.empty() || splitter.empty())
return false;
687 bool in_quote =
false;
688 char targets[2] = {q, splitter[0]};
689 std::string rest = splitter.substr(1);
692 for (
size_t i = 0; i < s.size(); ++i)
696 bool embedded =
false;
699 for (; i < s.size(); ++i)
701 if (s[i] ==
'\\') embedded = !embedded;
702 else if (s[i] == q && !embedded)
break;
703 else embedded =
false;
708 for (; i < s.size(); ++i)
713 if (i + 1 < s.size() && s[i + 1] == q) embedded = !embedded;
714 else if (!embedded)
break;
715 else embedded =
false;
723 i = s.find_first_of(targets, i, 2);
724 if (i == std::string::npos)
break;
725 if (s[i] == q) { in_quote =
true; }
726 else if (s.compare(i + 1, rest.size(), rest) == 0)
728 substrings.push_back(s.substr(start, i - start));
729 start = i + splitter.size();
736 "unbalanced quotation marks in string '" + s +
"'");
737 substrings.push_back(s.substr(start));
738 return substrings.size() > 1;
742 template <
class StringIterator>
744 StringIterator first, StringIterator last,
745 const std::string& glue =
"")
747 if (first == last) { target.clear();
return; }
749 for (
auto it = ++first; it != last; ++it)
757 template <
class StringIterator>
758 inline std::string
concatenate(StringIterator first, StringIterator last,
759 const std::string& glue =
"")
777 template <std::ranges::range Container>
778 inline std::string
concatenate(
const Container& container,
const std::string& glue =
"")
780 return concatenate(container.begin(), container.end(), glue);
790 namespace StringConversions
792 inline void append(
int i, std::string& target)
794 inline void append(
unsigned int i, std::string& target)
796 inline void append(
short int i, std::string& target)
798 inline void append(
short unsigned int i, std::string& target)
800 inline void append(
long int i, std::string& target)
802 inline void append(
long unsigned int i, std::string& target)
804 inline void append(
long long unsigned int i, std::string& target)
806 inline void append(
long long signed int i, std::string& target)
808 inline void append(
float f, std::string& target)
810 inline void append(
double d, std::string& target)
812 inline void append(
long double ld, std::string& target)
823 template <
typename T>
826 inline std::string
toString(
float f,
bool fp =
true)
828 inline std::string
toString(
double d,
bool fp =
true)
830 inline std::string
toString(
long double ld,
bool fp =
true)
855inline std::string&
operator+=(std::string& s,
unsigned int i)
859inline std::string&
operator+=(std::string& s,
short unsigned int i)
863inline std::string&
operator+=(std::string& s,
long unsigned int i)
865inline std::string&
operator+=(std::string& s,
long long unsigned int i)
867inline std::string&
operator+=(std::string& s,
long long signed int i)
873inline std::string&
operator+=(std::string& s,
long double ld)
878{
return std::move(s += i); }
879inline std::string
operator+(std::string s,
unsigned int i)
880{
return std::move(s += i); }
882{
return std::move(s += i); }
883inline std::string
operator+(std::string s,
short unsigned int i)
884{
return std::move(s += i); }
886{
return std::move(s += i); }
887inline std::string
operator+(std::string s,
long unsigned int i)
888{
return std::move(s += i); }
889inline std::string
operator+(std::string s,
long long unsigned int i)
890{
return std::move(s += i); }
891inline std::string
operator+(std::string s,
long long signed int i)
892{
return std::move(s += i); }
894{
return std::move(s += f); }
896{
return std::move(s += d); }
897inline std::string
operator+(std::string s,
long double ld)
898{
return std::move(s += ld); }
std::string & operator+=(std::string &s, int i)
Definition StringUtils.h:853
std::string operator+(std::string s, int i)
Definition StringUtils.h:877
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
Invalid conversion exception.
Definition Exception.h:331
Int overflow exception.
Definition Exception.h:211
Int underflow exception.
Definition Exception.h:175
Class to hold strings, numeric values, vectors of strings and vectors of numeric values using the stl...
Definition ParamValue.h:31
Definition StringUtils.h:46
static double toDouble(const std::string &s)
Parse double from string (leading/trailing whitespace allowed)
static Int64 toInt64(const std::string &s)
Parse int64 from string (leading/trailing whitespace allowed)
static bool extractDouble(const char *&begin, const char *end, double &target)
static Int32 toInt32(const std::string &s)
Parse int32 from string (leading/trailing whitespace allowed)
static float toFloat(const std::string &s)
Parse float from string (leading/trailing whitespace allowed)
static bool extractInt(const char *&begin, const char *end, int &target)
int32_t Int32
Signed integer type (32bit)
Definition Types.h:26
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
uint8_t Byte
Byte type.
Definition Types.h:81
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
void append(int i, std::string &target)
Definition StringUtils.h:792
void appendLowP(float f, std::string &target)
Definition StringUtils.h:814
std::string toStringLowP(float f)
Definition StringUtils.h:837
std::string toString()
Definition StringUtils.h:834
float toFloat(const std::string &s)
Definition StringUtils.h:170
void appendToStr(int i, std::string &target)
std::string & fillRight(std::string &s, char c, UInt size)
Definition StringUtils.h:458
Int64 toInt64(const std::string &s)
Definition StringUtils.h:169
std::string substituted(std::string s, char from, char to)
substitute on a copy (for chained/rvalue expressions)
Definition StringUtils.h:490
bool hasPrefix(const std::string &s, const std::string &prefix)
Definition StringUtils.h:264
std::string & toUpper(std::string &s)
Definition StringUtils.h:401
std::string chop(const std::string &s, Size n)
Remove n characters from the end; returns empty string if n >= size.
Definition StringUtils.h:372
std::string & trim(std::string &s)
Definition StringUtils.h:383
std::string & unquote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:557
bool hasSuffix(const std::string &s, const std::string &sfx)
Definition StringUtils.h:273
bool extractDouble(const char *&begin, const char *end, double &target)
Definition StringUtils.h:173
std::string & fillLeft(std::string &s, char c, UInt size)
Definition StringUtils.h:451
std::string & firstToUpper(std::string &s)
Definition StringUtils.h:417
void appendToStrLowP(float f, std::string &target)
std::string suffix(const std::string &s, size_t length)
Definition StringUtils.h:308
std::string substr(const std::string &s, size_t pos=0, size_t n=std::string::npos)
Wrapper around std::string::substr; clamps pos to [0, size].
Definition StringUtils.h:351
bool extractInt(const char *&begin, const char *end, int &target)
Definition StringUtils.h:176
std::string random(UInt length)
Returns a random string of length characters from [0-9a-zA-Z].
bool isQuoted(const std::string &s, char q)
Definition StringUtils.h:536
std::string toLowered(std::string s)
Returns a lower-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:415
std::string & removeWhitespaces(std::string &s)
Definition StringUtils.h:511
std::string numberLength(double d, UInt n)
Returns a string for d with at most n characters total (scientific notation if needed)
std::string number(double d, UInt n)
Returns a string with exactly n decimal places for d.
std::string prefix(const std::string &s, size_t length)
Definition StringUtils.h:301
const char * skipNonWhitespace(const char *p, const char *p_end)
Returns pointer to first whitespace character in [p, p_end), or p_end.
bool hasSubstring(const std::string &s, const std::string &sub)
Definition StringUtils.h:282
const char * skipWhitespace(const char *p, const char *p_end)
Returns pointer to first non-whitespace character in [p, p_end), or p_end.
std::string trimmed(std::string s)
Returns a trimmed copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:399
std::string toStr(int i)
Definition StringUtils.h:101
bool split(const std::string &s, char splitter, std::vector< std::string > &substrings, bool quote_protect=false)
Definition StringUtils.h:584
std::string & ensureLastChar(std::string &s, char end)
Definition StringUtils.h:503
void concatenate(std::string &target, StringIterator first, StringIterator last, const std::string &glue="")
Join elements [first, last) with glue between them, storing result in target.
Definition StringUtils.h:743
std::string & simplify(std::string &s)
Definition StringUtils.h:429
std::string & reverse(std::string &s)
Definition StringUtils.h:423
bool has(const std::string &s, Byte byte)
Definition StringUtils.h:289
double toDouble(const std::string &s)
Definition StringUtils.h:171
std::string & toLower(std::string &s)
Definition StringUtils.h:407
std::string & remove(std::string &s, char what)
Definition StringUtils.h:493
std::string toUppered(std::string s)
Returns an upper-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:413
Int32 toInt32(const std::string &s)
Definition StringUtils.h:168
bool split_quoted(const std::string &s, const std::string &splitter, std::vector< std::string > &substrings, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:680
std::string & quote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:541
std::string & substitute(std::string &s, char from, char to)
Definition StringUtils.h:465
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition StringUtils.h:39