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));
276 this_s.std::string::operator=(q + this_s + q);
283 if ((this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q))
286 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
287 "'" + this_s +
"' does not have the expected format of a quoted string");
289 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
304 bool last_was_whitespace =
false;
305 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
307 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
309 if (!last_was_whitespace)
313 last_was_whitespace =
true;
318 last_was_whitespace =
false;
328 srand(time(
nullptr));
331 for (
Size i = 0; i < length; ++i)
333 random =
static_cast<size_t>(floor((
static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
336 tmp[i] =
static_cast<char>(
random + 48);
340 tmp[i] =
static_cast<char>(
random + 55);
344 tmp[i] =
static_cast<char>(
random + 61);
353 for (
Size i = 0; i != this_s.size(); ++i)
355 this_s[i] = tmp[this_s.size() - 1 - i];
360 static inline bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
367 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
369 if (!quote_protect && (nsplits == 0))
371 substrings.push_back(this_s);
376 substrings.reserve(nsplits + 1);
379 std::string::const_iterator begin = this_s.begin();
380 std::string::const_iterator end = this_s.begin();
385 for (; end != this_s.end(); ++end)
391 if ((quote_count % 2 == 0) && (*end == splitter))
395 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
400 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
401 String(
"Could not dequote string '") + block +
402 "' due to wrongly placed '\"'.");
404 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
407 block = block.
substr(1, block.size() - 2);
409 substrings.push_back(block);
414 if (substrings.empty())
416 substrings.push_back(this_s);
422 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
427 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
428 String(
"Could not dequote string '") + block +
429 "' due to wrongly placed '\"'.");
431 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
434 block = block.
substr(1, block.size() - 2);
436 substrings.push_back(block);
440 for (; end != this_s.end(); ++end)
442 if (*end == splitter)
444 substrings.push_back(
String(begin, end));
448 substrings.push_back(
String(begin, end));
455 static inline bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
461 if (splitter.empty())
463 substrings.resize(this_s.size());
464 for (
Size i = 0; i < this_s.size(); ++i)
465 substrings[i] = this_s[i];
469 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
472 while (pos != std::string::npos)
474 substrings.push_back(this_s.
substr(start, pos - start));
476 pos = this_s.find(splitter, start);
478 substrings.push_back(this_s.
substr(start, this_s.size() - start));
479 return substrings.size() > 1;
486 if (this_s.empty() || splitter.empty())
489 bool in_quote =
false;
490 char targets[2] = {q, splitter[0]};
491 std::string rest = splitter.
substr(1, splitter.size() - 1);
493 for (
Size i = 0; i < this_s.size(); ++i)
497 bool embedded =
false;
500 for (; i < this_s.size(); ++i)
502 if (this_s[i] ==
'\\')
503 embedded = !embedded;
504 else if ((this_s[i] == q) && !embedded)
512 for (; i < this_s.size(); ++i)
519 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
520 embedded = !embedded;
534 i = this_s.find_first_of(targets, i, 2);
535 if (i == std::string::npos)
539 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
541 substrings.push_back(this_s.
substr(start, i - start));
542 start = i + splitter.size();
550 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
551 "unbalanced quotation marks in string '" + this_s +
"'");
553 substrings.push_back(this_s.
substr(start, this_s.size() - start));
554 return substrings.size() > 1;
559 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))toupper);
567 this_s[0] = toupper(this_s[0]);
574 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))tolower);
580 std::replace(this_s.begin(), this_s.end(), from, to);
588 std::vector<String> parts;
589 this_s.
split(from, parts);
590 this_s.
concatenate(parts.begin(), parts.end(), to);
597 this_s.erase(
std::remove(this_s.begin(), this_s.end(), what), this_s.end());
604 this_s.append(1, end);
610 std::string::const_iterator it = this_s.begin();
611 std::string::iterator dest = this_s.begin();
612 std::string::const_iterator it_end = this_s.end();
613 bool has_spaces(
false);
617 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
624 if (has_spaces) *dest = *it;
631 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:466
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:209
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:111
static String & firstToUpper(String &this_s)
Definition: StringUtilsSimple.h:563
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:595
static String chop(const String &this_s, Size n)
Definition: StringUtilsSimple.h:219
static String & reverse(String &this_s)
Definition: StringUtilsSimple.h:350
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:578
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtilsSimple.h:124
static String & toLower(String &this_s)
Definition: StringUtilsSimple.h:572
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtilsSimple.h:601
static String random(UInt length)
Definition: StringUtilsSimple.h:326
static String & trim(String &this_s)
Definition: StringUtilsSimple.h:229
static String & simplify(String &this_s)
Definition: StringUtilsSimple.h:300
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:280
static String & removeWhitespaces(String &this_s)
Definition: StringUtilsSimple.h:608
static String & toUpper(String &this_s)
Definition: StringUtilsSimple.h:557
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtilsSimple.h:360
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:267
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtilsSimple.h:482
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:47
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