cprover
Loading...
Searching...
No Matches
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
59optionalt<int> string2optional_int(const std::string &str, int base)
60{
61 return string2optional<int>(str, base);
62}
63
64optionalt<unsigned> string2optional_unsigned(const std::string &str, int base)
65{
66 return string2optional<unsigned>(str, base);
67}
68
69optionalt<std::size_t> string2optional_size_t(const std::string &str, int base)
70{
71 return string2optional<std::size_t>(str, base);
72}
output_type narrow_cast(input_type value)
Alias for static_cast intended to be used for numeric casting Rationale: Easier to grep than static_c...
Definition narrow.h:19
nonstd::optional< T > optionalt
Definition optional.h:35
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
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...
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...
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)
optionalt< int > string2optional_int(const std::string &str, int base)
Convert string to integer as per stoi, but return nullopt when stoi would throw.
unsigned unsafe_string2unsigned(const std::string &str, int base)
std::size_t safe_string2size_t(const std::string &str, int base)
signed long long int unsafe_string2signedlonglong(const std::string &str, int base)
optionalt< 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:110