43 #include <QtCore/QString> 44 #include <boost/spirit/include/qi.hpp> 45 #include <boost/spirit/include/karma.hpp> 55 namespace StringConversions
63 static unsigned int precision(T) {
return writtenDigits<T>(T()); }
79 std::back_insert_iterator<std::string> sink(target);
80 boost::spirit::karma::generate(sink, i);
100 std::back_insert_iterator<std::string> sink(target);
101 boost::spirit::karma::generate(sink, f);
116 std::back_insert_iterator<std::string> sink(target);
117 boost::spirit::karma::generate(sink, d);
131 std::back_insert_iterator<std::string> sink(target);
132 boost::spirit::karma::generate(sink, ld);
147 std::back_insert_iterator<std::string> sink(target);
148 boost::spirit::karma::generate(sink, BK_PrecPolicyFloat, f);
163 std::back_insert_iterator<std::string> sink(target);
164 boost::spirit::karma::generate(sink, BK_PrecPolicyDouble, d);
178 std::back_insert_iterator<std::string> sink(target);
179 boost::spirit::karma::generate(sink, BK_PrecPolicyLongDouble, ld);
192 target += d.
toString(full_precision);
203 return std::string(1, c);
213 return std::string(s);
226 while (count < length)
252 if (d < pow(10.0,
Int(n - sign - 2)))
262 while (d > pow(10.0,
Int(n - sign - 4)))
276 return s.str().substr(0, n);
281 return QString::number(d,
'f', n);
286 if (this_s.size() < size)
288 this_s.std::string::operator=(
String(size - this_s.size(),
c) + this_s);
295 if (this_s.size() < size)
297 this_s.std::string::operator=(this_s +
String(size - this_s.size(),
c));
305 if (
string.size() > this_s.size())
313 return this_s.compare(0,
string.size(),
string) == 0;
318 if (
string.size() > this_s.size())
326 return this_s.compare(this_s.size() -
string.size(),
string.size(), string) == 0;
331 return this_s.find(
string) != std::string::npos;
336 return this_s.find(
char(byte)) != std::string::npos;
341 if (length > this_s.size())
345 return this_s.
substr(0, length);
350 if (length > this_s.size())
354 return this_s.
substr(this_s.size() - length, length);
363 if (length >
Int(this_s.size()))
367 return this_s.
substr(0, length);
376 if (length >
Int(this_s.size()))
380 return this_s.
substr(this_s.size() - length, length);
385 Size pos = this_s.find(delim);
386 if (pos == std::string::npos)
391 return this_s.
substr(0, pos);
396 Size pos = this_s.rfind(delim);
397 if (pos == std::string::npos)
402 return this_s.
substr(++pos);
407 Size begin = std::min(pos, this_s.size());
408 return static_cast<String>(this_s.std::string::substr(begin, n));
414 if (n < this_s.size())
416 end = this_s.size() - n;
418 return String(this_s.begin(), this_s.begin() + end);
424 std::string::iterator begin = this_s.begin();
425 while (begin != this_s.end() && (*begin ==
' ' || *begin ==
'\t' || *begin ==
'\n' || *begin ==
'\r'))
431 if (begin == this_s.end())
438 std::string::iterator end = this_s.end();
440 while (end != begin && (*end ==
' ' || *end ==
'\n' || *end ==
'\t' || *end ==
'\r'))
447 if (begin == this_s.begin() && end == this_s.end())
454 this_s.std::string::operator=(std::string(begin, end));
468 this_s.std::string::operator=(q + this_s + q);
475 if ((this_s.size() < 2) || (this_s[0] != q) || (this_s[this_s.size() - 1] != q))
478 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
479 "'" + this_s +
"' does not have the expected format of a quoted string");
481 this_s.std::string::operator=(this_s.
substr(1, this_s.size() - 2));
496 bool last_was_whitespace =
false;
497 for (std::string::iterator it = this_s.begin(); it != this_s.end(); ++it)
499 if (*it ==
' ' || *it ==
'\n' || *it ==
'\t' || *it ==
'\r')
501 if (!last_was_whitespace)
505 last_was_whitespace =
true;
510 last_was_whitespace =
false;
520 srand(time(
nullptr));
523 for (
Size i = 0; i < length; ++i)
525 random =
static_cast<size_t>(floor((static_cast<double>(rand()) / (
double(RAND_MAX) + 1)) * 62.0));
528 tmp[i] =
static_cast<char>(random + 48);
530 else if (random < 36)
532 tmp[i] =
static_cast<char>(random + 55);
536 tmp[i] =
static_cast<char>(random + 61);
545 for (
Size i = 0; i != this_s.size(); ++i)
547 this_s[i] = tmp[this_s.size() - 1 - i];
552 static bool split(
const String & this_s,
const char splitter, std::vector<String>& substrings,
559 Size nsplits = count(this_s.begin(), this_s.end(), splitter);
561 if (!quote_protect && (nsplits == 0))
563 substrings.push_back(this_s);
568 substrings.reserve(nsplits + 1);
571 std::string::const_iterator begin = this_s.begin();
572 std::string::const_iterator end = this_s.begin();
577 for (; end != this_s.end(); ++end)
583 if ((quote_count % 2 == 0) && (*end == splitter))
587 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
592 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
593 String(
"Could not dequote string '") + block +
594 "' due to wrongly placed '\"'.");
596 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
599 block = block.
substr(1, block.size() - 2);
601 substrings.push_back(block);
606 if (substrings.empty())
608 substrings.push_back(this_s);
614 if ((block.size() >= 2) && ((block.
prefix(1) ==
String(
"\"")) ^
619 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
620 String(
"Could not dequote string '") + block +
621 "' due to wrongly placed '\"'.");
623 else if ((block.size() >= 2) && (block.
prefix(1) ==
String(
"\"")) &&
626 block = block.
substr(1, block.size() - 2);
628 substrings.push_back(block);
632 for (; end != this_s.end(); ++end)
634 if (*end == splitter)
636 substrings.push_back(
String(begin, end));
640 substrings.push_back(
String(begin, end));
647 static bool split(
const String & this_s,
const String& splitter, std::vector<String>& substrings)
653 if (splitter.empty())
655 substrings.resize(this_s.size());
656 for (
Size i = 0; i < this_s.size(); ++i)
657 substrings[i] = this_s[i];
661 Size len = splitter.size(), start = 0, pos = this_s.find(splitter);
664 while (pos != std::string::npos)
666 substrings.push_back(this_s.
substr(start, pos - start));
668 pos = this_s.find(splitter, start);
670 substrings.push_back(this_s.
substr(start, this_s.size() - start));
671 return substrings.size() > 1;
678 if (this_s.empty() || splitter.empty())
681 bool in_quote =
false;
682 char targets[2] = {q, splitter[0]};
683 std::string rest = splitter.
substr(1, splitter.size() - 1);
685 for (
Size i = 0; i < this_s.size(); ++i)
689 bool embedded =
false;
692 for (; i < this_s.size(); ++i)
694 if (this_s[i] ==
'\\')
695 embedded = !embedded;
696 else if ((this_s[i] == q) && !embedded)
704 for (; i < this_s.size(); ++i)
711 if ((i < this_s.size() - 1) && (this_s[i + 1] == q))
712 embedded = !embedded;
726 i = this_s.find_first_of(targets, i, 2);
727 if (i == std::string::npos)
731 else if (this_s.compare(i + 1, rest.size(), rest) == 0)
733 substrings.push_back(this_s.
substr(start, i - start));
734 start = i + splitter.size();
742 __FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
743 "unbalanced quotation marks in string '" + this_s +
"'");
745 substrings.push_back(this_s.
substr(start, this_s.size() - start));
746 return substrings.size() > 1;
751 return QString(this_s.c_str());
761 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::int_, boost::spirit::ascii::space, ret))
766 if (it != this_s.end())
768 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));
780 if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_float_, boost::spirit::ascii::space, ret))
785 if (it != this_s.end())
787 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));
805 if (!boost::spirit::qi::phrase_parse(it, s.end(), parse_double_, boost::spirit::ascii::space, ret))
812 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));
821 template <
typename IteratorT>
822 static bool extractDouble(IteratorT& begin,
const IteratorT& end,
double& target)
828 return boost::spirit::qi::parse(begin, end, parse_double_, target);
834 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (
int (*)(
int))toupper);
840 if (this_s.size() != 0)
842 this_s[0] = toupper(this_s[0]);
849 std::transform(this_s.begin(), this_s.end(), this_s.begin(), (
int (*)(
int))tolower);
855 std::replace(this_s.begin(), this_s.end(), from, to);
863 std::vector<String> parts;
864 this_s.
split(from, parts);
865 this_s.
concatenate(parts.begin(), parts.end(), to);
872 this_s.erase(std::remove(this_s.begin(), this_s.end(), what), this_s.end());
879 this_s.append(1, end);
885 std::string::const_iterator it = this_s.begin();
886 std::string::iterator dest = this_s.begin();
887 std::string::const_iterator it_end = this_s.end();
888 bool has_spaces(
false);
892 if (c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r')
899 if (has_spaces) *dest = *it;
906 if (has_spaces) this_s.resize(dest - this_s.begin());
920 template <
typename T>
923 template <
typename Iterator,
typename Attribute>
930 if (*first !=
'n' && *first !=
'N')
934 if (boost::spirit::qi::detail::string_parse(
"nan",
"NAN", first, last, boost::spirit::qi::unused))
936 if (first != last && *first ==
'(')
941 while (++i != last && *i !=
')')
948 attr_ = std::numeric_limits<T>::quiet_NaN();
957 static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> >
parse_double_;
958 static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> >
parse_float_;
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:822
static String random(UInt length)
Definition: StringUtils.h:518
int Int
Signed integer type.
Definition: Types.h:102
static String & unquote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:472
static unsigned int precision(T)
Definition: StringUtils.h:63
static bool hasPrefix(const String &this_s, const String &string)
Definition: StringUtils.h:303
static String suffix(const String &this_s, char delim)
Definition: StringUtils.h:394
static String prefix(const String &this_s, size_t length)
Definition: StringUtils.h:339
static String suffix(const String &this_s, Int length)
Definition: StringUtils.h:370
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:111
static String prefix(const String &this_s, Int length)
Definition: StringUtils.h:357
static String & trim(String &this_s)
Definition: StringUtils.h:421
static String & firstToUpper(String &this_s)
Definition: StringUtils.h:838
String suffix(SizeType length) const
returns the suffix of length length
String toString(const T &i)
fallback template for general purpose using Boost::Karma; more specializations below ...
Definition: StringUtils.h:85
static String substr(const String &this_s, size_t pos, size_t n)
Definition: StringUtils.h:405
static String & quote(String &this_s, char q, String::QuotingMethod method)
Definition: StringUtils.h:459
const BK_PrecPolicyDouble_type BK_PrecPolicyDouble
Definition: StringUtils.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
String prefix(SizeType length) const
returns the prefix of length length
static String numberLength(double d, UInt n)
Functions.
Definition: StringUtils.h:243
static bool split(const String &this_s, const char splitter, std::vector< String > &substrings, bool quote_protect)
Definition: StringUtils.h:552
static String number(double d, UInt n)
Definition: StringUtils.h:279
void append(const T &i, String &target)
Definition: StringUtils.h:77
const_iterator ConstIterator
Const Iterator.
Definition: String.h:72
const BK_PrecPolicyFloat_type BK_PrecPolicyFloat
Definition: StringUtils.h:66
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
static String & toUpper(String &this_s)
Definition: StringUtils.h:832
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...
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
static String chop(const String &this_s, Size n)
Definition: StringUtils.h:411
constexpr 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 boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:958
Int underflow exception.
Definition: Exception.h:216
Definition: StringUtils.h:60
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
const BK_PrecPolicyLongDouble_type BK_PrecPolicyLongDouble
Definition: StringUtils.h:70
static String & toLower(String &this_s)
Definition: StringUtils.h:847
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
static String & removeWhitespaces(String &this_s)
Definition: StringUtils.h:883
static String suffix(const String &this_s, size_t length)
Definition: StringUtils.h:348
static double toDouble(const String &s)
convert String (leading and trailing whitespace allowed) to double
Definition: StringUtils.h:799
boost::spirit::karma::real_generator< long double, BK_PrecPolicy< long double > > BK_PrecPolicyLongDouble_type
Definition: StringUtils.h:69
static String & fillRight(String &this_s, char c, UInt size)
Definition: StringUtils.h:293
Element could not be found exception.
Definition: Exception.h:662
static float toFloat(const String &this_s)
Definition: StringUtils.h:773
static QString toQString(const String &this_s)
Definition: StringUtils.h:749
String toString(bool full_precision=true) const
Conversion to String full_precision Controls number of fractional digits for all double types or list...
String toStringLowP(float f)
low precision (3 fractional digits) conversion to string (Karma default)
Definition: StringUtils.h:104
static bool split(const String &this_s, const String &splitter, std::vector< String > &substrings)
Definition: StringUtils.h:647
void appendLowP(float f, String &target)
Definition: StringUtils.h:98
static bool hasSuffix(const String &this_s, const String &string)
Definition: StringUtils.h:316
static bool split_quoted(const String &this_s, const String &splitter, std::vector< String > &substrings, char q, String::QuotingMethod method)
Definition: StringUtils.h:674
Int overflow exception.
Definition: Exception.h:254
Definition: StringUtils.h:921
static String & simplify(String &this_s)
Definition: StringUtils.h:492
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
static bool hasSubstring(const String &this_s, const String &string)
Definition: StringUtils.h:329
boost::spirit::karma::real_generator< float, BK_PrecPolicy< float > > BK_PrecPolicyFloat_type
Definition: StringUtils.h:65
A more convenient string class.
Definition: String.h:58
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:81
boost::spirit::karma::real_generator< double, BK_PrecPolicy< double > > BK_PrecPolicyDouble_type
Definition: StringUtils.h:67
static bool parse_nan(Iterator &first, Iterator const &last, Attribute &attr_)
Definition: StringUtils.h:925
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
static String & reverse(String &this_s)
Definition: StringUtils.h:542
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:957
static String & substitute(String &this_s, const String &from, const String &to)
Definition: StringUtils.h:859
Definition: StringUtils.h:235
static String & substitute(String &this_s, char from, char to)
Definition: StringUtils.h:853
static String & ensureLastChar(String &this_s, char end)
Definition: StringUtils.h:876
static Int toInt(const String &this_s)
Definition: StringUtils.h:754
static bool has(const String &this_s, Byte byte)
Definition: StringUtils.h:334
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Invalid conversion exception.
Definition: Exception.h:362
static String prefix(const String &this_s, char delim)
Definition: StringUtils.h:383
static String & fillLeft(String &this_s, char c, UInt size)
Definition: StringUtils.h:284