35 #ifndef OPENMS_DATASTRUCTURES_STRINGUTILS_H 36 #define OPENMS_DATASTRUCTURES_STRINGUTILS_H 43 #include <QtCore/QString> 44 #include <boost/spirit/include/qi.hpp> 54 namespace StringConversions
79 return std::string(1,
c);
91 return std::string(s);
128 while (count < length && *(s + count) != 0)
154 if (d < pow(10.0,
Int(n - sign - 2)))
164 while (d > pow(10.0,
Int(n - sign - 4)))
178 return s.str().substr(0, n);
183 return QString::number(d,
'f', n);
188 if (this_s.size() < size)
190 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
197 if (this_s.size() < size)
199 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
227 if (
string.size() > this_s.size())
235 return this_s.compare(0,
string.size(),
string) == 0;
240 if (
string.size() > this_s.size())
248 return this_s.compare(this_s.size() -
string.size(),
string.size(), string) == 0;
253 return this_s.find(
string) != std::string::npos;
258 return this_s.find(
char(byte)) != std::string::npos;
263 if (length > this_s.size())
267 return this_s.
substr(0, length);
272 if (length > this_s.size())
276 return this_s.
substr(this_s.size() - length, length);
285 if (length >
Int(this_s.size()))
289 return this_s.
substr(0, length);
298 if (length >
Int(this_s.size()))
302 return this_s.
substr(this_s.size() - length, length);
307 Size pos = this_s.find(delim);
308 if (pos == std::string::npos)
313 return this_s.
substr(0, pos);
318 Size pos = this_s.rfind(delim);
319 if (pos == std::string::npos)
324 return this_s.
substr(++pos);
329 Size begin = std::min(pos, this_s.size());
330 return static_cast<String>(this_s.std::string::substr(begin, n));
336 if (n < this_s.size())
338 end = this_s.size() - n;
340 return String(this_s.begin(), this_s.begin() + end);
346 std::string::iterator begin = this_s.begin();
347 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
353 if (begin == this_s.end())
360 std::string::iterator end = this_s.end();
362 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
369 if (begin == this_s.begin() && end == this_s.end())
376 this_s.std::string::operator=(std::string(begin, end));
390 this_s.std::string::operator=(q + this_s + q);
397 if ((this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q))
400 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
401 "'" + this_s +
"' does not have the expected format of a quoted string");
403 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
418 bool last_was_whitespace =
false;
419 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
421 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
423 if (!last_was_whitespace)
427 last_was_whitespace =
true;
432 last_was_whitespace =
false;
445 for (
Size i = 0; i < length; ++i)
447 random =
static_cast<size_t>(floor((static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
450 tmp[i] =
static_cast<char>(random + 48);
452 else if (random < 36)
454 tmp[i] =
static_cast<char>(random + 55);
458 tmp[i] =
static_cast<char>(random + 61);
467 for (
Size i = 0; i != this_s.size(); ++i)
469 this_s[i] = tmp[this_s.size() - 1 - i];
474 static bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
481 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
483 if (!quote_protect && (nsplits == 0))
485 substrings.push_back(this_s);
490 substrings.reserve(nsplits + 1);
493 std::string::const_iterator begin = this_s.begin();
494 std::string::const_iterator end = this_s.begin();
499 for (; end != this_s.end(); ++end)
505 if ((quote_count % 2 == 0) && (*end == splitter))
509 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
514 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
515 String(
"Could not dequote string '") + block +
516 "' due to wrongly placed '\"'.");
518 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
521 block = block.
substr(1, block.size() - 2);
523 substrings.push_back(block);
528 if (substrings.empty())
530 substrings.push_back(this_s);
536 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
541 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
542 String(
"Could not dequote string '") + block +
543 "' due to wrongly placed '\"'.");
545 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
548 block = block.
substr(1, block.size() - 2);
550 substrings.push_back(block);
554 for (; end != this_s.end(); ++end)
556 if (*end == splitter)
558 substrings.push_back(
String(begin, end));
562 substrings.push_back(
String(begin, end));
569 static bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
575 if (splitter.empty())
577 substrings.resize(this_s.size());
578 for (
Size i = 0; i < this_s.size(); ++i)
579 substrings[i] = this_s[i];
583 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
586 while (pos != std::string::npos)
588 substrings.push_back(this_s.
substr(start, pos - start));
590 pos = this_s.find(splitter, start);
592 substrings.push_back(this_s.
substr(start, this_s.size() - start));
593 return substrings.size() > 1;
600 if (this_s.empty() || splitter.empty())
603 bool in_quote =
false;
604 char targets[2] = {q, splitter[0]};
605 std::string rest = splitter.
substr(1, splitter.size() - 1);
607 for (
Size i = 0; i < this_s.size(); ++i)
611 bool embedded =
false;
614 for (; i < this_s.size(); ++i)
616 if (this_s[i] ==
'\\')
617 embedded = !embedded;
618 else if ((this_s[i] == q) && !embedded)
626 for (; i < this_s.size(); ++i)
633 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
634 embedded = !embedded;
648 i = this_s.find_first_of(targets, i, 2);
649 if (i == std::string::npos)
653 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
655 substrings.push_back(this_s.
substr(start, i - start));
656 start = i + splitter.size();
664 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
665 "unbalanced quotation marks in string '" + this_s +
"'");
667 substrings.push_back(this_s.
substr(start, this_s.size() - start));
668 return substrings.size() > 1;
673 return QString(this_s.c_str());
683 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::int_, boost::spirit::ascii::space, ret))
688 if (it != this_s.end())
690 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));
702 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_float_, boost::spirit::ascii::space, ret))
707 if (it != this_s.end())
709 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));
720 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_double_, boost::spirit::ascii::space, ret))
725 if (it != this_s.end())
727 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));
735 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(int))toupper);
741 if (this_s.size() != 0)
743 this_s[0] = toupper(this_s[0]);
750 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(int))tolower);
756 std::replace(this_s.begin(), this_s.end(), from, to);
764 std::vector<String> parts;
765 this_s.
split(from, parts);
766 this_s.
concatenate(parts.begin(), parts.end(), to);
773 this_s.erase(std::remove(this_s.begin(), this_s.end(), what), this_s.end());
780 this_s.append(1, end);
786 bool contains_ws =
false;
787 for (std::string::const_iterator it = this_s.begin(); it != this_s.end(); ++it)
790 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
800 tmp.reserve(this_s.size());
801 for (std::string::const_iterator it = this_s.begin(); it != this_s.end(); ++it)
804 if (c !=
' ' && c !=
'\t' && c !=
'\n' && c !=
'\r')
822 template <
typename T>
825 template <
typename Iterator,
typename Attribute>
827 parse_nan(Iterator& first, Iterator
const& last, Attribute& attr_)
832 if (*first !=
'n' && *first !=
'N')
836 if (boost::spirit::qi::detail::string_parse(
"nan",
"NAN", first, last, boost::spirit::qi::unused))
838 if (first != last && *first ==
'(')
843 while (++i != last && *i !=
')')
850 attr_ = std::numeric_limits<T>::quiet_NaN();
859 static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> >
parse_double_;
860 static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> >
parse_float_;
866 #endif // OPENMS_DATASTRUCTURES_STRINGUTILS_H static bool split(const String &this_s, const String &splitter, std::vector< String > &substrings)
Definition: StringUtils.h:569
static boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:860
Int underflow exception.
Definition: Exception.h:217
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtils.h:145
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtils.h:195
A more convenient string class.
Definition: String.h:57
static String number(double d, UInt n)
Definition: StringUtils.h:181
Definition: StringUtils.h:137
Element could not be found exception.
Definition: Exception.h:663
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtils.h:186
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtils.h:225
Int overflow exception.
Definition: Exception.h:255
String floatToString(T f)
toString functions (for floating point types)
Definition: StringUtils.h:59
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
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:295
static String & removeWhitespaces(String &this_s)
Definition: StringUtils.h:784
static QString toQString(const String &this_s)
Definition: StringUtils.h:671
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:47
static String & trim(String &this_s)
Definition: StringUtils.h:343
Definition: StringUtils.h:823
static String & substitute(String &this_s, char from, char to)
Definition: StringUtils.h:754
String toString() const
Conversion to String.
static String suffix(const String &this_s, Int length)
Definition: StringUtils.h:292
static double toDouble(const String &this_s)
Definition: StringUtils.h:714
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:57
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtils.h:474
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:596
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:859
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:827
static String prefix(const String &this_s, char delim)
Definition: StringUtils.h:305
static String & toLower(String &this_s)
Definition: StringUtils.h:748
String suffix(SizeType length) const
returns the suffix of length length
static String random(UInt length)
Definition: StringUtils.h:440
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtils.h:327
static String prefix(const String &this_s, Int length)
Definition: StringUtils.h:279
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:270
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtils.h:251
static String chop(const String &this_s, Size n)
Definition: StringUtils.h:333
static String & toUpper(String &this_s)
Definition: StringUtils.h:733
static String & substitute(String &this_s, const String &from, const String &to)
Definition: StringUtils.h:760
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtils.h:777
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:381
Invalid conversion exception.
Definition: Exception.h:363
static String & firstToUpper(String &this_s)
Definition: StringUtils.h:739
static bool has(const String &this_s, Byte byte)
Definition: StringUtils.h:256
static String & reverse(String &this_s)
Definition: StringUtils.h:464
static String suffix(const String &this_s, char delim)
Definition: StringUtils.h:316
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
static float toFloat(const String &this_s)
Definition: StringUtils.h:695
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:394
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:112
static String prefix(const String &this_s, size_t length)
Definition: StringUtils.h:261
static String & simplify(String &this_s)
Definition: StringUtils.h:414
String toString(T i)
toString functions (single argument)
Definition: StringUtils.h:69
int Int
Signed integer type.
Definition: Types.h:103
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:676
String toString< const char >(const char c)
Definition: StringUtils.h:77
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:238