CBMC
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
15#include <charconv>
16#include <optional>
17#include <string>
18#include <string_view>
19#include <type_traits>
20
21// These check that the string is indeed a valid number,
22// and fail an assertion otherwise.
23// We use those for data types that C++11's std::stoi etc. do not
24// cover.
25unsigned safe_string2unsigned(std::string_view, int base = 10);
26std::size_t safe_string2size_t(std::string_view, int base = 10);
27
28// The below mimic C's atoi/atol: any errors are silently ignored.
29// They are meant to replace atoi/atol.
30int unsafe_string2int(const std::string &, int base = 10);
31unsigned unsafe_string2unsigned(const std::string &, int base = 10);
32std::size_t unsafe_string2size_t(const std::string &, int base = 10);
33
34// Same for atoll
35long long int unsafe_string2signedlonglong(const std::string &, int base = 10);
36long long unsigned int
37unsafe_string2unsignedlonglong(const std::string &, int base = 10);
38
39// if we had a `resultt` รก la Boost.Outcome (https://ned14.github.io/outcome/)
40// we could also return the reason why the conversion failed
41
44std::optional<int> string2optional_int(std::string_view, int base = 10);
45
49std::optional<unsigned>
50string2optional_unsigned(std::string_view, int base = 10);
51
55std::optional<std::size_t>
56string2optional_size_t(std::string_view, int base = 10);
57
65template <typename T>
66std::optional<T> string2optional(std::string_view str, int base = 10)
67{
68 PRECONDITION(base == 2 || base == 8 || base == 10 || base == 16);
69
70 static_assert(
71 std::is_integral<T>::value, "string2optional requires an integral type");
72
73 if(str.empty())
74 return std::nullopt;
75
76 // reject negative inputs for unsigned types
77 if(std::is_unsigned<T>::value && str.front() == '-')
78 return std::nullopt;
79
80 const char *first = str.data();
81 const char *last = str.data() + str.size();
82
83 T value{};
84 auto [ptr, ec] = std::from_chars(first, last, value, base);
85
86 if(ec != std::errc{} || ptr != last)
87 return std::nullopt;
88
89 return value;
90}
91
92#endif // CPROVER_UTIL_STRING2INT_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
#define PRECONDITION(CONDITION)
Definition invariant.h:463
std::optional< std::size_t > string2optional_size_t(std::string_view, 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 &, int base=10)
std::optional< unsigned > string2optional_unsigned(std::string_view, int base=10)
Convert string to unsigned similar to the stoul or stoull functions, return nullopt when the conversi...
std::size_t unsafe_string2size_t(const std::string &, int base=10)
std::optional< int > string2optional_int(std::string_view, int base=10)
Convert string to integer as per stoi, but return nullopt when stoi would throw.
std::optional< T > string2optional(std::string_view str, int base=10)
Convert a string to an integer, given the base of the representation, works with signed and unsigned ...
Definition string2int.h:66
unsigned safe_string2unsigned(std::string_view, int base=10)
unsigned unsafe_string2unsigned(const std::string &, int base=10)
std::size_t safe_string2size_t(std::string_view, int base=10)
int unsafe_string2int(const std::string &, int base=10)
long long unsigned int unsafe_string2unsignedlonglong(const std::string &, int base=10)