OpenMS
StringUtils.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg, Chris Bielow $
32 // $Authors: Marc Sturm, Stephan Aiche, Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
43 
44 #include <QtCore/QString>
45 #include <boost/spirit/include/qi.hpp>
46 #include <boost/spirit/include/karma.hpp>
47 #include <boost/type_traits.hpp>
48 
49 #include <string>
50 #include <vector>
51 
52 
53 namespace OpenMS
54 {
55  class String;
56 
57  class OPENMS_DLLAPI StringUtilsHelper
58  {
59 
60 public:
61 
62  //
64  //
65  static Int toInt32(const String& this_s)
66  {
67  Int ret;
68 
69  // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio),
70  // so don't change this unless you have benchmarks for all platforms!
71  String::ConstIterator it = this_s.begin();
72  if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::int_, boost::spirit::ascii::space, ret))
73  {
74  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + this_s + "' to an integer value");
75  }
76  // 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
77  if (it != this_s.end())
78  {
79  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Prefix of string '") + this_s + "' successfully converted to an int32 value. Additional characters found at position " + (int)(distance(this_s.begin(), it) + 1));
80  }
81  return ret;
82  }
83 
84  static Int64 toInt64(const String& this_s)
85  {
86  Int64 ret;
87 
88  // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio),
89  // so don't change this unless you have benchmarks for all platforms!
90  String::ConstIterator it = this_s.begin();
91  if (!boost::spirit::qi::phrase_parse(it, this_s.end(), boost::spirit::qi::long_long, boost::spirit::ascii::space, ret))
92  {
93  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + this_s + "' to an int64 value");
94  }
95  // 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
96  if (it != this_s.end())
97  {
98  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
99  String("Prefix of string '") + this_s + "' successfully converted to an integer value. Additional characters found at position " +
100  (int)(distance(this_s.begin(), it) + 1));
101  }
102  return ret;
103  }
104 
105  static float toFloat(const String& this_s)
106  {
107  float ret;
108 
109  // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio),
110  // so don't change this unless you have benchmarks for all platforms!
111  String::ConstIterator it = this_s.begin();
112  if (!boost::spirit::qi::phrase_parse(it, this_s.end(), parse_float_, boost::spirit::ascii::space, ret))
113  {
114  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + this_s + "' to a float value");
115  }
116  // 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
117  if (it != this_s.end())
118  {
119  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));
120  }
121  return ret;
122  }
123 
131  static double toDouble(const String& s)
132  {
133  double ret;
134  // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio),
135  // so don't change this unless you have benchmarks for all platforms!
136  String::ConstIterator it = s.begin();
137  if (!boost::spirit::qi::phrase_parse(it, s.end(), parse_double_, boost::spirit::ascii::space, ret))
138  {
139  throw Exception::ConversionError(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Could not convert string '") + s + "' to a double value");
140  }
141  // 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
142  if (it != s.end())
143  {
144  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));
145  }
146  return ret;
147  }
148 
153  template <typename IteratorT>
154  static bool extractDouble(IteratorT& begin, const IteratorT& end, double& target)
155  {
156  // boost::spirit::qi was found to be vastly superior to boost::lexical_cast or stringstream extraction (especially for VisualStudio),
157  // so don't change this unless you have benchmarks for all platforms!
158 
159  // qi::parse() does not consume whitespace before or after the double (qi::parse_phrase() would).
160  return boost::spirit::qi::parse(begin, end, parse_double_, target);
161  }
162 
163  private:
164 
165  /*
166  @brief A fixed Boost:pi real parser policy, capable of dealing with 'nan' without crashing
167 
168  The original Boost implementation has a bug, see https://svn.boost.org/trac/boost/ticket/6955.
169  Can be removed if Boost 1.60 or above is required
170 
171  */
172  template <typename T>
174  {
175  template <typename Iterator, typename Attribute>
176  static bool
177  parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
178  {
179  if (first == last)
180  return false; // end of input reached
181 
182  if (*first != 'n' && *first != 'N')
183  return false; // not "nan"
184 
185  // nan[(...)] ?
186  if (boost::spirit::qi::detail::string_parse("nan", "NAN", first, last, boost::spirit::qi::unused))
187  {
188  if (first != last && *first == '(') /* this check is broken in boost 1.49 - (at least) 1.54; fixed in 1.60 */
189  {
190  // skip trailing (...) part
191  Iterator i = first;
192 
193  while (++i != last && *i != ')')
194  ;
195  if (i == last)
196  return false; // no trailing ')' found, give up
197 
198  first = ++i;
199  }
200  attr_ = std::numeric_limits<T>::quiet_NaN();
201  return true;
202  }
203  return false;
204  }
205  };
206 
207  // Qi parsers using the 'real_policies_NANfixed_' template which allows for 'nan'
208  // (the original Boost implementation has a bug, see https://svn.boost.org/trac/boost/ticket/6955)
209  static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> > parse_double_;
210  static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> > parse_float_;
211 
212  };
213 
214  namespace StringUtils
215  {
216 
217  [[maybe_unused]] static String number(double d, UInt n)
218  {
219  return QString::number(d, 'f', n);
220  }
221 
222  [[maybe_unused]] static QString toQString(const String & this_s)
223  {
224  return QString(this_s.c_str());
225  }
226 
227  [[maybe_unused]] static Int32 toInt32(const String & this_s)
228  {
229  return StringUtilsHelper::toInt32(this_s);
230  }
231 
232  [[maybe_unused]] static Int64 toInt64(const String& this_s)
233  {
234  return StringUtilsHelper::toInt64(this_s);
235  }
236 
237  [[maybe_unused]] static float toFloat(const String & this_s)
238  {
239  return StringUtilsHelper::toFloat(this_s);
240  }
241 
242  [[maybe_unused]] static double toDouble(const String & this_s)
243  {
244  return StringUtilsHelper::toDouble(this_s);
245  }
246 
247  template <typename IteratorT>
248  static bool extractDouble(IteratorT& begin, const IteratorT& end, double& target)
249  {
250  return StringUtilsHelper::extractDouble(begin, end, target);
251  }
252 
253  }
254 } // namespace OPENMS
255 
Invalid conversion exception.
Definition: Exception.h:356
Definition: StringUtils.h:58
static double toDouble(const String &s)
convert String (leading and trailing whitespace allowed) to double
Definition: StringUtils.h:131
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:154
static Int64 toInt64(const String &this_s)
Definition: StringUtils.h:84
static float toFloat(const String &this_s)
Definition: StringUtils.h:105
static Int toInt32(const String &this_s)
Functions.
Definition: StringUtils.h:65
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:209
static boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:210
A more convenient string class.
Definition: String.h:60
const_iterator ConstIterator
Const Iterator.
Definition: String.h:72
int Int
Signed integer type.
Definition: Types.h:102
OPENMS_INT32_TYPE Int32
Signed integer type (32bit)
Definition: Types.h:56
OPENMS_INT64_TYPE Int64
Signed integer type (64bit)
Definition: Types.h:70
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:248
static String number(double d, UInt n)
Definition: StringUtils.h:217
static Int32 toInt32(const String &this_s)
Definition: StringUtils.h:227
static Int64 toInt64(const String &this_s)
Definition: StringUtils.h:232
static double toDouble(const String &this_s)
Definition: StringUtils.h:242
static float toFloat(const String &this_s)
Definition: StringUtils.h:237
static QString toQString(const String &this_s)
Definition: StringUtils.h:222
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
static bool parse_nan(Iterator &first, Iterator const &last, Attribute &attr_)
Definition: StringUtils.h:177