cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
string2int.h
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
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.
25
unsigned
safe_string2unsigned
(std::string_view,
int
base = 10);
26
std::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.
30
int
unsafe_string2int
(
const
std::string &,
int
base = 10);
31
unsigned
unsafe_string2unsigned
(
const
std::string &,
int
base = 10);
32
std::size_t
unsafe_string2size_t
(
const
std::string &,
int
base = 10);
33
34
// Same for atoll
35
long
long
int
unsafe_string2signedlonglong
(
const
std::string &,
int
base = 10);
36
long
long
unsigned
int
37
unsafe_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
44
std::optional<int>
string2optional_int
(std::string_view,
int
base = 10);
45
49
std::optional<unsigned>
50
string2optional_unsigned
(std::string_view,
int
base = 10);
51
55
std::optional<std::size_t>
56
string2optional_size_t
(std::string_view,
int
base = 10);
57
65
template
<
typename
T>
66
std::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
narrow.h
PRECONDITION
#define PRECONDITION(CONDITION)
Definition
invariant.h:463
string2optional_size_t
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...
Definition
string2int.cpp:70
unsafe_string2signedlonglong
long long int unsafe_string2signedlonglong(const std::string &, int base=10)
Definition
string2int.cpp:45
string2optional_unsigned
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...
Definition
string2int.cpp:64
unsafe_string2size_t
std::size_t unsafe_string2size_t(const std::string &, int base=10)
Definition
string2int.cpp:40
string2optional_int
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.
Definition
string2int.cpp:59
string2optional
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
safe_string2unsigned
unsigned safe_string2unsigned(std::string_view, int base=10)
Definition
string2int.cpp:16
unsafe_string2unsigned
unsigned unsafe_string2unsigned(const std::string &, int base=10)
Definition
string2int.cpp:35
safe_string2size_t
std::size_t safe_string2size_t(std::string_view, int base=10)
Definition
string2int.cpp:23
unsafe_string2int
int unsafe_string2int(const std::string &, int base=10)
Definition
string2int.cpp:30
unsafe_string2unsignedlonglong
long long unsigned int unsafe_string2unsignedlonglong(const std::string &, int base=10)
Definition
string2int.cpp:52
util
string2int.h
Generated by
1.17.0