10 #ifndef CPROVER_UTIL_STRING2INT_H
11 #define CPROVER_UTIL_STRING2INT_H
17 #include <type_traits>
35 const std::string &str,
int base=10);
47 std::optional<unsigned>
53 std::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);
88 template <
typename do_conversiont>
90 -> std::optional<decltype(do_conversion())>
94 return do_conversion();
100 catch(
const std::out_of_range &)
110 template <
typename T>
114 return narrow_or_throw_out_of_range<T>(string2optional_base<T>(str, base));
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...
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::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< 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...
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...
invalid_command_line_argument_exceptiont invalid_argument(const std::string &option_name, const std::string &bad_argument, const mappingt &mapping)