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 if (begin == end)
return false;
183 const char*
const p_start = &(*begin);
184 const char* p = p_start;
187 const char*
const e = p_start + (end - begin);
189 begin += (p - p_start);
193 inline bool extractDouble(std::string::iterator& begin,
const std::string::iterator& end,
double& target)
195 if (begin == end)
return false;
196 const char*
const p_start = &(*begin);
197 const char* p = p_start;
200 const char*
const e = p_start + (end - begin);
202 begin += (p - p_start);
206 inline bool extractInt(std::string::const_iterator& begin,
const std::string::const_iterator& end,
int& target)
208 if (begin == end)
return false;
209 const char*
const p_start = &(*begin);
210 const char* p = p_start;
213 const char*
const e = p_start + (end - begin);
215 begin += (p - p_start);
219 inline bool extractInt(std::string::iterator& begin,
const std::string::iterator& end,
int& target)
221 if (begin == end)
return false;
222 const char*
const p_start = &(*begin);
223 const char* p = p_start;
226 const char*
const e = p_start + (end - begin);
228 begin += (p - p_start);
243 auto pos =
skipWhitespace(data.data(), data.data() + data.size());
244 return static_cast<size_t>(pos - data.data());
254 return static_cast<size_t>(pos - data.data());
263 [[maybe_unused]] OPENMS_DLLAPI std::string
number(
double d,
UInt n);
278 if (
prefix.size() > s.size())
return false;
279 if (
prefix.empty())
return true;
283 {
return !s.empty() && s.front() == c; }
285 inline bool hasSuffix(
const std::string& s,
const std::string& sfx)
287 if (sfx.size() > s.size())
return false;
288 if (sfx.empty())
return true;
289 return s.compare(s.size() - sfx.size(), sfx.size(), sfx) == 0;
292 {
return !s.empty() && s.back() == c; }
296 return s.find(sub) != std::string::npos;
299 {
return s.find(c) != std::string::npos; }
301 inline bool has(
const std::string& s,
Byte byte)
303 return s.find(
static_cast<char>(
byte)) != std::string::npos;
305 inline bool has(
const std::string& s,
char c)
306 {
return s.find(c) != std::string::npos; }
313 inline std::string
prefix(
const std::string& s,
size_t length)
315 if (length > s.size())
317 return s.substr(0, length);
320 inline std::string
suffix(
const std::string& s,
size_t length)
322 if (length > s.size())
324 return s.substr(s.size() - length, length);
327 inline std::string
prefix(
const std::string& s,
Int length)
331 return prefix(s,
static_cast<size_t>(length));
334 inline std::string
suffix(
const std::string& s,
Int length)
338 return suffix(s,
static_cast<size_t>(length));
346 inline std::string
prefix(
const std::string& s,
char delim)
348 return s.substr(0, s.find(delim));
356 inline std::string
suffix(
const std::string& s,
char delim)
358 size_t pos = s.rfind(delim);
359 return pos == std::string::npos ? s : s.substr(pos + 1);
363 inline std::string
substr(
const std::string& s,
size_t pos = 0,
size_t n = std::string::npos)
365 size_t begin = std::min(pos, s.size());
366 return s.substr(begin, n);
369 inline std::string
substr(std::string_view s,
size_t pos = 0,
size_t n = std::string::npos)
371 size_t begin = std::min(pos, s.size());
372 return std::string(s.substr(begin, n));
378 inline bool hasSuffix(std::string_view s,
const std::string& sfx)
379 {
return s.size() >= sfx.size() && s.substr(s.size() - sfx.size()) == sfx; }
381 {
return s.find(sub) != std::string_view::npos; }
384 inline std::string
chop(
const std::string& s,
Size n)
386 size_t end = (n < s.size()) ? s.size() - n : 0;
387 return std::string(s.begin(), s.begin() + end);
395 inline std::string&
trim(std::string& s)
397 auto begin = s.begin();
398 while (begin != s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
400 if (begin == s.end()) { s.clear();
return s; }
401 auto end = s.end() - 1;
402 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
405 if (begin != s.begin() || end != s.end())
406 s.assign(begin, end);
411 inline std::string
trimmed(std::string s) {
return std::move(
trim(s)); }
415 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::toupper(c); });
421 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::tolower(c); });
431 if (!s.empty()) s[0] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(s[0])));
437 std::reverse(s.begin(), s.end());
444 result.reserve(s.size());
445 bool last_ws =
false;
448 if (c ==
' ' || c ==
'\n' || c ==
'\t' || c ==
'\r')
450 if (!last_ws) result +=
' ';
466 s.insert(s.begin(), size - s.size(), c);
473 s.append(size - s.size(), c);
477 inline std::string&
substitute(std::string& s,
char from,
char to)
479 std::replace(s.begin(), s.end(), from, to);
484 inline std::string&
substitute(std::string& s,
const std::string& from,
const std::string& to)
486 if (from.empty())
return s;
488 result.reserve(s.size());
491 while ((pos = s.find(from, start)) != std::string::npos)
493 result.append(s, start, pos - start);
495 start = pos + from.size();
497 result.append(s, start, std::string::npos);
503 inline std::string
substituted(std::string s,
const std::string& from,
const std::string& to) {
return std::move(
substitute(s, from, to)); }
505 inline std::string&
remove(std::string& s,
char what)
507 s.erase(std::remove(s.begin(), s.end(), what), s.end());
517 if (s.empty() || s.back() != end) s.push_back(end);
527 auto it = s.cbegin() + start;
528 auto dest = s.begin() + start;
529 auto it_end = s.cend();
530 bool has_spaces =
false;
534 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
540 if (has_spaces) *dest = *it;
544 if (has_spaces) s.resize(
static_cast<size_t>(dest - s.begin()));
550 return s.size() >= 2 && s.front() == q && s.back() == q;
557 substitute(s, std::string(
"\\"), std::string(
"\\\\"));
558 substitute(s, std::string(1, q), std::string(
"\\") + q);
562 substitute(s, std::string(1, q), std::string(2, q));
564 s.insert(s.begin(), q);
573 "'" + s +
"' does not have the expected format of a quoted string");
578 substitute(s, std::string(
"\\") + q, std::string(1, q));
579 substitute(s, std::string(
"\\\\"), std::string(
"\\"));
583 substitute(s, std::string(2, q), std::string(1, q));
596 inline bool split(
const std::string& s,
char splitter,
597 std::vector<std::string>& substrings,
598 bool quote_protect =
false)
601 if (s.empty())
return false;
603 size_t nsplits =
static_cast<size_t>(std::count(s.begin(), s.end(), splitter));
605 if (!quote_protect && nsplits == 0)
607 substrings.push_back(s);
611 substrings.reserve(nsplits + 1);
616 auto begin = s.cbegin();
617 auto end = s.cbegin();
618 for (; end != s.cend(); ++end)
620 if (*end ==
'"') ++quote_count;
621 if ((quote_count % 2 == 0) && (*end == splitter))
623 std::string block(begin, end);
625 bool has_start = block.size() >= 1 && block.front() ==
'"';
626 bool has_end = block.size() >= 1 && block.back() ==
'"';
627 if (block.size() >= 2 && (has_start ^ has_end))
629 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
630 if (block.size() >= 2 && has_start && has_end)
631 block = block.substr(1, block.size() - 2);
632 substrings.push_back(std::move(block));
636 if (substrings.empty()) { substrings.push_back(s);
return false; }
637 std::string block(begin, end);
639 bool has_start = block.size() >= 1 && block.front() ==
'"';
640 bool has_end = block.size() >= 1 && block.back() ==
'"';
641 if (block.size() >= 2 && (has_start ^ has_end))
643 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
644 if (block.size() >= 2 && has_start && has_end)
645 block = block.substr(1, block.size() - 2);
646 substrings.push_back(std::move(block));
650 auto begin = s.cbegin();
651 for (
auto it = s.cbegin(); it != s.cend(); ++it)
655 substrings.emplace_back(begin, it);
659 substrings.emplace_back(begin, s.cend());
666 inline bool split(
const std::string& s,
const std::string& splitter,
667 std::vector<std::string>& substrings)
670 if (s.empty())
return false;
671 if (splitter.empty())
673 substrings.resize(s.size());
674 for (
size_t i = 0; i < s.size(); ++i) substrings[i] = std::string(1, s[i]);
677 size_t len = splitter.size();
679 size_t pos = s.find(splitter);
680 while (pos != std::string::npos)
682 substrings.push_back(s.substr(start, pos - start));
684 pos = s.find(splitter, start);
686 substrings.push_back(s.substr(start));
687 return substrings.size() > 1;
692 inline bool split_quoted(
const std::string& s,
const std::string& splitter,
693 std::vector<std::string>& substrings,
697 if (s.empty() || splitter.empty())
return false;
699 bool in_quote =
false;
700 char targets[2] = {q, splitter[0]};
701 std::string rest = splitter.substr(1);
704 for (
size_t i = 0; i < s.size(); ++i)
708 bool embedded =
false;
711 for (; i < s.size(); ++i)
713 if (s[i] ==
'\\') embedded = !embedded;
714 else if (s[i] == q && !embedded)
break;
715 else embedded =
false;
720 for (; i < s.size(); ++i)
725 if (i + 1 < s.size() && s[i + 1] == q) embedded = !embedded;
726 else if (!embedded)
break;
727 else embedded =
false;
735 i = s.find_first_of(targets, i, 2);
736 if (i == std::string::npos)
break;
737 if (s[i] == q) { in_quote =
true; }
738 else if (s.compare(i + 1, rest.size(), rest) == 0)
740 substrings.push_back(s.substr(start, i - start));
741 start = i + splitter.size();
748 "unbalanced quotation marks in string '" + s +
"'");
749 substrings.push_back(s.substr(start));
750 return substrings.size() > 1;
754 template <
class StringIterator>
756 StringIterator
first, StringIterator last,
757 const std::string& glue =
"")
759 if (
first == last) { target.clear();
return; }
761 for (
auto it = ++
first; it != last; ++it)
769 template <
class StringIterator>
771 const std::string& glue =
"")
789 template <std::ranges::range Container>
790 inline std::string
concatenate(
const Container& container,
const std::string& glue =
"")
792 return concatenate(container.begin(), container.end(), glue);
802 namespace StringConversions
804 inline void append(
int i, std::string& target)
806 inline void append(
unsigned int i, std::string& target)
808 inline void append(
short int i, std::string& target)
810 inline void append(
short unsigned int i, std::string& target)
812 inline void append(
long int i, std::string& target)
814 inline void append(
long unsigned int i, std::string& target)
816 inline void append(
long long unsigned int i, std::string& target)
818 inline void append(
long long signed int i, std::string& target)
820 inline void append(
float f, std::string& target)
822 inline void append(
double d, std::string& target)
824 inline void append(
long double ld, std::string& target)
835 template <
typename T>
838 inline std::string
toString(
float f,
bool fp =
true)
840 inline std::string
toString(
double d,
bool fp =
true)
842 inline std::string
toString(
long double ld,
bool fp =
true)
867inline std::string&
operator+=(std::string& s,
unsigned int i)
871inline std::string&
operator+=(std::string& s,
short unsigned int i)
875inline std::string&
operator+=(std::string& s,
long unsigned int i)
877inline std::string&
operator+=(std::string& s,
long long unsigned int i)
879inline std::string&
operator+=(std::string& s,
long long signed int i)
885inline std::string&
operator+=(std::string& s,
long double ld)
890{
return std::move(s += i); }
891inline std::string
operator+(std::string s,
unsigned int i)
892{
return std::move(s += i); }
894{
return std::move(s += i); }
895inline std::string
operator+(std::string s,
short unsigned int i)
896{
return std::move(s += i); }
898{
return std::move(s += i); }
899inline std::string
operator+(std::string s,
long unsigned int i)
900{
return std::move(s += i); }
901inline std::string
operator+(std::string s,
long long unsigned int i)
902{
return std::move(s += i); }
903inline std::string
operator+(std::string s,
long long signed int i)
904{
return std::move(s += i); }
906{
return std::move(s += f); }
908{
return std::move(s += d); }
909inline std::string
operator+(std::string s,
long double ld)
910{
return std::move(s += ld); }
std::string & operator+=(std::string &s, int i)
Definition StringUtils.h:865
std::string operator+(std::string s, int i)
Definition StringUtils.h:889
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)
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:179
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:804
void appendLowP(float f, std::string &target)
Definition StringUtils.h:826
std::string toStringLowP(float f)
Definition StringUtils.h:849
std::string toString()
Definition StringUtils.h:846
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:470
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:502
bool hasPrefix(const std::string &s, const std::string &prefix)
Definition StringUtils.h:276
std::string & toUpper(std::string &s)
Definition StringUtils.h:413
std::string chop(const std::string &s, Size n)
Remove n characters from the end; returns empty string if n >= size.
Definition StringUtils.h:384
std::string & trim(std::string &s)
Definition StringUtils.h:395
std::string & unquote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:569
bool hasSuffix(const std::string &s, const std::string &sfx)
Definition StringUtils.h:285
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:463
std::string & firstToUpper(std::string &s)
Definition StringUtils.h:429
void appendToStrLowP(float f, std::string &target)
std::string suffix(const std::string &s, size_t length)
Definition StringUtils.h:320
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:363
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:548
std::string toLowered(std::string s)
Returns a lower-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:427
std::string & removeWhitespaces(std::string &s)
Definition StringUtils.h:523
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:313
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:294
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:411
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:596
std::string & ensureLastChar(std::string &s, char end)
Definition StringUtils.h:515
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:755
std::string & simplify(std::string &s)
Definition StringUtils.h:441
std::string & reverse(std::string &s)
Definition StringUtils.h:435
bool has(const std::string &s, Byte byte)
Definition StringUtils.h:301
double toDouble(const std::string &s)
Definition StringUtils.h:171
std::string & toLower(std::string &s)
Definition StringUtils.h:419
std::string & remove(std::string &s, char what)
Definition StringUtils.h:505
std::string toUppered(std::string s)
Returns an upper-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:425
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:692
std::string & quote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:553
std::string & substitute(std::string &s, char from, char to)
Definition StringUtils.h:477
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