10#ifndef CPROVER_UTIL_STRING2INT_H
11#define CPROVER_UTIL_STRING2INT_H
35 const std::string &str,
int base=10);
47std::optional<unsigned>
53std::optional<std::size_t>
59 typename std::enable_if<std::is_signed<T>::value,
long long>::type
62 sizeof(T) <=
sizeof(
long long),
63 "this works under the assumption that long long is the largest type we try "
65 return std::stoll(str,
nullptr, base);
71 typename std::enable_if<std::is_unsigned<T>::value,
unsigned long long>::type
74 sizeof(T) <=
sizeof(
unsigned long long),
75 "this works under the assumption that long long is the largest type we try "
77 if(str.find(
'-') != std::string::npos)
79 throw std::out_of_range{
80 "unsigned conversion behaves a bit strangely with negative values, "
81 "therefore we disable it"};
83 return std::stoull(str,
nullptr, base);
88template <
typename do_conversiont>
96 catch(
const std::invalid_argument &)
100 catch(
const std::out_of_range &)
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
long long int unsafe_string2signedlonglong(const std::string &str, int base=10)
unsigned unsafe_string2unsigned(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
long long unsigned int unsafe_string2unsignedlonglong(const std::string &str, int base=10)
std::optional< int > string2optional_int(const std::string &, int base=10)
Convert string to integer as per stoi, but return nullopt when stoi would throw.
std::optional< 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...
std::size_t safe_string2size_t(const std::string &str, int base=10)
unsigned safe_string2unsigned(const std::string &str, int base=10)
std::optional< 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...
std::optional< 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...
int unsafe_string2int(const std::string &str, int base=10)
std::size_t unsafe_string2size_t(const std::string &str, int base=10)
auto wrap_string_conversion(do_conversiont do_conversion) -> std::optional< decltype(do_conversion())>
attempt a given conversion, return nullopt if the conversion fails with out_of_range or invalid_argum...