cprover
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 <cerrno>
12 #include <cstdlib>
13 #include <cstring>
14 #include <limits>
15 #include <stdexcept>
16 
17 #include "invariant.h"
18 
19 unsigned safe_string2unsigned(const std::string &str, int base)
20 {
21  auto converted = string2optional<unsigned>(str, base);
22  CHECK_RETURN(converted != nullopt);
23  return *converted;
24 }
25 
26 std::size_t safe_string2size_t(const std::string &str, int base)
27 {
28  auto converted = string2optional<std::size_t>(str, base);
29  CHECK_RETURN(converted != nullopt);
30  return *converted;
31 }
32 
33 int unsafe_string2int(const std::string &str, int base)
34 {
35  return narrow_cast<int>(std::strtoll(str.c_str(), nullptr, base));
36 }
37 
38 unsigned unsafe_string2unsigned(const std::string &str, int base)
39 {
40  return narrow_cast<unsigned>(std::strtoul(str.c_str(), nullptr, base));
41 }
42 
43 std::size_t unsafe_string2size_t(const std::string &str, int base)
44 {
45  return narrow_cast<std::size_t>(std::strtoull(str.c_str(), nullptr, base));
46 }
47 
48 signed long long int unsafe_string2signedlonglong(
49  const std::string &str,
50  int base)
51 {
52  return std::strtoll(str.c_str(), nullptr, false);
53 }
54 
55 unsigned long long int unsafe_string2unsignedlonglong(
56  const std::string &str,
57  int base)
58 {
59  return *string2optional<unsigned long long>(str, base);
60 }
61 
62 optionalt<int> string2optional_int(const std::string &str, int base)
63 {
64  return string2optional<int>(str, base);
65 }
66 
67 optionalt<unsigned> string2optional_unsigned(const std::string &str, int base)
68 {
69  return string2optional<unsigned>(str, base);
70 }
71 
72 optionalt<std::size_t> string2optional_size_t(const std::string &str, int base)
73 {
74  return string2optional<std::size_t>(str, base);
75 }
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
safe_string2size_t
std::size_t safe_string2size_t(const std::string &str, int base)
Definition: string2int.cpp:26
unsafe_string2unsignedlonglong
unsigned long long int unsafe_string2unsignedlonglong(const std::string &str, int base)
Definition: string2int.cpp:55
string2int.h
string2optional_unsigned
optionalt< 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:67
unsafe_string2signedlonglong
signed long long int unsafe_string2signedlonglong(const std::string &str, int base)
Definition: string2int.cpp:48
string2optional_size_t
optionalt< 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:72
string2optional_int
optionalt< 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:62
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
safe_string2unsigned
unsigned safe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:19
invariant.h
unsafe_string2unsigned
unsigned unsafe_string2unsigned(const std::string &str, int base)
Definition: string2int.cpp:38
unsafe_string2size_t
std::size_t unsafe_string2size_t(const std::string &str, int base)
Definition: string2int.cpp:43
unsafe_string2int
int unsafe_string2int(const std::string &str, int base)
Definition: string2int.cpp:33