@brief convert String (leading and trailing whitespace allowed) to double @p s Input string which represents a double, e.g. " 12.3 " @return A double representation of @p s @throws Exception::ConversionError if the string is not completely explained by the double (whitespaces are allowed) */
| 103 | @throws Exception::ConversionError if the string is not completely explained by the double (whitespaces are allowed) |
| 104 | */ |
| 105 | static double toDouble(const String& s) |
| 106 | { |
| 107 | double ret; |
| 108 | // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio), |
| 109 | // so don't change this unless you have benchmarks for all platforms! |
| 110 | String::ConstIterator it = s.begin(); |
| 111 | if (!boost::spirit::qi::phrase_parse(it, s.end(), parse_double_, boost::spirit::ascii::space, ret)) |
| 112 | { |
| 113 | throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + s + "' to a double value"); |
| 114 | } |
| 115 | // was the string parsed (white spaces are skipped automatically!) completely? If not, we have a problem because a previous split might have used the wrong split char |
| 116 | if (it != s.end()) |
| 117 | { |
| 118 | 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)); |
| 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | /// Reads a double from an iterator position. |
| 124 | /// The begin iterator is modified (advanced) if parsing was successful. |
no test coverage detected