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_;