CBMC
string2int.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: 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 
16 unsigned 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 
23 std::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 
30 int unsafe_string2int(const std::string &str, int base)
31 {
32  return narrow_cast<int>(std::strtoll(str.c_str(), nullptr, base));
33 }
34 
35 unsigned unsafe_string2unsigned(const std::string &str, int base)
36 {
37  return narrow_cast<unsigned>(std::strtoul(str.c_str(), nullptr, base));
38 }
39 
40 std::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 
45 signed 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 
52 unsigned long long int unsafe_string2unsignedlonglong(
53  const std::string &str,
54  int base)
55 {
56  return *string2optional<unsigned long long>(str, base);
57 }
58 
59 std::optional<int> string2optional_int(const std::string &str, int base)
60 {
61  return string2optional<int>(str, base);
62 }
63 
64 std::optional<unsigned>
65 string2optional_unsigned(const std::string &str, int base)
66 {
67  return string2optional<unsigned>(str, base);
68 }
69 
70 std::optional<std::size_t>
71 string2optional_size_t(const std::string &str, int base)
72 {
73  return string2optional<std::size_t>(str, base);
74 }
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:495
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...
Definition: string2int.cpp:65
std::size_t unsafe_string2size_t(const std::string &str, int base)
Definition: string2int.cpp:40
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.
Definition: string2int.cpp:59
int unsafe_string2int(const std::string &str, int base)
Definition: string2int.cpp:30
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...
Definition: string2int.cpp:71
unsigned long long int unsafe_string2unsignedlonglong(const std::string &str, int base)
Definition: string2int.cpp:52
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:16
unsigned unsafe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:35
std::size_t safe_string2size_t(const std::string &str, int base)
Definition: string2int.cpp:23
signed long long int unsafe_string2signedlonglong(const std::string &str, int base)
Definition: string2int.cpp:45