string_conv.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_STRING_CONV_H
24 #define O2SCL_STRING_CONV_H
25 /** \file string_conv.h
26  \brief Various string conversion functions
27 */
28 
29 #include <iostream>
30 #include <cmath>
31 #include <string>
32 #include <vector>
33 #include <fstream>
34 #include <sstream>
35 
36 // For screenify()
37 #include <o2scl/misc.h>
38 
39 #ifndef DOXYGEN_NO_O2NS
40 namespace o2scl {
41 #endif
42 
43  /// \name Functions in string_conv.h
44  //@{
45  /** \brief Convert a pointer to a string
46 
47  This uses an \c ostringstream to convert a pointer to a string
48  and is architecture-dependent.
49  */
50  std::string ptos(void *p);
51 
52  /** \brief Convert an integer to a string
53  */
54  std::string itos(int x);
55 
56  /** \brief Convert a size_t to a string
57  */
58  std::string szttos(size_t x);
59 
60  /** \brief Convert a boolean value to a string
61 
62  This returns \c "1" for true and \c "0" for false.
63  */
64  std::string btos(bool b);
65 
66  /** \brief Convert a double to a string
67 
68  If \c auto_prec is false, then the number is converted to
69  a string in the <tt>ios::scientific</tt> mode, otherwise,
70  neither the scientific or fixed mode flags are set and the
71  number is converted to a string in "automatic" mode.
72  */
73  std::string dtos(double x, int prec=6, bool auto_prec=false);
74 
75  /** \brief Returns the number of characters required to display the
76  exponent of \c x in scientific mode
77 
78  This returns 2 or 3, depending on whether or not the absolute
79  magnitude of the exponent is greater than or equal to 100. It
80  uses <tt>stringstream</tt> to convert the number to a string and
81  counts the number of characters directly.
82  */
83  size_t size_of_exponent(double x);
84 
85  /** \brief Convert a double to a string using a specified format
86  */
87  std::string dtos(double x, std::ostream &format);
88 
89  /** \brief Convert a string to an integer
90 
91  This function is now just a wrapper for <tt>std::stoi</tt>.
92  */
93  int stoi(std::string s);
94 
95  /** \brief Convert a string to an integer without throwing an
96  exception
97  */
98  int stoi_nothrow(std::string s, int &result);
99 
100  /** \brief Convert a string to a size_t
101 
102  If \c err_on_fail is true and the conversion fails, this
103  function calls the error handler, otherwise this function just
104  returns zero.
105  */
106  size_t stoszt(std::string s);
107 
108  /** \brief Convert a string to a size_t without throwing an
109  exception
110 
111  If \c err_on_fail is true and the conversion fails, this
112  function calls the error handler, otherwise this function just
113  returns zero.
114  */
115  int stoszt_nothrow(std::string s, size_t &result);
116 
117  /** \brief Convert a string to a boolean value
118 
119  This returns true if only if the string has at least one
120  character and the first non-whitespace character is either \c t,
121  \c T, or one of the numbers 1 through 9.
122 
123  If \c err_on_fail is true and the conversion fails, this
124  function calls the error handler, otherwise this function just
125  returns false.
126  */
127  bool stob(std::string s, bool err_on_fail=true);
128 
129  /** \brief Convert a string to a double
130 
131  This function is now just a wrapper for <tt>std::stod</tt>.
132 
133  \warning Because of the presence of <tt>std::stod()</tt> in
134  C++11, you may have to explicitly provide the
135  namespace, i.e. <tt>o2scl::stod()</tt> in your code.
136  */
137  double stod(std::string s);
138 
139  /** \brief Convert a string to a double returning non-zero
140  value for failure
141  */
142  int stod_nothrow(std::string s, double &result);
143 
144  /** \brief Find out if the number pointed to by \c x has a minus sign
145 
146  This function returns true if the number pointed to by \c x has
147  a minus sign, determining this by converting the number to a
148  string. It is useful, for example, in distinguishing "-0.0" from
149  "+0.0".
150  */
151  bool has_minus_sign(double *x);
152 
153  /** \brief Return true if the string \c s is likely a integral or
154  floating point number
155 
156  \note The test employed is not exhaustive and this function may
157  return \c true for some numbers and may return \c false for some
158  non-numbers.
159  */
160  bool is_number(std::string s);
161 
162  /** \brief Convert a formula to a double
163 
164  This function removes all quotes and apostrophes from the string
165  and then uses \ref o2scl::calculator to convert strings like
166  "-1.0e-3", "1.0/3.0" and "exp(cos(-1.0e-2))" to floating point
167  numbers.
168  */
169  double function_to_double(std::string s);
170 
171  /** \brief Split a string into words using whitespace for delimiters
172  and (partially) respecting quotes
173 
174  This function separates a string into words, and handles words
175  that begin with a <tt>"</tt> by adding more words until finding
176  one which ends with another <tt>"</tt>. Strings like
177  \code
178  this is a test
179  \endcode
180  get parsed as "this", "is", "a", "test" and strings like
181  \code
182  "this is" a test
183  \endcode
184  get parsed as "this is", "a", "test".
185 
186  This is used to reformat command descriptions and help text for
187  the screen width in cli::comm_option_help(), to process lines
188  read from a file in cli::comm_option_run(), and to process input
189  in cli::run_interactive().
190 
191  \note The parsing algorithm here is simple-minded and can
192  produce unexpected results in several ways. For example, it may
193  not properly handle nested quotes, like <tt>"""test" test2"
194  test3"</tt>.
195 
196  \future Replace with a better algorithm
197  \future Add user-specified delimiters?
198  */
199  void split_string(std::string str, std::vector<std::string> &sv);
200 
201  /** \brief Rewrap a string into a single column, avoiding
202  strings less than a particular number of characters
203  */
204  void rewrap(std::string str, std::vector<std::string> &sv,
205  size_t ncol=79);
206 
207  /** \brief Convert a string-based list of unsigned integers
208  to a list
209  */
210  template<class size_vec_t>
211  int string_to_uint_list(const std::string &x,
212  size_vec_t &list) {
213 
214  list.clear();
215  std::vector<std::string> ranges;
216  size_t k=0;
217  while (k<x.length()) {
218  size_t loc=x.find(',',k);
219  if (loc!=std::string::npos) {
220  std::string stemp=x.substr(k,loc-k);
221  ranges.push_back(stemp);
222  k+=stemp.length()+1;
223  } else {
224  if (k<x.length()) {
225  ranges.push_back(x.substr(k,x.length()-k));
226  }
227  k=x.length();
228  }
229  }
230  size_t uitmp, uitmp2;
231  for(size_t j=0;j<ranges.size();j++) {
232  if (ranges[j].find('-')==std::string::npos) {
233  int ret=stoszt_nothrow(ranges[j],uitmp);
234  if (ret!=0) return ret;
235  list.push_back(uitmp);
236  } else {
237  size_t loc=ranges[j].find('-');
238  std::string sstart=ranges[j].substr(0,loc);
239  std::string send=ranges[j].substr(loc+1,ranges[j].size()-loc);
240  int ret=stoszt_nothrow(sstart,uitmp);
241  if (ret!=0) return ret;
242  ret=stoszt_nothrow(send,uitmp2);
243  if (ret!=0) return ret;
244  for(size_t jk=uitmp;jk<=uitmp2;jk++) {
245  list.push_back(jk);
246  }
247  }
248  }
249 
250  return 0;
251  }
252  //@}
253 
254 #ifndef DOXYGEN_NO_O2NS
255 }
256 #endif
257 
258 #endif
259 
int stoszt_nothrow(std::string s, size_t &result)
Convert a string to a size_t without throwing an exception.
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
void rewrap(std::string str, std::vector< std::string > &sv, size_t ncol=79)
Rewrap a string into a single column, avoiding strings less than a particular number of characters...
int string_to_uint_list(const std::string &x, size_vec_t &list)
Convert a string-based list of unsigned integers to a list.
Definition: string_conv.h:211
std::string btos(bool b)
Convert a boolean value to a string.
size_t size_of_exponent(double x)
Returns the number of characters required to display the exponent of x in scientific mode...
bool stob(std::string s, bool err_on_fail=true)
Convert a string to a boolean value.
double function_to_double(std::string s)
Convert a formula to a double.
size_t stoszt(std::string s)
Convert a string to a size_t.
int stod_nothrow(std::string s, double &result)
Convert a string to a double returning non-zero value for failure.
bool has_minus_sign(double *x)
Find out if the number pointed to by x has a minus sign.
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
int stoi_nothrow(std::string s, int &result)
Convert a string to an integer without throwing an exception.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
double stod(std::string s)
Convert a string to a double.
void split_string(std::string str, std::vector< std::string > &sv)
Split a string into words using whitespace for delimiters and (partially) respecting quotes...
std::string ptos(void *p)
Convert a pointer to a string.
std::string itos(int x)
Convert an integer to a string.
std::string szttos(size_t x)
Convert a size_t to a string.
int stoi(std::string s)
Convert a string to an integer.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).