66 if (d < pow(10.0,
Int(n - sign - 2)))
76 while (d > pow(10.0,
Int(n - sign - 4)))
90 return s.str().substr(0, n);
95 if (this_s.size() < size)
97 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
104 if (this_s.size() < size)
106 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
113 if (
string.size() > this_s.size())
121 return this_s.compare(0,
string.size(),
string) == 0;
126 if (
string.size() > this_s.size())
134 return this_s.compare(this_s.size() -
string.size(),
string.size(),
string) == 0;
139 return this_s.find(
string) != std::string::npos;
144 return this_s.find(
char(
byte)) != std::string::npos;
149 if (length > this_s.size())
153 return this_s.
substr(0, length);
158 if (length > this_s.size())
162 return this_s.
substr(this_s.size() - length, length);
171 if (length >
Int(this_s.size()))
175 return this_s.
substr(0, length);
184 if (length >
Int(this_s.size()))
188 return this_s.
substr(this_s.size() - length, length);
193 Size pos = this_s.find(delim);
194 if (pos == std::string::npos)
199 return this_s.
substr(0, pos);
204 Size pos = this_s.rfind(delim);
205 if (pos == std::string::npos)
210 return this_s.
substr(++pos);
215 Size begin = std::min(pos, this_s.size());
216 return static_cast<String>(this_s.std::string::substr(begin, n));
222 if (n < this_s.size())
224 end = this_s.size() - n;
226 return String(this_s.begin(), this_s.begin() + end);
232 std::string::iterator begin = this_s.begin();
233 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
239 if (begin == this_s.end())
246 std::string::iterator end = this_s.end();
248 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
255 if (begin == this_s.begin() && end == this_s.end())
262 this_s.std::string::operator=(std::string(begin, end));
269 return (this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q);
281 this_s.std::string::operator=(q + this_s + q);
291 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
292 "'" + this_s +
"' does not have the expected format of a quoted string");
294 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
309 bool last_was_whitespace =
false;
310 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
312 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
314 if (!last_was_whitespace)
318 last_was_whitespace =
true;
323 last_was_whitespace =
false;
333 srand(time(
nullptr));
336 for (
Size i = 0; i < length; ++i)
338 random =
static_cast<size_t>(floor((
static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
341 tmp[i] =
static_cast<char>(
random + 48);
345 tmp[i] =
static_cast<char>(
random + 55);
349 tmp[i] =
static_cast<char>(
random + 61);
358 for (
Size i = 0; i != this_s.size(); ++i)
360 this_s[i] = tmp[this_s.size() - 1 - i];
365 static inline bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
372 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
374 if (!quote_protect && (nsplits == 0))
376 substrings.push_back(this_s);
381 substrings.reserve(nsplits + 1);
384 std::string::const_iterator begin = this_s.begin();
385 std::string::const_iterator end = this_s.begin();
390 for (; end != this_s.end(); ++end)
396 if ((quote_count % 2 == 0) && (*end == splitter))
400 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
405 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
406 String(
"Could not dequote string '") + block +
407 "' due to wrongly placed '\"'.");
409 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
412 block = block.
substr(1, block.size() - 2);
414 substrings.push_back(block);
419 if (substrings.empty())
421 substrings.push_back(this_s);
427 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
432 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
433 String(
"Could not dequote string '") + block +
434 "' due to wrongly placed '\"'.");
436 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
439 block = block.
substr(1, block.size() - 2);
441 substrings.push_back(block);
445 for (; end != this_s.end(); ++end)
447 if (*end == splitter)
449 substrings.push_back(
String(begin, end));
453 substrings.push_back(
String(begin, end));
460 static inline bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
466 if (splitter.empty())
468 substrings.resize(this_s.size());
469 for (
Size i = 0; i < this_s.size(); ++i)
470 substrings[i] = this_s[i];
474 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
477 while (pos != std::string::npos)
479 substrings.push_back(this_s.
substr(start, pos - start));
481 pos = this_s.find(splitter, start);
483 substrings.push_back(this_s.
substr(start, this_s.size() - start));
484 return substrings.size() > 1;
491 if (this_s.empty() || splitter.empty())
494 bool in_quote =
false;
495 char targets[2] = {q, splitter[0]};
496 std::string rest = splitter.
substr(1, splitter.size() - 1);
498 for (
Size i = 0; i < this_s.size(); ++i)
502 bool embedded =
false;
505 for (; i < this_s.size(); ++i)
507 if (this_s[i] ==
'\\')
508 embedded = !embedded;
509 else if ((this_s[i] == q) && !embedded)
517 for (; i < this_s.size(); ++i)
524 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
525 embedded = !embedded;
539 i = this_s.find_first_of(targets, i, 2);
540 if (i == std::string::npos)
544 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
546 substrings.push_back(this_s.
substr(start, i - start));
547 start = i + splitter.size();
555 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
556 "unbalanced quotation marks in string '" + this_s +
"'");
558 substrings.push_back(this_s.
substr(start, this_s.size() - start));
559 return substrings.size() > 1;
564 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))toupper);
572 this_s[0] = toupper(this_s[0]);
579 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))tolower);
585 std::replace(this_s.begin(), this_s.end(), from, to);
593 std::vector<String> parts;
594 this_s.
split(from, parts);
595 this_s.
concatenate(parts.begin(), parts.end(), to);
602 this_s.erase(
std::remove(this_s.begin(), this_s.end(), what), this_s.end());
609 this_s.append(1, end);
615 std::string::const_iterator it = this_s.begin();
616 std::string::iterator dest = this_s.begin();
617 std::string::const_iterator it_end = this_s.end();
618 bool has_spaces(
false);
622 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
629 if (has_spaces) *dest = *it;
636 if (has_spaces) this_s.resize(dest - this_s.begin());
Invalid conversion exception.
Definition: Exception.h:356
Element could not be found exception.
Definition: Exception.h:676
Int overflow exception.
Definition: Exception.h:247
Int underflow exception.
Definition: Exception.h:209
A more convenient string class.
Definition: String.h:60
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:81
@ ESCAPE
Definition: String.h:81
@ DOUBLE
Definition: String.h:81
@ NONE
Definition: String.h:81
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:498
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:111
int Int
Signed integer type.
Definition: Types.h:102
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
const double c
Definition: Constants.h:214
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:111
static String & firstToUpper(String &this_s)
Definition: StringUtilsSimple.h:568
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:600
static String chop(const String &this_s, Size n)
Definition: StringUtilsSimple.h:219
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:355
static bool isQuoted(const String &this_s, char q)
Definition: StringUtilsSimple.h:267
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtilsSimple.h:57
static String & substitute(String &this_s, char from, char to)
Definition: StringUtilsSimple.h:583
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:124
static String & toLower(String &this_s)
Definition: StringUtilsSimple.h:577
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtilsSimple.h:606
static String random(UInt length)
Definition: StringUtilsSimple.h:331
static String & trim(String &this_s)
Definition: StringUtilsSimple.h:229
static String & simplify(String &this_s)
Definition: StringUtilsSimple.h:305
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:285
static String & removeWhitespaces(String &this_s)
Definition: StringUtilsSimple.h:613
static String & toUpper(String &this_s)
Definition: StringUtilsSimple.h:562
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtilsSimple.h:365
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:137
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtilsSimple.h:102
static bool has(const String &this_s, Byte byte)
Definition: StringUtilsSimple.h:142
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:272
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:487
static String suffix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:156
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtilsSimple.h:213
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtilsSimple.h:93
static String prefix(const String &this_s, size_t length)
Definition: StringUtilsSimple.h:147
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
constexpr Int writtenDigits(const FloatingPointType &=FloatingPointType())
Number of digits commonly used for writing a floating point type (a.k.a. precision)....
Definition: Types.h:294