15#include <OpenMS/OpenMSConfig.h>
52 static float toFloat(
const std::string& s);
58 static bool extractDouble(
const char*& begin,
const char* end,
double& target);
62 static bool extractInt(
const char*& begin,
const char* end,
int& target);
77 OPENMS_DLLAPI
void appendToStr(
unsigned int i, std::string& target);
78 OPENMS_DLLAPI
void appendToStr(
short int i, std::string& target);
79 OPENMS_DLLAPI
void appendToStr(
short unsigned int i, std::string& target);
80 OPENMS_DLLAPI
void appendToStr(
long int i, std::string& target);
81 OPENMS_DLLAPI
void appendToStr(
long unsigned int i, std::string& target);
82 OPENMS_DLLAPI
void appendToStr(
long long unsigned int i, std::string& target);
83 OPENMS_DLLAPI
void appendToStr(
long long signed int i, std::string& target);
87 OPENMS_DLLAPI
void appendToStr(
double d, std::string& target);
89 OPENMS_DLLAPI
void appendToStr(
long double ld, std::string& target);
102 inline std::string
toStr(
unsigned int i)
104 inline std::string
toStr(
short int i)
106 inline std::string
toStr(
short unsigned int i)
108 inline std::string
toStr(
long int i)
110 inline std::string
toStr(
long unsigned int i)
112 inline std::string
toStr(
long long unsigned int i)
114 inline std::string
toStr(
long long signed int i)
118 OPENMS_DLLAPI std::string
toStr(
float f,
bool full_precision =
true);
120 OPENMS_DLLAPI std::string
toStr(
double d,
bool full_precision =
true);
122 OPENMS_DLLAPI std::string
toStr(
long double ld,
bool full_precision =
true);
125 {
return std::string(1, c); }
127 inline std::string
toStr(
const std::string& s)
130 inline std::string
toStr(
const char* s)
131 {
return std::string(s); }
160 inline std::string
toStr(std::string_view sv) {
return std::string(sv); }
172 inline bool extractDouble(
const char*& begin,
const char* end,
double& target)
175 inline bool extractInt(
const char*& begin,
const char* end,
int& target)
179 inline bool extractDouble(std::string::const_iterator& begin,
const std::string::const_iterator& end,
double& target)
181 if (begin == end)
return false;
182 const char*
const p_start = &(*begin);
183 const char* p = p_start;
186 const char*
const e = p_start + (end - begin);
188 begin += (p - p_start);
192 inline bool extractDouble(std::string::iterator& begin,
const std::string::iterator& end,
double& target)
194 if (begin == end)
return false;
195 const char*
const p_start = &(*begin);
196 const char* p = p_start;
199 const char*
const e = p_start + (end - begin);
201 begin += (p - p_start);
205 inline bool extractInt(std::string::const_iterator& begin,
const std::string::const_iterator& end,
int& target)
207 if (begin == end)
return false;
208 const char*
const p_start = &(*begin);
209 const char* p = p_start;
212 const char*
const e = p_start + (end - begin);
214 begin += (p - p_start);
218 inline bool extractInt(std::string::iterator& begin,
const std::string::iterator& end,
int& target)
220 if (begin == end)
return false;
221 const char*
const p_start = &(*begin);
222 const char* p = p_start;
225 const char*
const e = p_start + (end - begin);
227 begin += (p - p_start);
242 auto pos =
skipWhitespace(data.data(), data.data() + data.size());
243 return static_cast<size_t>(pos - data.data());
253 return static_cast<size_t>(pos - data.data());
262 [[maybe_unused]] OPENMS_DLLAPI std::string
number(
double d,
UInt n);
277 if (
prefix.size() > s.size())
return false;
278 if (
prefix.empty())
return true;
282 {
return !s.empty() && s.front() == c; }
284 inline bool hasSuffix(
const std::string& s,
const std::string& sfx)
286 if (sfx.size() > s.size())
return false;
287 if (sfx.empty())
return true;
288 return s.compare(s.size() - sfx.size(), sfx.size(), sfx) == 0;
291 {
return !s.empty() && s.back() == c; }
295 return s.find(sub) != std::string::npos;
298 {
return s.find(c) != std::string::npos; }
300 inline bool has(
const std::string& s,
Byte byte)
302 return s.find(
static_cast<char>(
byte)) != std::string::npos;
304 inline bool has(
const std::string& s,
char c)
305 {
return s.find(c) != std::string::npos; }
312 inline std::string
prefix(
const std::string& s,
size_t length)
314 if (length > s.size())
316 return s.substr(0, length);
319 inline std::string
suffix(
const std::string& s,
size_t length)
321 if (length > s.size())
323 return s.substr(s.size() - length, length);
326 inline std::string
prefix(
const std::string& s,
Int length)
330 return prefix(s,
static_cast<size_t>(length));
333 inline std::string
suffix(
const std::string& s,
Int length)
337 return suffix(s,
static_cast<size_t>(length));
345 inline std::string
prefix(
const std::string& s,
char delim)
347 return s.substr(0, s.find(delim));
355 inline std::string
suffix(
const std::string& s,
char delim)
357 size_t pos = s.rfind(delim);
358 return pos == std::string::npos ? s : s.substr(pos + 1);
362 inline std::string
substr(
const std::string& s,
size_t pos = 0,
size_t n = std::string::npos)
364 size_t begin = std::min(pos, s.size());
365 return s.substr(begin, n);
368 inline std::string
substr(std::string_view s,
size_t pos = 0,
size_t n = std::string::npos)
370 size_t begin = std::min(pos, s.size());
371 return std::string(s.substr(begin, n));
377 inline bool hasSuffix(std::string_view s,
const std::string& sfx)
378 {
return s.size() >= sfx.size() && s.substr(s.size() - sfx.size()) == sfx; }
380 {
return s.find(sub) != std::string_view::npos; }
383 inline std::string
chop(
const std::string& s,
Size n)
385 size_t end = (n < s.size()) ? s.size() - n : 0;
386 return std::string(s.begin(), s.begin() + end);
394 inline std::string&
trim(std::string& s)
396 auto begin = s.begin();
397 while (begin != s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
399 if (begin == s.end()) { s.clear();
return s; }
400 auto end = s.end() - 1;
401 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
404 if (begin != s.begin() || end != s.end())
405 s.assign(begin, end);
410 inline std::string
trimmed(std::string s) {
return std::move(
trim(s)); }
414 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::toupper(c); });
420 std::transform(s.begin(), s.end(), s.begin(), [](
unsigned char c){ return std::tolower(c); });
430 if (!s.empty()) s[0] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(s[0])));
436 std::reverse(s.begin(), s.end());
443 result.reserve(s.size());
444 bool last_ws =
false;
447 if (c ==
' ' || c ==
'\n' || c ==
'\t' || c ==
'\r')
449 if (!last_ws) result +=
' ';
465 s.insert(s.begin(), size - s.size(), c);
472 s.append(size - s.size(), c);
476 inline std::string&
substitute(std::string& s,
char from,
char to)
478 std::replace(s.begin(), s.end(), from, to);
483 inline std::string&
substitute(std::string& s,
const std::string& from,
const std::string& to)
485 if (from.empty())
return s;
487 result.reserve(s.size());
490 while ((pos = s.find(from, start)) != std::string::npos)
492 result.append(s, start, pos - start);
494 start = pos + from.size();
496 result.append(s, start, std::string::npos);
502 inline std::string
substituted(std::string s,
const std::string& from,
const std::string& to) {
return std::move(
substitute(s, from, to)); }
504 inline std::string&
remove(std::string& s,
char what)
506 s.erase(std::remove(s.begin(), s.end(), what), s.end());
516 if (s.empty() || s.back() != end) s.push_back(end);
526 auto it = s.cbegin() + start;
527 auto dest = s.begin() + start;
528 auto it_end = s.cend();
529 bool has_spaces =
false;
533 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
539 if (has_spaces) *dest = *it;
543 if (has_spaces) s.resize(
static_cast<size_t>(dest - s.begin()));
549 return s.size() >= 2 && s.front() == q && s.back() == q;
556 substitute(s, std::string(
"\\"), std::string(
"\\\\"));
557 substitute(s, std::string(1, q), std::string(
"\\") + q);
561 substitute(s, std::string(1, q), std::string(2, q));
563 s.insert(s.begin(), q);
572 "'" + s +
"' does not have the expected format of a quoted string");
577 substitute(s, std::string(
"\\") + q, std::string(1, q));
578 substitute(s, std::string(
"\\\\"), std::string(
"\\"));
582 substitute(s, std::string(2, q), std::string(1, q));
595 inline bool split(
const std::string& s,
char splitter,
596 std::vector<std::string>& substrings,
597 bool quote_protect =
false)
600 if (s.empty())
return false;
602 size_t nsplits =
static_cast<size_t>(std::count(s.begin(), s.end(), splitter));
604 if (!quote_protect && nsplits == 0)
606 substrings.push_back(s);
610 substrings.reserve(nsplits + 1);
615 auto begin = s.cbegin();
616 auto end = s.cbegin();
617 for (; end != s.cend(); ++end)
619 if (*end ==
'"') ++quote_count;
620 if ((quote_count % 2 == 0) && (*end == splitter))
622 std::string block(begin, end);
624 bool has_start = block.size() >= 1 && block.front() ==
'"';
625 bool has_end = block.size() >= 1 && block.back() ==
'"';
626 if (block.size() >= 2 && (has_start ^ has_end))
628 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
629 if (block.size() >= 2 && has_start && has_end)
630 block = block.substr(1, block.size() - 2);
631 substrings.push_back(std::move(block));
635 if (substrings.empty()) { substrings.push_back(s);
return false; }
636 std::string block(begin, end);
638 bool has_start = block.size() >= 1 && block.front() ==
'"';
639 bool has_end = block.size() >= 1 && block.back() ==
'"';
640 if (block.size() >= 2 && (has_start ^ has_end))
642 "Could not dequote string '" + block +
"' due to wrongly placed '\"'.");
643 if (block.size() >= 2 && has_start && has_end)
644 block = block.substr(1, block.size() - 2);
645 substrings.push_back(std::move(block));
649 auto begin = s.cbegin();
650 for (
auto it = s.cbegin(); it != s.cend(); ++it)
654 substrings.emplace_back(begin, it);
658 substrings.emplace_back(begin, s.cend());
665 inline bool split(
const std::string& s,
const std::string& splitter,
666 std::vector<std::string>& substrings)
669 if (s.empty())
return false;
670 if (splitter.empty())
672 substrings.resize(s.size());
673 for (
size_t i = 0; i < s.size(); ++i) substrings[i] = std::string(1, s[i]);
676 size_t len = splitter.size();
678 size_t pos = s.find(splitter);
679 while (pos != std::string::npos)
681 substrings.push_back(s.substr(start, pos - start));
683 pos = s.find(splitter, start);
685 substrings.push_back(s.substr(start));
686 return substrings.size() > 1;
691 inline bool split_quoted(
const std::string& s,
const std::string& splitter,
692 std::vector<std::string>& substrings,
696 if (s.empty() || splitter.empty())
return false;
698 bool in_quote =
false;
699 char targets[2] = {q, splitter[0]};
700 std::string rest = splitter.substr(1);
703 for (
size_t i = 0; i < s.size(); ++i)
707 bool embedded =
false;
710 for (; i < s.size(); ++i)
712 if (s[i] ==
'\\') embedded = !embedded;
713 else if (s[i] == q && !embedded)
break;
714 else embedded =
false;
719 for (; i < s.size(); ++i)
724 if (i + 1 < s.size() && s[i + 1] == q) embedded = !embedded;
725 else if (!embedded)
break;
726 else embedded =
false;
734 i = s.find_first_of(targets, i, 2);
735 if (i == std::string::npos)
break;
736 if (s[i] == q) { in_quote =
true; }
737 else if (s.compare(i + 1, rest.size(), rest) == 0)
739 substrings.push_back(s.substr(start, i - start));
740 start = i + splitter.size();
747 "unbalanced quotation marks in string '" + s +
"'");
748 substrings.push_back(s.substr(start));
749 return substrings.size() > 1;
753 template <
class StringIterator>
755 StringIterator
first, StringIterator last,
756 const std::string& glue =
"")
758 if (
first == last) { target.clear();
return; }
760 for (
auto it = ++
first; it != last; ++it)
768 template <
class StringIterator>
770 const std::string& glue =
"")
790 template <
typename Container>
791 requires requires(
const Container& c) { c.begin(); c.end(); }
792 inline std::string
concatenate(
const Container& container,
const std::string& glue =
"")
794 return concatenate(container.begin(), container.end(), glue);
804 namespace StringConversions
806 inline void append(
int i, std::string& target)
808 inline void append(
unsigned int i, std::string& target)
810 inline void append(
short int i, std::string& target)
812 inline void append(
short unsigned int i, std::string& target)
814 inline void append(
long int i, std::string& target)
816 inline void append(
long unsigned int i, std::string& target)
818 inline void append(
long long unsigned int i, std::string& target)
820 inline void append(
long long signed int i, std::string& target)
822 inline void append(
float f, std::string& target)
824 inline void append(
double d, std::string& target)
826 inline void append(
long double ld, std::string& target)
837 template <
typename T>
840 inline std::string
toString(
float f,
bool fp =
true)
842 inline std::string
toString(
double d,
bool fp =
true)
844 inline std::string
toString(
long double ld,
bool fp =
true)
869inline std::string&
operator+=(std::string& s,
unsigned int i)
873inline std::string&
operator+=(std::string& s,
short unsigned int i)
877inline std::string&
operator+=(std::string& s,
long unsigned int i)
879inline std::string&
operator+=(std::string& s,
long long unsigned int i)
881inline std::string&
operator+=(std::string& s,
long long signed int i)
887inline std::string&
operator+=(std::string& s,
long double ld)
892{
return std::move(s += i); }
893inline std::string
operator+(std::string s,
unsigned int i)
894{
return std::move(s += i); }
896{
return std::move(s += i); }
897inline std::string
operator+(std::string s,
short unsigned int i)
898{
return std::move(s += i); }
900{
return std::move(s += i); }
901inline std::string
operator+(std::string s,
long unsigned int i)
902{
return std::move(s += i); }
903inline std::string
operator+(std::string s,
long long unsigned int i)
904{
return std::move(s += i); }
905inline std::string
operator+(std::string s,
long long signed int i)
906{
return std::move(s += i); }
908{
return std::move(s += f); }
910{
return std::move(s += d); }
911inline std::string
operator+(std::string s,
long double ld)
912{
return std::move(s += ld); }
std::string & operator+=(std::string &s, int i)
Definition StringUtils.h:867
std::string operator+(std::string s, int i)
Definition StringUtils.h:891
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:45
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:806
void appendLowP(float f, std::string &target)
Definition StringUtils.h:828
std::string toStringLowP(float f)
Definition StringUtils.h:851
std::string toString()
Definition StringUtils.h:848
float toFloat(const std::string &s)
Definition StringUtils.h:169
void appendToStr(int i, std::string &target)
std::string & fillRight(std::string &s, char c, UInt size)
Definition StringUtils.h:469
Int64 toInt64(const std::string &s)
Definition StringUtils.h:168
std::string substituted(std::string s, char from, char to)
substitute on a copy (for chained/rvalue expressions)
Definition StringUtils.h:501
bool hasPrefix(const std::string &s, const std::string &prefix)
Definition StringUtils.h:275
std::string & toUpper(std::string &s)
Definition StringUtils.h:412
std::string chop(const std::string &s, Size n)
Remove n characters from the end; returns empty string if n >= size.
Definition StringUtils.h:383
std::string & trim(std::string &s)
Definition StringUtils.h:394
std::string & unquote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:568
bool hasSuffix(const std::string &s, const std::string &sfx)
Definition StringUtils.h:284
bool extractDouble(const char *&begin, const char *end, double &target)
Definition StringUtils.h:172
std::string & fillLeft(std::string &s, char c, UInt size)
Definition StringUtils.h:462
std::string & firstToUpper(std::string &s)
Definition StringUtils.h:428
void appendToStrLowP(float f, std::string &target)
std::string suffix(const std::string &s, size_t length)
Definition StringUtils.h:319
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:362
bool extractInt(const char *&begin, const char *end, int &target)
Definition StringUtils.h:175
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:547
std::string toLowered(std::string s)
Returns a lower-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:426
std::string & removeWhitespaces(std::string &s)
Definition StringUtils.h:522
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:312
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:293
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:410
std::string toStr(int i)
Definition StringUtils.h:100
bool split(const std::string &s, char splitter, std::vector< std::string > &substrings, bool quote_protect=false)
Definition StringUtils.h:595
std::string & ensureLastChar(std::string &s, char end)
Definition StringUtils.h:514
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:754
std::string & simplify(std::string &s)
Definition StringUtils.h:440
std::string & reverse(std::string &s)
Definition StringUtils.h:434
bool has(const std::string &s, Byte byte)
Definition StringUtils.h:300
double toDouble(const std::string &s)
Definition StringUtils.h:170
std::string & toLower(std::string &s)
Definition StringUtils.h:418
std::string & remove(std::string &s, char what)
Definition StringUtils.h:504
std::string toUppered(std::string s)
Returns an upper-cased copy of s (for use in chained/rvalue expressions)
Definition StringUtils.h:424
Int32 toInt32(const std::string &s)
Definition StringUtils.h:167
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:691
std::string & quote(std::string &s, char q='"', QuotingMethod method = QuotingMethod::ESCAPE)
Definition StringUtils.h:552
std::string & substitute(std::string &s, char from, char to)
Definition StringUtils.h:476
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition StringUtils.h:38