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