OpenMS  3.0.0
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-2022.
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 
167  template <typename IteratorT>
168  static bool extractInt(IteratorT& begin, const IteratorT& end, int& target)
169  {
170  // qi::parse() does not consume whitespace before or after the int (qi::parse_phrase() would).
171  return boost::spirit::qi::parse(begin, end, parse_int_, target);
172  }
173 
174  private:
175 
176  /*
177  @brief A fixed Boost:pi real parser policy, capable of dealing with 'nan' without crashing
178 
179  The original Boost implementation has a bug, see https://svn.boost.org/trac/boost/ticket/6955.
180  Can be removed if Boost 1.60 or above is required
181 
182  */
183  template <typename T>
184  struct real_policies_NANfixed_ : boost::spirit::qi::real_policies<T>
185  {
186  template <typename Iterator, typename Attribute>
187  static bool
188  parse_nan(Iterator& first, Iterator const& last, Attribute& attr_)
189  {
190  if (first == last)
191  return false; // end of input reached
192 
193  if (*first != 'n' && *first != 'N')
194  return false; // not "nan"
195 
196  // nan[(...)] ?
197  if (boost::spirit::qi::detail::string_parse("nan", "NAN", first, last, boost::spirit::qi::unused))
198  {
199  if (first != last && *first == '(') /* this check is broken in boost 1.49 - (at least) 1.54; fixed in 1.60 */
200  {
201  // skip trailing (...) part
202  Iterator i = first;
203 
204  while (++i != last && *i != ')')
205  ;
206  if (i == last)
207  return false; // no trailing ')' found, give up
208 
209  first = ++i;
210  }
211  attr_ = std::numeric_limits<T>::quiet_NaN();
212  return true;
213  }
214  return false;
215  }
216  };
217 
218  // Qi parsers using the 'real_policies_NANfixed_' template which allows for 'nan'
219  // (the original Boost implementation has a bug, see https://svn.boost.org/trac/boost/ticket/6955)
220  static boost::spirit::qi::real_parser<double, real_policies_NANfixed_<double> > parse_double_;
221  static boost::spirit::qi::real_parser<float, real_policies_NANfixed_<float> > parse_float_;
222  static boost::spirit::qi::int_parser<> parse_int_;
223 
224  };
225 
226  namespace StringUtils
227  {
228 
229  [[maybe_unused]] static String number(double d, UInt n)
230  {
231  return QString::number(d, 'f', n);
232  }
233 
234  [[maybe_unused]] static QString toQString(const String & this_s)
235  {
236  return QString(this_s.c_str());
237  }
238 
239  [[maybe_unused]] static Int32 toInt32(const String & this_s)
240  {
241  return StringUtilsHelper::toInt32(this_s);
242  }
243 
244  [[maybe_unused]] static Int64 toInt64(const String& this_s)
245  {
246  return StringUtilsHelper::toInt64(this_s);
247  }
248 
249  [[maybe_unused]] static float toFloat(const String & this_s)
250  {
251  return StringUtilsHelper::toFloat(this_s);
252  }
253 
254  [[maybe_unused]] static double toDouble(const String & this_s)
255  {
256  return StringUtilsHelper::toDouble(this_s);
257  }
258 
259  template <typename IteratorT>
260  static bool extractDouble(IteratorT& begin, const IteratorT& end, double& target)
261  {
262  return StringUtilsHelper::extractDouble(begin, end, target);
263  }
264 
265  template <typename IteratorT>
266  static bool extractInt(IteratorT& begin, const IteratorT& end, int& target)
267  {
268  return StringUtilsHelper::extractInt(begin, end, target);
269  }
270  }
271 } // namespace OPENMS
272 
static Int32 toInt32(const String &this_s)
Definition: StringUtils.h:239
static boost::spirit::qi::real_parser< float, real_policies_NANfixed_< float > > parse_float_
Definition: StringUtils.h:221
A more convenient string class.
Definition: String.h:58
static bool parse_nan(Iterator &first, Iterator const &last, Attribute &attr_)
Definition: StringUtils.h:188
Definition: StringUtils.h:57
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:154
static double toDouble(const String &s)
convert String (leading and trailing whitespace allowed) to double
Definition: StringUtils.h:131
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
static boost::spirit::qi::real_parser< double, real_policies_NANfixed_< double > > parse_double_
Definition: StringUtils.h:220
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static Int64 toInt64(const String &this_s)
Definition: StringUtils.h:84
const_iterator ConstIterator
Const Iterator.
Definition: String.h:72
static bool extractInt(IteratorT &begin, const IteratorT &end, int &target)
Definition: StringUtils.h:266
static Int toInt32(const String &this_s)
Functions.
Definition: StringUtils.h:65
static Int64 toInt64(const String &this_s)
Definition: StringUtils.h:244
OPENMS_INT32_TYPE Int32
Signed integer type (32bit)
Definition: Types.h:56
static QString toQString(const String &this_s)
Definition: StringUtils.h:234
Invalid conversion exception.
Definition: Exception.h:354
static String number(double d, UInt n)
Definition: StringUtils.h:229
static float toFloat(const String &this_s)
Definition: StringUtils.h:249
static boost::spirit::qi::int_parser parse_int_
Definition: StringUtils.h:222
OPENMS_INT64_TYPE Int64
Signed integer type (64bit)
Definition: Types.h:70
static float toFloat(const String &this_s)
Definition: StringUtils.h:105
static double toDouble(const String &this_s)
Definition: StringUtils.h:254
static bool extractInt(IteratorT &begin, const IteratorT &end, int &target)
Definition: StringUtils.h:168
static bool extractDouble(IteratorT &begin, const IteratorT &end, double &target)
Definition: StringUtils.h:260
int Int
Signed integer type.
Definition: Types.h:102