CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
15#include <optional>
16#include <string>
17#include <type_traits>
18
19// These check that the string is indeed a valid number,
20// and fail an assertion otherwise.
21// We use those for data types that C++11's std::stoi etc. do not
22// cover.
23unsigned safe_string2unsigned(const std::string &str, int base=10);
24std::size_t safe_string2size_t(const std::string &str, int base=10);
25
26// The below mimic C's atoi/atol: any errors are silently ignored.
27// They are meant to replace atoi/atol.
28int unsafe_string2int(const std::string &str, int base=10);
29unsigned unsafe_string2unsigned(const std::string &str, int base=10);
30std::size_t unsafe_string2size_t(const std::string &str, int base=10);
31
32// Same for atoll
33long long int unsafe_string2signedlonglong(const std::string &str, int base=10);
34long long unsigned int unsafe_string2unsignedlonglong(
35 const std::string &str, int base=10);
36
37// if we had a `resultt` á la Boost.Outcome (https://ned14.github.io/outcome/)
38// we could also return the reason why the conversion failed
39
42std::optional<int> string2optional_int(const std::string &, int base = 10);
43
47std::optional<unsigned>
48string2optional_unsigned(const std::string &, int base = 10);
49
53std::optional<std::size_t>
54string2optional_size_t(const std::string &, int base = 10);
55
57template <typename T>
58auto string2optional_base(const std::string &str, int base) ->
59 typename std::enable_if<std::is_signed<T>::value, long long>::type
60{
61 static_assert(
62 sizeof(T) <= sizeof(long long),
63 "this works under the assumption that long long is the largest type we try "
64 "to convert");
65 return std::stoll(str, nullptr, base);
66}
67
69template <typename T>
70auto string2optional_base(const std::string &str, int base) ->
71 typename std::enable_if<std::is_unsigned<T>::value, unsigned long long>::type
72{
73 static_assert(
74 sizeof(T) <= sizeof(unsigned long long),
75 "this works under the assumption that long long is the largest type we try "
76 "to convert");
77 if(str.find('-') != std::string::npos)
78 {
79 throw std::out_of_range{
80 "unsigned conversion behaves a bit strangely with negative values, "
81 "therefore we disable it"};
82 }
83 return std::stoull(str, nullptr, base);
84}
85
88template <typename do_conversiont>
90 -> std::optional<decltype(do_conversion())>
91{
92 try
93 {
94 return do_conversion();
95 }
96 catch(const std::invalid_argument &)
97 {
98 return std::nullopt;
99 }
100 catch(const std::out_of_range &)
101 {
102 return std::nullopt;
103 }
104}
105
110template <typename T>
111std::optional<T> string2optional(const std::string &str, int base = 10)
112{
113 return wrap_string_conversion([&]() {
115 });
116}
117
118#endif // CPROVER_UTIL_STRING2INT_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
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
Definition string2int.h:58
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...
Definition string2int.h:111
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...
Definition string2int.h:89