A toolkit for working with phylogenetic data.
v0.24.0
convert.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2020 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <algorithm>
36 #include <cctype>
37 #include <limits>
38 #include <string>
39 #include <stdexcept>
40 #include <vector>
41 
42 namespace genesis {
43 namespace utils {
44 
45 // =================================================================================================
46 // Bool Text Conversion
47 // =================================================================================================
48 
53 bool convert_to_bool_( std::string const& str, bool& result )
54 {
55  // Prep.
56  auto const cont = to_lower_ascii( trim( str ));
57 
58  // Value check.
59  if( cont == "true" || cont == "yes" || cont == "on" || cont == "1" ) {
60  result = true;
61  return true;
62  } else if( cont.empty() || cont == "false" || cont == "no" || cont == "off" || cont == "0" ) {
63  result = false;
64  return true;
65  }
66  return false;
67 }
68 
69 bool convert_to_bool( std::string const& str )
70 {
71  bool result;
72  if( !convert_to_bool_( str, result )) {
73  throw std::runtime_error( "String is not convertible to bool." );
74  }
75  return result;
76 }
77 
78 bool is_convertible_to_bool( std::string const& str )
79 {
80  bool result;
81  return convert_to_bool_( str, result );
82 }
83 
84 // =================================================================================================
85 // Bool Double Text Conversion
86 // =================================================================================================
87 
92 bool convert_to_bool_double_( std::string const& str, double& result )
93 {
94  // Prep.
95  auto const cont = to_lower_ascii( trim( str ));
96 
97  // Value check. Need a bit of code duplciation here in order to catch the empty case.
98  if( cont == "true" || cont == "yes" || cont == "on" || cont == "1" ) {
99  result = 1.0;
100  return true;
101  } else if( cont == "false" || cont == "no" || cont == "off" || cont == "0" ) {
102  result = 0.0;
103  return true;
104  } else if( cont.empty() ) {
105  result = std::numeric_limits<double>::quiet_NaN();
106  return true;
107  }
108 
109  // For old compilers.
110  return false;
111 }
112 
113 double convert_to_bool_double( std::string const& str )
114 {
115  double result;
116  if( !convert_to_bool_double_( str, result )) {
117  throw std::runtime_error( "String is not convertible to bool." );
118  }
119  return result;
120 }
121 
122 bool is_convertible_to_bool_double( std::string const& str )
123 {
124  double result;
125  return convert_to_bool_double_( str, result );
126 }
127 
128 // =================================================================================================
129 // Double Text Conversion
130 // =================================================================================================
131 
136 bool convert_to_double_( std::string const& str, double& result )
137 {
138  bool err = false;
139  result = std::numeric_limits<double>::quiet_NaN();
140  if( str.empty() ) {
141  return true;
142  }
143 
144  try{
145  // Try conversion. Throws on failure.
146  auto const val = trim( str );
147  std::string::size_type sz;
148  result = std::stod( val, &sz );
149  if( sz != val.size() ) {
150  err = true;
151  }
152  } catch(...) {
153  err = true;
154  }
155  return !err;
156 }
157 
158 double convert_to_double( std::string const& str )
159 {
160  double result;
161  if( !convert_to_double_( str, result )) {
162  throw std::runtime_error( "String is not convertible to double." );
163  }
164  return result;
165 }
166 
167 bool is_convertible_to_double( std::string const& str )
168 {
169  double result;
170  return convert_to_double_( str, result );
171 }
172 
173 } // namespace utils
174 } // namespace genesis
bool is_convertible_to_double(Dataframe const &df, size_t col_index)
bool is_convertible_to_bool(Dataframe const &df, size_t col_index)
void convert_to_bool(Dataframe &df, size_t col_index)
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
void convert_to_double(Dataframe &df, size_t col_index)
bool convert_to_bool_(std::string const &str, bool &result)
Local helper function. Converts a string to bool, storing the result in result, and returning whether...
Definition: convert.cpp:53
double convert_to_bool_double(std::string const &str)
Definition: convert.cpp:113
bool convert_to_double_(std::string const &str, double &result)
Local helper function. Converts a string to double, storing the result in result, and returning wheth...
Definition: convert.cpp:136
std::string trim(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with trimmed white spaces.
Definition: string.cpp:394
bool is_convertible_to_bool_double(std::string const &str)
Definition: convert.cpp:122
Provides some commonly used string utility functions.
std::string to_lower_ascii(std::string const &str)
Return an all-lowercase copy of the given string, ASCII-only.
Definition: string.cpp:460
bool convert_to_bool_double_(std::string const &str, double &result)
Local helper function. Converts a string to bool, but stores it as a double, storing the result in re...
Definition: convert.cpp:92