CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
string2int.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Michael Tautschnig, michael.tautschnig@cs.ox.ac.uk
6
7\*******************************************************************/
8
9#include "string2int.h"
10
11#include <cstdlib>
12#include <stdexcept>
13
14#include "invariant.h"
15
16unsigned safe_string2unsigned(const std::string &str, int base)
17{
18 auto converted = string2optional<unsigned>(str, base);
19 CHECK_RETURN(converted.has_value());
20 return *converted;
21}
22
23std::size_t safe_string2size_t(const std::string &str, int base)
24{
25 auto converted = string2optional<std::size_t>(str, base);
26 CHECK_RETURN(converted.has_value());
27 return *converted;
28}
29
30int unsafe_string2int(const std::string &str, int base)
31{
32 return narrow_cast<int>(std::strtoll(str.c_str(), nullptr, base));
33}
34
35unsigned unsafe_string2unsigned(const std::string &str, int base)
36{
37 return narrow_cast<unsigned>(std::strtoul(str.c_str(), nullptr, base));
38}
39
40std::size_t unsafe_string2size_t(const std::string &str, int base)
41{
42 return narrow_cast<std::size_t>(std::strtoull(str.c_str(), nullptr, base));
43}
44
45signed long long int unsafe_string2signedlonglong(
46 const std::string &str,
47 int base)
48{
49 return std::strtoll(str.c_str(), nullptr, false);
50}
51
52unsigned long long int unsafe_string2unsignedlonglong(
53 const std::string &str,
54 int base)
55{
56 return *string2optional<unsigned long long>(str, base);
57}
58
59std::optional<int> string2optional_int(const std::string &str, int base)
60{
61 return string2optional<int>(str, base);
62}
63
64std::optional<unsigned>
65string2optional_unsigned(const std::string &str, int base)
66{
67 return string2optional<unsigned>(str, base);
68}
69
70std::optional<std::size_t>
71string2optional_size_t(const std::string &str, int base)
72{
73 return string2optional<std::size_t>(str, base);
74}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
std::optional< std::size_t > string2optional_size_t(const std::string &str, int base)
Convert string to size_t similar to the stoul or stoull functions, return nullopt when the conversion...
std::size_t unsafe_string2size_t(const std::string &str, int base)
int unsafe_string2int(const std::string &str, int base)
unsigned long long int unsafe_string2unsignedlonglong(const std::string &str, int base)
unsigned safe_string2unsigned(const std::string &str, int base)
unsigned unsafe_string2unsigned(const std::string &str, int base)
std::size_t safe_string2size_t(const std::string &str, int base)
std::optional< unsigned > string2optional_unsigned(const std::string &str, int base)
Convert string to unsigned similar to the stoul or stoull functions, return nullopt when the conversi...
signed long long int unsafe_string2signedlonglong(const std::string &str, int base)
std::optional< int > string2optional_int(const std::string &str, int base)
Convert string to integer as per stoi, but return nullopt when stoi would throw.