cprover
Loading...
Searching...
No Matches
string2int.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Michael Tautschnig, michael.tautschnig@cs.ox.ac.uk
6
7\*******************************************************************/
8
9
10#ifndef CPROVER_UTIL_STRING2INT_H
11#define CPROVER_UTIL_STRING2INT_H
12
13#include "narrow.h"
14#include "optional.h"
15#include <string>
16#include <type_traits>
17
18// These check that the string is indeed a valid number,
19// and fail an assertion otherwise.
20// We use those for data types that C++11's std::stoi etc. do not
21// cover.
22unsigned safe_string2unsigned(const std::string &str, int base=10);
23std::size_t safe_string2size_t(const std::string &str, int base=10);
24
25// The below mimic C's atoi/atol: any errors are silently ignored.
26// They are meant to replace atoi/atol.
27int unsafe_string2int(const std::string &str, int base=10);
28unsigned unsafe_string2unsigned(const std::string &str, int base=10);
29std::size_t unsafe_string2size_t(const std::string &str, int base=10);
30
31// Same for atoll
32long long int unsafe_string2signedlonglong(const std::string &str, int base=10);
33long long unsigned int unsafe_string2unsignedlonglong(
34 const std::string &str, int base=10);
35
36// if we had a `resultt` รก la Boost.Outcome (https://ned14.github.io/outcome/)
37// we could also return the reason why the conversion failed
38
41optionalt<int> string2optional_int(const std::string &, int base = 10);
42
47string2optional_unsigned(const std::string &, int base = 10);
48
53string2optional_size_t(const std::string &, int base = 10);
54
56template <typename T>
57auto string2optional_base(const std::string &str, int base) ->
58 typename std::enable_if<std::is_signed<T>::value, long long>::type
59{
60 static_assert(
61 sizeof(T) <= sizeof(long long),
62 "this works under the assumption that long long is the largest type we try "
63 "to convert");
64 return std::stoll(str, nullptr, base);
65}
66
68template <typename T>
69auto string2optional_base(const std::string &str, int base) ->
70 typename std::enable_if<std::is_unsigned<T>::value, unsigned long long>::type
71{
72 static_assert(
73 sizeof(T) <= sizeof(unsigned long long),
74 "this works under the assumption that long long is the largest type we try "
75 "to convert");
76 if(str.find('-') != std::string::npos)
77 {
78 throw std::out_of_range{
79 "unsigned conversion behaves a bit strangely with negative values, "
80 "therefore we disable it"};
81 }
82 return std::stoull(str, nullptr, base);
83}
84
87template <typename do_conversiont>
89 -> optionalt<decltype(do_conversion())>
90{
91 try
92 {
93 return do_conversion();
94 }
95 catch(const std::invalid_argument &)
96 {
97 return nullopt;
98 }
99 catch(const std::out_of_range &)
100 {
101 return nullopt;
102 }
103}
104
109template <typename T>
110optionalt<T> string2optional(const std::string &str, int base = 10)
111{
112 return wrap_string_conversion([&]() {
114 });
115}
116
117#endif // CPROVER_UTIL_STRING2INT_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:563
nonstd::optional< T > optionalt
Definition optional.h:35
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< 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...
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 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
Definition string2int.h:57
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...
Definition string2int.h:88
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...
Definition string2int.h:110
std::size_t safe_string2size_t(const std::string &str, int base=10)
unsigned safe_string2unsigned(const std::string &str, int base=10)
int unsafe_string2int(const std::string &str, int base=10)
std::size_t unsafe_string2size_t(const std::string &str, int base=10)