42 #include <QtCore/QString> 43 #include <boost/spirit/include/qi.hpp> 53 namespace StringConversions
78 return std::string(1,
c);
90 return std::string(s);
127 while (count < length && *(s + count) != 0)
153 if (d < pow(10.0,
Int(n - sign - 2)))
163 while (d > pow(10.0,
Int(n - sign - 4)))
177 return s.str().substr(0, n);
182 return QString::number(d,
'f', n);
187 if (this_s.size() < size)
189 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
196 if (this_s.size() < size)
198 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
226 if (
string.size() > this_s.size())
234 return this_s.compare(0,
string.size(),
string) == 0;
239 if (
string.size() > this_s.size())
247 return this_s.compare(this_s.size() -
string.size(),
string.size(), string) == 0;
252 return this_s.find(
string) != std::string::npos;
257 return this_s.find(
char(byte)) != std::string::npos;
262 if (length > this_s.size())
266 return this_s.
substr(0, length);
271 if (length > this_s.size())
275 return this_s.
substr(this_s.size() - length, length);
284 if (length >
Int(this_s.size()))
288 return this_s.
substr(0, length);
297 if (length >
Int(this_s.size()))
301 return this_s.
substr(this_s.size() - length, length);
306 Size pos = this_s.find(delim);
307 if (pos == std::string::npos)
312 return this_s.
substr(0, pos);
317 Size pos = this_s.rfind(delim);
318 if (pos == std::string::npos)
323 return this_s.
substr(++pos);
328 Size begin = std::min(pos, this_s.size());
329 return static_cast<String>(this_s.std::string::substr(begin, n));
335 if (n < this_s.size())
337 end = this_s.size() - n;
339 return String(this_s.begin(), this_s.begin() + end);
345 std::string::iterator begin = this_s.begin();
346 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
352 if (begin == this_s.end())
359 std::string::iterator end = this_s.end();
361 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
368 if (begin == this_s.begin() && end == this_s.end())
375 this_s.std::string::operator=(std::string(begin, end));
389 this_s.std::string::operator=(q + this_s + q);
396 if ((this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q))
399 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
400 "'" + this_s +
"' does not have the expected format of a quoted string");
402 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
417 bool last_was_whitespace =
false;
418 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
420 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
422 if (!last_was_whitespace)
426 last_was_whitespace =
true;
431 last_was_whitespace =
false;
441 srand(time(
nullptr));
444 for (
Size i = 0; i < length; ++i)
446 random =
static_cast<size_t>(floor((static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
449 tmp[i] =
static_cast<char>(random + 48);
451 else if (random < 36)
453 tmp[i] =
static_cast<char>(random + 55);
457 tmp[i] =
static_cast<char>(random + 61);
466 for (
Size i = 0; i != this_s.size(); ++i)
468 this_s[i] = tmp[this_s.size() - 1 - i];
473 static bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
480 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
482 if (!quote_protect && (nsplits == 0))
484 substrings.push_back(this_s);
489 substrings.reserve(nsplits + 1);
492 std::string::const_iterator begin = this_s.begin();
493 std::string::const_iterator end = this_s.begin();
498 for (; end != this_s.end(); ++end)
504 if ((quote_count % 2 == 0) && (*end == splitter))
508 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
513 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
514 String(
"Could not dequote string '") + block +
515 "' due to wrongly placed '\"'.");
517 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
520 block = block.
substr(1, block.size() - 2);
522 substrings.push_back(block);
527 if (substrings.empty())
529 substrings.push_back(this_s);
535 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
540 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
541 String(
"Could not dequote string '") + block +
542 "' due to wrongly placed '\"'.");
544 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
547 block = block.
substr(1, block.size() - 2);
549 substrings.push_back(block);
553 for (; end != this_s.end(); ++end)
555 if (*end == splitter)
557 substrings.push_back(
String(begin, end));
561 substrings.push_back(
String(begin, end));
568 static bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
574 if (splitter.empty())
576 substrings.resize(this_s.size());
577 for (
Size i = 0; i < this_s.size(); ++i)
578 substrings[i] = this_s[i];
582 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
585 while (pos != std::string::npos)
587 substrings.push_back(this_s.
substr(start, pos - start));
589 pos = this_s.find(splitter, start);
591 substrings.push_back(this_s.
substr(start, this_s.size() - start));
592 return substrings.size() > 1;
599 if (this_s.empty() || splitter.empty())
602 bool in_quote =
false;
603 char targets[2] = {q, splitter[0]};
604 std::string rest = splitter.
substr(1, splitter.size() - 1);
606 for (
Size i = 0; i < this_s.size(); ++i)
610 bool embedded =
false;
613 for (; i < this_s.size(); ++i)
615 if (this_s[i] ==
'\\')
616 embedded = !embedded;
617 else if ((this_s[i] == q) && !embedded)
625 for (; i < this_s.size(); ++i)
632 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
633 embedded = !embedded;
647 i = this_s.find_first_of(targets, i, 2);
648 if (i == std::string::npos)
652 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
654 substrings.push_back(this_s.
substr(start, i - start));
655 start = i + splitter.size();
663 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
664 "unbalanced quotation marks in string '" + this_s +
"'");
666 substrings.push_back(this_s.
substr(start, this_s.size() - start));
667 return substrings.size() > 1;
672 return QString(this_s.c_str());
682 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::int_, boost::spirit::ascii::space, ret))
687 if (it != this_s.end())
689 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"Prefix of string '") + this_s +
"' successfully converted to an integer value. Additional characters found at position " + (
int)(distance(this_s.begin(), it) + 1));
701 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_float_, boost::spirit::ascii::space, ret))
706 if (it != this_s.end())
708 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"Prefix of string '") + this_s +
"' successfully converted to a float value. Additional characters found at position " + (
int)(distance(this_s.begin(), it) + 1));
719 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_double_, boost::spirit::ascii::space, ret))
724 if (it != this_s.end())
726 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"Prefix of string '") + this_s +
"' successfully converted to a double value. Additional characters found at position " + (
int)(distance(this_s.begin(), it) + 1));
734 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(int))toupper);
740 if (this_s.size() != 0)
742 this_s[0] = toupper(this_s[0]);
749 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(int))tolower);
755 std::replace(this_s.begin(), this_s.end(), from, to);
763 std::vector<String> parts;
764 this_s.
split(from, parts);
765 this_s.
concatenate(parts.begin(), parts.end(), to);
772 this_s.erase(std::remove(this_s.begin(), this_s.end(), what), this_s.end());
779 this_s.append(1, end);
785 std::string::const_iterator it = this_s.begin();
786 std::string::iterator dest = this_s.begin();
787 std::string::const_iterator it_end = this_s.end();
788 bool has_spaces(
false);
792 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
799 if (has_spaces) *dest = *it;
806 if (has_spaces) this_s.resize(dest - this_s.begin());
820 template <
typename T>
823 template <
typename Iterator,
typename Attribute>
825 parse_nan(Iterator& first, Iterator
const& last, Attribute& attr_)
830 if (*first !=
'n' && *first !=
'N')
834 if (boost::spirit::qi::detail::string_parse(
"nan",
"NAN", first, last, boost::spirit::qi::unused))
836 if (first != last && *first ==
'(')
841 while (++i != last && *i !=
')')
848 attr_ = std::numeric_limits<T>::quiet_NaN();
857 static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> >
parse_double_;
858 static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> >
parse_float_;
static bool split(const String &this_s, const String &splitter, std::vector< String > &substrings)
Definition: StringUtils.h:568
static boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:858
Int underflow exception.
Definition: Exception.h:216
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtils.h:144
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtils.h:194
A more convenient string class.
Definition: String.h:57
static String number(double d, UInt n)
Definition: StringUtils.h:180
Definition: StringUtils.h:136
Element could not be found exception.
Definition: Exception.h:662
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtils.h:185
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtils.h:224
Int overflow exception.
Definition: Exception.h:254
String floatToString(T f)
toString functions (for floating point types)
Definition: StringUtils.h:58
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
Int writtenDigits(const FloatingPointType &=FloatingPointType())
Number of digits commonly used for writing a floating point type (a.k.a. precision). Specializations are defined for float, double, long double.
Definition: Types.h:294
static String & removeWhitespaces(String &this_s)
Definition: StringUtils.h:783
static QString toQString(const String &this_s)
Definition: StringUtils.h:670
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:458
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
static String & trim(String &this_s)
Definition: StringUtils.h:342
Definition: StringUtils.h:821
static String & substitute(String &this_s, char from, char to)
Definition: StringUtils.h:753
String toString() const
Conversion to String.
static String suffix(const String &this_s, Int length)
Definition: StringUtils.h:291
static double toDouble(const String &this_s)
Definition: StringUtils.h:713
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtils.h:473
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...
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtils.h:595
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:857
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:80
const_iterator ConstIterator
Const Iterator.
Definition: String.h:71
static bool parse_nan(Iterator &first, Iterator const &last, Attribute &attr_)
Definition: StringUtils.h:825
static String prefix(const String &this_s, char delim)
Definition: StringUtils.h:304
static String & toLower(String &this_s)
Definition: StringUtils.h:747
String suffix(SizeType length) const
returns the suffix of length length
static String random(UInt length)
Definition: StringUtils.h:439
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtils.h:326
static String prefix(const String &this_s, Int length)
Definition: StringUtils.h:278
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
static String suffix(const String &this_s, size_t length)
Definition: StringUtils.h:269
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtils.h:250
static String chop(const String &this_s, Size n)
Definition: StringUtils.h:332
static String & toUpper(String &this_s)
Definition: StringUtils.h:732
static String & substitute(String &this_s, const String &from, const String &to)
Definition: StringUtils.h:759
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtils.h:776
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:380
Invalid conversion exception.
Definition: Exception.h:362
static String & firstToUpper(String &this_s)
Definition: StringUtils.h:738
static bool has(const String &this_s, Byte byte)
Definition: StringUtils.h:255
static String & reverse(String &this_s)
Definition: StringUtils.h:463
static String suffix(const String &this_s, char delim)
Definition: StringUtils.h:315
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
static float toFloat(const String &this_s)
Definition: StringUtils.h:694
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:393
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
String prefix(SizeType length) const
returns the prefix of length length
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:111
static String prefix(const String &this_s, size_t length)
Definition: StringUtils.h:260
static String & simplify(String &this_s)
Definition: StringUtils.h:413
String toString(T i)
toString functions (single argument)
Definition: StringUtils.h:68
int Int
Signed integer type.
Definition: Types.h:102
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
static Int toInt(const String &this_s)
Definition: StringUtils.h:675
String toString< const char >(const char c)
Definition: StringUtils.h:76
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtils.h:237