18 #include <string_view>
41 if (d < pow(10.0,
Int(n - sign - 2)))
51 while (d > pow(10.0,
Int(n - sign - 4)))
65 return s.str().substr(0, n);
70 if (this_s.size() < size)
72 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
79 if (this_s.size() < size)
81 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
88 if (
string.size() > this_s.size())
96 return this_s.compare(0,
string.size(),
string) == 0;
101 if (
string.size() > this_s.size())
109 return this_s.compare(this_s.size() -
string.size(),
string.size(),
string) == 0;
114 return this_s.find(
string) != std::string::npos;
119 return this_s.find(
char(
byte)) != std::string::npos;
124 if (length > this_s.size())
128 return this_s.
substr(0, length);
133 if (length > this_s.size())
137 return this_s.
substr(this_s.size() - length, length);
146 if (length >
Int(this_s.size()))
150 return this_s.
substr(0, length);
159 if (length >
Int(this_s.size()))
163 return this_s.
substr(this_s.size() - length, length);
168 Size pos = this_s.find(delim);
169 if (pos == std::string::npos)
174 return this_s.
substr(0, pos);
179 Size pos = this_s.rfind(delim);
180 if (pos == std::string::npos)
185 return this_s.
substr(++pos);
190 Size begin = std::min(pos, this_s.size());
191 return static_cast<String>(this_s.std::string::substr(begin, n));
197 if (n < this_s.size())
199 end = this_s.size() - n;
201 return String(this_s.begin(), this_s.begin() + end);
207 std::string::iterator begin = this_s.begin();
208 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
214 if (begin == this_s.end())
221 std::string::iterator end = this_s.end();
223 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
230 if (begin == this_s.begin() && end == this_s.end())
237 this_s.std::string::operator=(std::string(begin, end));
244 return (this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q);
256 this_s.std::string::operator=(q + this_s + q);
266 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
267 "'" + this_s +
"' does not have the expected format of a quoted string");
269 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
284 bool last_was_whitespace =
false;
285 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
287 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
289 if (!last_was_whitespace)
293 last_was_whitespace =
true;
298 last_was_whitespace =
false;
308 srand(time(
nullptr));
311 for (
Size i = 0; i < length; ++i)
313 random =
static_cast<size_t>(floor((
static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
316 tmp[i] =
static_cast<char>(
random + 48);
320 tmp[i] =
static_cast<char>(
random + 55);
324 tmp[i] =
static_cast<char>(
random + 61);
333 for (
Size i = 0; i != this_s.size(); ++i)
335 this_s[i] = tmp[this_s.size() - 1 - i];
340 static inline bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
347 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
349 if (!quote_protect && (nsplits == 0))
351 substrings.push_back(this_s);
356 substrings.reserve(nsplits + 1);
359 std::string::const_iterator begin = this_s.begin();
360 std::string::const_iterator end = this_s.begin();
365 for (; end != this_s.end(); ++end)
371 if ((quote_count % 2 == 0) && (*end == splitter))
375 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
380 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
381 String(
"Could not dequote string '") + block +
382 "' due to wrongly placed '\"'.");
384 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
387 block = block.
substr(1, block.size() - 2);
389 substrings.push_back(block);
394 if (substrings.empty())
396 substrings.push_back(this_s);
402 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
407 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
408 String(
"Could not dequote string '") + block +
409 "' due to wrongly placed '\"'.");
411 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
414 block = block.
substr(1, block.size() - 2);
416 substrings.push_back(block);
420 for (; end != this_s.end(); ++end)
422 if (*end == splitter)
424 substrings.push_back(
String(begin, end));
428 substrings.push_back(
String(begin, end));
435 static inline bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
441 if (splitter.empty())
443 substrings.resize(this_s.size());
444 for (
Size i = 0; i < this_s.size(); ++i)
445 substrings[i] = this_s[i];
449 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
452 while (pos != std::string::npos)
454 substrings.push_back(this_s.
substr(start, pos - start));
456 pos = this_s.find(splitter, start);
458 substrings.push_back(this_s.
substr(start, this_s.size() - start));
459 return substrings.size() > 1;
466 if (this_s.empty() || splitter.empty())
469 bool in_quote =
false;
470 char targets[2] = {q, splitter[0]};
471 std::string rest = splitter.
substr(1, splitter.size() - 1);
473 for (
Size i = 0; i < this_s.size(); ++i)
477 bool embedded =
false;
480 for (; i < this_s.size(); ++i)
482 if (this_s[i] ==
'\\')
483 embedded = !embedded;
484 else if ((this_s[i] == q) && !embedded)
492 for (; i < this_s.size(); ++i)
499 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
500 embedded = !embedded;
514 i = this_s.find_first_of(targets, i, 2);
515 if (i == std::string::npos)
519 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
521 substrings.push_back(this_s.
substr(start, i - start));
522 start = i + splitter.size();
530 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
531 "unbalanced quotation marks in string '" + this_s +
"'");
533 substrings.push_back(this_s.
substr(start, this_s.size() - start));
534 return substrings.size() > 1;
539 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))toupper);
547 this_s[0] = toupper(this_s[0]);
554 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))tolower);
560 std::replace(this_s.begin(), this_s.end(), from, to);
568 std::vector<String> parts;
569 this_s.
split(from, parts);
570 this_s.
concatenate(parts.begin(), parts.end(), to);
577 this_s.erase(
std::remove(this_s.begin(), this_s.end(), what), this_s.end());
584 this_s.append(1, end);
600 auto pos =
skipWhitespace(data.data(), data.data() + data.size());
601 return pos - data.data();
617 return pos - data.data();
623 std::string::const_iterator it = this_s.begin() + start;
624 std::string::iterator dest = this_s.begin() + start;
625 std::string::const_iterator it_end = this_s.end();
626 bool has_spaces(
false);
630 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
637 if (has_spaces) *dest = *it;
644 if (has_spaces) this_s.resize(dest - this_s.begin());
Invalid conversion exception.
Definition: Exception.h:330
Element could not be found exception.
Definition: Exception.h:653
Int overflow exception.
Definition: Exception.h:211
Int underflow exception.
Definition: Exception.h:175
A more convenient string class.
Definition: String.h:34
String substr(size_t pos=0, size_t n=npos) const
Wrapper for the STL substr() method. Returns a String object with its contents initialized to a subst...
String prefix(SizeType length) const
returns the prefix of length length
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:55
@ ESCAPE
Definition: String.h:55
@ DOUBLE
Definition: String.h:55
@ NONE
Definition: String.h:55
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
String suffix(SizeType length) const
returns the suffix of length length
void concatenate(StringIterator first, StringIterator last, const String &glue="")
Concatenates all elements from first to last-1 and inserts glue between the elements.
Definition: String.h:472
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
const double c
Definition: Constants.h:188
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:86
static String & firstToUpper(String &this_s)
Definition: StringUtilsSimple.h:543
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:575
static String chop(const String &this_s, Size n)
Definition: StringUtilsSimple.h:194
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:330
static bool isQuoted(const String &this_s, char q)
Definition: StringUtilsSimple.h:242
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtilsSimple.h:32
static String & substitute(String &this_s, char from, char to)
Definition: StringUtilsSimple.h:558
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:99
static String & toLower(String &this_s)
Definition: StringUtilsSimple.h:552
const char * skipWhitespace(const char *p, const char *p_end)
Get the first non-whitespace character (anything but \n, \t, \r, ' ') in the string pointed to by p (...
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtilsSimple.h:581
const char * skipNonWhitespace(const char *p, const char *p_end)
Get the first whitespace character (\n, \t, \r, ' ') in the string pointed to by p (where p_end is pa...
static String random(UInt length)
Definition: StringUtilsSimple.h:306
static String & trim(String &this_s)
Definition: StringUtilsSimple.h:204
static String & simplify(String &this_s)
Definition: StringUtilsSimple.h:280
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:260
static String & removeWhitespaces(String &this_s)
Definition: StringUtilsSimple.h:620
static String & toUpper(String &this_s)
Definition: StringUtilsSimple.h:537
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtilsSimple.h:340
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:112
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtilsSimple.h:77
static bool has(const String &this_s, Byte byte)
Definition: StringUtilsSimple.h:117
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:247
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:462
static String suffix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:131
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtilsSimple.h:188
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtilsSimple.h:68
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:122
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
constexpr Int writtenDigits(const FloatingPointType &=FloatingPointType())
Number of digits commonly used for writing a floating point type (a.k.a. precision)....
Definition: Types.h:264