43 #include <QtCore/QString>
44 #include <boost/spirit/include/qi.hpp>
45 #include <boost/spirit/include/karma.hpp>
46 #include <boost/type_traits.hpp>
56 namespace StringConversions
91 return writtenDigits<T>();
97 if (boost::spirit::traits::test_zero(n))
98 return base_policy_type::fmtflags::fixed;
100 T abs_n = boost::spirit::traits::get_absolute_value(n);
103 return (abs_n >= 1e4 || abs_n < 1e-2)
104 ? base_policy_type::fmtflags::scientific : base_policy_type::fmtflags::fixed;
118 template <
typename T>
121 std::back_insert_iterator<std::string> sink(target);
122 boost::spirit::karma::generate(sink, i);
126 template <
typename T>
142 std::back_insert_iterator<std::string> sink(target);
143 boost::spirit::karma::generate(sink, f);
158 std::back_insert_iterator<std::string> sink(target);
159 boost::spirit::karma::generate(sink, d);
173 std::back_insert_iterator<std::string> sink(target);
174 boost::spirit::karma::generate(sink, ld);
189 std::back_insert_iterator<std::string> sink(target);
205 std::back_insert_iterator<std::string> sink(target);
220 std::back_insert_iterator<std::string> sink(target);
234 target += d.
toString(full_precision);
245 return std::string(1,
c);
255 return std::string(s);
268 while (count < length)
294 if (d < pow(10.0,
Int(n - sign - 2)))
304 while (d > pow(10.0,
Int(n - sign - 4)))
318 return s.str().substr(0, n);
323 return QString::number(d,
'f', n);
328 if (this_s.size() < size)
330 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
337 if (this_s.size() < size)
339 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
347 if (
string.size() > this_s.size())
355 return this_s.compare(0,
string.size(),
string) == 0;
360 if (
string.size() > this_s.size())
368 return this_s.compare(this_s.size() -
string.size(),
string.size(),
string) == 0;
373 return this_s.find(
string) != std::string::npos;
378 return this_s.find(
char(
byte)) != std::string::npos;
383 if (length > this_s.size())
387 return this_s.
substr(0, length);
392 if (length > this_s.size())
396 return this_s.
substr(this_s.size() - length, length);
405 if (length >
Int(this_s.size()))
409 return this_s.
substr(0, length);
418 if (length >
Int(this_s.size()))
422 return this_s.
substr(this_s.size() - length, length);
427 Size pos = this_s.find(delim);
428 if (pos == std::string::npos)
433 return this_s.
substr(0, pos);
438 Size pos = this_s.rfind(delim);
439 if (pos == std::string::npos)
444 return this_s.
substr(++pos);
449 Size begin = std::min(pos, this_s.size());
450 return static_cast<String>(this_s.std::string::substr(begin, n));
456 if (n < this_s.size())
458 end = this_s.size() - n;
460 return String(this_s.begin(), this_s.begin() + end);
466 std::string::iterator begin = this_s.begin();
467 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
473 if (begin == this_s.end())
480 std::string::iterator end = this_s.end();
482 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
489 if (begin == this_s.begin() && end == this_s.end())
496 this_s.std::string::operator=(std::string(begin, end));
510 this_s.std::string::operator=(q + this_s + q);
517 if ((this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q))
520 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
521 "'" + this_s +
"' does not have the expected format of a quoted string");
523 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
538 bool last_was_whitespace =
false;
539 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
541 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
543 if (!last_was_whitespace)
547 last_was_whitespace =
true;
552 last_was_whitespace =
false;
562 srand(time(
nullptr));
565 for (
Size i = 0; i < length; ++i)
567 random =
static_cast<size_t>(floor((
static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
570 tmp[i] =
static_cast<char>(random + 48);
572 else if (random < 36)
574 tmp[i] =
static_cast<char>(random + 55);
578 tmp[i] =
static_cast<char>(random + 61);
587 for (
Size i = 0; i != this_s.size(); ++i)
589 this_s[i] = tmp[this_s.size() - 1 - i];
594 static bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
601 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
603 if (!quote_protect && (nsplits == 0))
605 substrings.push_back(this_s);
610 substrings.reserve(nsplits + 1);
613 std::string::const_iterator begin = this_s.begin();
614 std::string::const_iterator end = this_s.begin();
619 for (; end != this_s.end(); ++end)
625 if ((quote_count % 2 == 0) && (*end == splitter))
629 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
634 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
635 String(
"Could not dequote string '") + block +
636 "' due to wrongly placed '\"'.");
638 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
641 block = block.
substr(1, block.size() - 2);
643 substrings.push_back(block);
648 if (substrings.empty())
650 substrings.push_back(this_s);
656 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
661 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
662 String(
"Could not dequote string '") + block +
663 "' due to wrongly placed '\"'.");
665 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
668 block = block.
substr(1, block.size() - 2);
670 substrings.push_back(block);
674 for (; end != this_s.end(); ++end)
676 if (*end == splitter)
678 substrings.push_back(
String(begin, end));
682 substrings.push_back(
String(begin, end));
689 static bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
695 if (splitter.empty())
697 substrings.resize(this_s.size());
698 for (
Size i = 0; i < this_s.size(); ++i)
699 substrings[i] = this_s[i];
703 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
706 while (pos != std::string::npos)
708 substrings.push_back(this_s.
substr(start, pos - start));
710 pos = this_s.find(splitter, start);
712 substrings.push_back(this_s.
substr(start, this_s.size() - start));
713 return substrings.size() > 1;
720 if (this_s.empty() || splitter.empty())
723 bool in_quote =
false;
724 char targets[2] = {q, splitter[0]};
725 std::string rest = splitter.
substr(1, splitter.size() - 1);
727 for (
Size i = 0; i < this_s.size(); ++i)
731 bool embedded =
false;
734 for (; i < this_s.size(); ++i)
736 if (this_s[i] ==
'\\')
737 embedded = !embedded;
738 else if ((this_s[i] == q) && !embedded)
746 for (; i < this_s.size(); ++i)
753 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
754 embedded = !embedded;
768 i = this_s.find_first_of(targets, i, 2);
769 if (i == std::string::npos)
773 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
775 substrings.push_back(this_s.
substr(start, i - start));
776 start = i + splitter.size();
784 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
785 "unbalanced quotation marks in string '" + this_s +
"'");
787 substrings.push_back(this_s.
substr(start, this_s.size() - start));
788 return substrings.size() > 1;
793 return QString(this_s.c_str());
803 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::int_, boost::spirit::ascii::space, ret))
808 if (it != this_s.end())
810 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));
822 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_float_, boost::spirit::ascii::space, ret))
827 if (it != this_s.end())
829 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));
847 if (!boost::spirit::qi::phrase_parse(it, s.end(), parse_double_, boost::spirit::ascii::space, ret))
854 throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
String(
"Prefix of string '") + s +
"' successfully converted to a double value. Additional characters found at position " + (
int)(distance(s.begin(), it) + 1));
863 template <
typename IteratorT>
864 static bool extractDouble(IteratorT& begin,
const IteratorT& end,
double& target)
870 return boost::spirit::qi::parse(begin, end, parse_double_, target);
876 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))toupper);
882 if (this_s.size() != 0)
884 this_s[0] = toupper(this_s[0]);
891 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (int (*)(
int))tolower);
897 std::replace(this_s.begin(), this_s.end(), from, to);
905 std::vector<String> parts;
906 this_s.
split(from, parts);
907 this_s.
concatenate(parts.begin(), parts.end(), to);
914 this_s.erase(std::remove(this_s.begin(), this_s.end(), what), this_s.end());
921 this_s.append(1, end);
927 std::string::const_iterator it = this_s.begin();
928 std::string::iterator dest = this_s.begin();
929 std::string::const_iterator it_end = this_s.end();
930 bool has_spaces(
false);
934 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\n' ||
c ==
'\r')
941 if (has_spaces) *dest = *it;
948 if (has_spaces) this_s.resize(dest - this_s.begin());
962 template <
typename T>
965 template <
typename Iterator,
typename Attribute>
972 if (*first !=
'n' && *first !=
'N')
976 if (boost::spirit::qi::detail::string_parse(
"nan",
"NAN", first, last, boost::spirit::qi::unused))
978 if (first != last && *first ==
'(')
983 while (++i != last && *i !=
')')
990 attr_ = std::numeric_limits<T>::quiet_NaN();
999 static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> >
parse_double_;
1000 static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> >
parse_float_;
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:59
String toString(bool full_precision=true) const
Conversion to String full_precision Controls number of fractional digits for all double types or list...
Invalid conversion exception.
Definition: Exception.h:356
Element could not be found exception.
Definition: Exception.h:670
Int overflow exception.
Definition: Exception.h:248
Int underflow exception.
Definition: Exception.h:210
Definition: StringUtils.h:62
static unsigned precision(T)
Definition: StringUtils.h:65
static unsigned floatfield(T n)
Definition: StringUtils.h:95
boost::spirit::karma::real_policies< T > base_policy_type
Definition: StringUtils.h:63
Definition: StringUtils.h:278
static String & substitute(String &this_s, const String &from, const String &to)
Definition: StringUtils.h:901
static double toDouble(const String &s)
convert String (leading and trailing whitespace allowed) to double
Definition: StringUtils.h:841
static String & toUpper(String &this_s)
Definition: StringUtils.h:874
static String chop(const String &this_s, Size n)
Definition: StringUtils.h:453
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:864
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtils.h:594
static QString toQString(const String &this_s)
Definition: StringUtils.h:791
static String & substitute(String &this_s, char from, char to)
Definition: StringUtils.h:895
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtils.h:345
static String suffix(const String &this_s, char delim)
Definition: StringUtils.h:436
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtils.h:326
static Int toInt(const String &this_s)
Definition: StringUtils.h:796
static float toFloat(const String &this_s)
Definition: StringUtils.h:815
static String & simplify(String &this_s)
Definition: StringUtils.h:534
static String & firstToUpper(String &this_s)
Definition: StringUtils.h:880
static String & toLower(String &this_s)
Definition: StringUtils.h:889
static String prefix(const String &this_s, char delim)
Definition: StringUtils.h:425
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:999
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtils.h:371
static boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:1000
static String suffix(const String &this_s, size_t length)
Definition: StringUtils.h:390
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtils.h:285
static String & trim(String &this_s)
Definition: StringUtils.h:463
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtils.h:358
static String suffix(const String &this_s, Int length)
Definition: StringUtils.h:412
static bool has(const String &this_s, Byte byte)
Definition: StringUtils.h:376
static String random(UInt length)
Definition: StringUtils.h:560
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtils.h:716
static String prefix(const String &this_s, Int length)
Definition: StringUtils.h:399
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtils.h:447
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtils.h:918
static String number(double d, UInt n)
Definition: StringUtils.h:321
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtils.h:335
static String & remove(String &this_s, char what)
Definition: StringUtils.h:912
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:514
static String prefix(const String &this_s, size_t length)
Definition: StringUtils.h:381
static String & removeWhitespaces(String &this_s)
Definition: StringUtils.h:925
static bool split(const String &this_s, const String &splitter, std::vector< String > &substrings)
Definition: StringUtils.h:689
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:501
static String & reverse(String &this_s)
Definition: StringUtils.h:584
A more convenient string class.
Definition: String.h:61
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
const_iterator ConstIterator
Const Iterator.
Definition: String.h:73
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:82
@ ESCAPE
Definition: String.h:82
@ DOUBLE
Definition: String.h:82
@ NONE
Definition: String.h:82
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:467
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
boost::spirit::karma::real_generator< long double, BK_PrecPolicy< long double > > BK_PrecPolicyLongDouble_type
Definition: StringUtils.h:111
boost::spirit::karma::real_generator< float, BK_PrecPolicy< float > > BK_PrecPolicyFloat_type
Definition: StringUtils.h:107
const BK_PrecPolicyLongDouble_type BK_PrecPolicyLongDouble
Definition: StringUtils.h:112
String toStringLowP(float f)
low precision (3 fractional digits) conversion to string (Karma default)
Definition: StringUtils.h:146
void appendLowP(float f, String &target)
Definition: StringUtils.h:140
const BK_PrecPolicyDouble_type BK_PrecPolicyDouble
Definition: StringUtils.h:110
String toString(const T &i)
fallback template for general purpose using Boost::Karma; more specializations below
Definition: StringUtils.h:127
void append(const T &i, String &target)
Definition: StringUtils.h:119
const BK_PrecPolicyFloat_type BK_PrecPolicyFloat
Definition: StringUtils.h:108
boost::spirit::karma::real_generator< double, BK_PrecPolicy< double > > BK_PrecPolicyDouble_type
Definition: StringUtils.h:109
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
Definition: StringUtils.h:964
static bool parse_nan(Iterator &first, Iterator const &last, Attribute &attr_)
Definition: StringUtils.h:967