Go to the documentation of this file.
10 #ifndef CPROVER_UTIL_STRING2INT_H
11 #define CPROVER_UTIL_STRING2INT_H
16 #include <type_traits>
34 const std::string &str,
int base=10);
58 typename std::enable_if<std::is_signed<T>::value,
long long>::type
61 sizeof(T) <=
sizeof(
long long),
62 "this works under the assumption that long long is the largest type we try "
64 return std::stoll(str,
nullptr, base);
70 typename std::enable_if<std::is_unsigned<T>::value,
unsigned long long>::type
73 sizeof(T) <=
sizeof(
unsigned long long),
74 "this works under the assumption that long long is the largest type we try "
76 if(str.find(
'-') != std::string::npos)
78 throw std::out_of_range{
79 "unsigned conversion behaves a bit strangely with negative values, "
80 "therefore we disable it"};
82 return std::stoull(str,
nullptr, base);
87 template <
typename do_conversiont>
93 return do_conversion();
99 catch(
const std::out_of_range &)
109 template <
typename T>
113 return narrow_or_throw_out_of_range<T>(string2optional_base<T>(str, base));
117 #endif // CPROVER_UTIL_STRING2INT_H
unsigned unsafe_string2unsigned(const std::string &str, int base=10)
optionalt< unsigned > string2optional_unsigned(const std::string &, int base=10)
Convert string to unsigned similar to the stoul or stoull functions, return nullopt when the conversi...
invalid_command_line_argument_exceptiont invalid_argument(const std::string &option_name, const std::string &bad_argument, const mappingt &mapping)
std::size_t unsafe_string2size_t(const std::string &str, int base=10)
optionalt< int > string2optional_int(const std::string &, int base=10)
Convert string to integer as per stoi, but return nullopt when stoi would throw.
unsigned safe_string2unsigned(const std::string &str, int base=10)
nonstd::optional< T > optionalt
long long unsigned int unsafe_string2unsignedlonglong(const std::string &str, int base=10)
auto wrap_string_conversion(do_conversiont do_conversion) -> optionalt< decltype(do_conversion())>
attempt a given conversion, return nullopt if the conversion fails with out_of_range or invalid_argum...
int unsafe_string2int(const std::string &str, int base=10)
auto string2optional_base(const std::string &str, int base) -> typename std::enable_if< std::is_signed< T >::value, long long >::type
convert string to signed long long if T is signed
std::size_t safe_string2size_t(const std::string &str, int base=10)
optionalt< std::size_t > string2optional_size_t(const std::string &, int base=10)
Convert string to size_t similar to the stoul or stoull functions, return nullopt when the conversion...
long long int unsafe_string2signedlonglong(const std::string &str, int base=10)
optionalt< T > string2optional(const std::string &str, int base=10)
convert a string to an integer, given the base of the representation works with signed and unsigned i...