-
Notifications
You must be signed in to change notification settings - Fork 273
Add string2optional conversion functions #4192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hannes-steffenhagen-diffblue
merged 1 commit into
diffblue:develop
from
hannes-steffenhagen-diffblue:string_to_optional
Feb 15, 2019
+215
−45
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,82 +10,66 @@ Author: Michael Tautschnig, [email protected] | |
|
||
#include <cerrno> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <limits> | ||
#include <stdexcept> | ||
|
||
#include "invariant.h" | ||
|
||
template <typename T> | ||
inline T str2number(const char *str, int base, bool safe) | ||
{ | ||
int errno_bak=errno; | ||
errno=0; | ||
char *endptr; | ||
// _strtoi64 is available in Visual Studio, but not yet in MINGW | ||
#ifdef _MSC_VER | ||
const __int64 val=_strtoi64(str, &endptr, base); | ||
#else | ||
const long long val=strtoll(str, &endptr, base); | ||
#endif | ||
|
||
if(safe) | ||
{ | ||
CHECK_RETURN(0==errno); | ||
errno=errno_bak; | ||
CHECK_RETURN(endptr!=str); | ||
if(std::numeric_limits<T>::min()==0) | ||
{ | ||
// unsigned | ||
CHECK_RETURN(val>=0); | ||
CHECK_RETURN( | ||
(unsigned long long)(T)val<= | ||
(unsigned long long)std::numeric_limits<T>::max()); | ||
} | ||
else | ||
{ | ||
// signed | ||
CHECK_RETURN(val<=(long long)std::numeric_limits<T>::max()); | ||
CHECK_RETURN(val>=(long long)std::numeric_limits<T>::min()); | ||
} | ||
} | ||
|
||
return (T)val; | ||
} | ||
|
||
unsigned safe_string2unsigned(const std::string &str, int base) | ||
{ | ||
return str2number<unsigned>(str.c_str(), base, true); | ||
auto converted = string2optional<unsigned>(str, base); | ||
CHECK_RETURN(converted != nullopt); | ||
return *converted; | ||
} | ||
|
||
std::size_t safe_string2size_t(const std::string &str, int base) | ||
{ | ||
return str2number<std::size_t>(str.c_str(), base, true); | ||
auto converted = string2optional<std::size_t>(str, base); | ||
CHECK_RETURN(converted != nullopt); | ||
return *converted; | ||
} | ||
|
||
int unsafe_string2int(const std::string &str, int base) | ||
{ | ||
return str2number<int>(str.c_str(), base, false); | ||
return narrow_cast<int>(std::strtoll(str.c_str(), nullptr, base)); | ||
} | ||
|
||
unsigned unsafe_string2unsigned(const std::string &str, int base) | ||
{ | ||
return str2number<unsigned>(str.c_str(), base, false); | ||
return narrow_cast<unsigned>(std::strtoul(str.c_str(), nullptr, base)); | ||
} | ||
|
||
std::size_t unsafe_string2size_t(const std::string &str, int base) | ||
{ | ||
return str2number<std::size_t>(str.c_str(), base, false); | ||
return narrow_cast<std::size_t>(std::strtoull(str.c_str(), nullptr, base)); | ||
} | ||
|
||
signed long long int unsafe_string2signedlonglong( | ||
const std::string &str, | ||
int base) | ||
{ | ||
return str2number<signed long long int>(str.c_str(), base, false); | ||
return std::strtoll(str.c_str(), nullptr, false); | ||
} | ||
|
||
unsigned long long int unsafe_string2unsignedlonglong( | ||
const std::string &str, | ||
int base) | ||
{ | ||
return str2number<unsigned long long int>(str.c_str(), base, false); | ||
return *string2optional<unsigned long long>(str, base); | ||
} | ||
|
||
optionalt<int> string2optional_int(const std::string &str, int base) | ||
{ | ||
return string2optional<int>(str, base); | ||
} | ||
|
||
optionalt<unsigned> string2optional_unsigned(const std::string &str, int base) | ||
{ | ||
return string2optional<unsigned>(str, base); | ||
} | ||
|
||
optionalt<std::size_t> string2optional_size_t(const std::string &str, int base) | ||
{ | ||
return string2optional<std::size_t>(str, base); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,10 @@ Author: Michael Tautschnig, [email protected] | |
#ifndef CPROVER_UTIL_STRING2INT_H | ||
#define CPROVER_UTIL_STRING2INT_H | ||
|
||
#include "narrow.h" | ||
#include "optional.h" | ||
#include <string> | ||
#include <type_traits> | ||
|
||
// These check that the string is indeed a valid number, | ||
// and fail an assertion otherwise. | ||
|
@@ -30,4 +33,85 @@ long long int unsafe_string2signedlonglong(const std::string &str, int base=10); | |
long long unsigned int unsafe_string2unsignedlonglong( | ||
const std::string &str, int base=10); | ||
|
||
// if we had a `resultt` á la Boost.Outcome (https://ned14.github.io/outcome/) | ||
// we could also return the reason why the conversion failed | ||
|
||
/// Convert string to integer as per stoi, but return nullopt when | ||
/// stoi would throw | ||
optionalt<int> string2optional_int(const std::string &, int base = 10); | ||
|
||
/// Convert string to unsigned similar to the stoul or stoull functions, | ||
/// return nullopt when the conversion fails. | ||
/// Note: Unlike stoul or stoull negative inputs are disallowed | ||
optionalt<unsigned> | ||
string2optional_unsigned(const std::string &, int base = 10); | ||
|
||
/// Convert string to size_t similar to the stoul or stoull functions, | ||
/// return nullopt when the conversion fails. | ||
/// Note: Unlike stoul or stoull negative inputs are disallowed | ||
optionalt<std::size_t> | ||
string2optional_size_t(const std::string &, int base = 10); | ||
|
||
/// convert string to signed long long if T is signed | ||
template <typename T> | ||
auto string2optional_base(const std::string &str, int base) -> | ||
typename std::enable_if<std::is_signed<T>::value, long long>::type | ||
{ | ||
static_assert( | ||
sizeof(T) <= sizeof(long long), | ||
"this works under the assumption that long long is the largest type we try " | ||
"to convert"); | ||
return std::stoll(str, nullptr, base); | ||
} | ||
|
||
/// convert string to unsigned long long if T is unsigned | ||
template <typename T> | ||
auto string2optional_base(const std::string &str, int base) -> | ||
typename std::enable_if<std::is_unsigned<T>::value, unsigned long long>::type | ||
{ | ||
static_assert( | ||
sizeof(T) <= sizeof(unsigned long long), | ||
"this works under the assumption that long long is the largest type we try " | ||
"to convert"); | ||
if(str.find('-') != std::string::npos) | ||
{ | ||
throw std::out_of_range{ | ||
"unsigned conversion behaves a bit strangely with negative values, " | ||
"therefore we disable it"}; | ||
} | ||
return std::stoull(str, nullptr, base); | ||
} | ||
|
||
/// attempt a given conversion, return nullopt if the conversion fails | ||
/// with out_of_range or invalid_argument | ||
template <typename do_conversiont> | ||
auto wrap_string_conversion(do_conversiont do_conversion) | ||
-> optionalt<decltype(do_conversion())> | ||
{ | ||
try | ||
{ | ||
return do_conversion(); | ||
} | ||
catch(const std::invalid_argument &) | ||
{ | ||
return nullopt; | ||
} | ||
catch(const std::out_of_range &) | ||
{ | ||
return nullopt; | ||
} | ||
} | ||
|
||
/// convert a string to an integer, given the base of the representation | ||
/// works with signed and unsigned integer types smaller than | ||
/// (unsigned) long long | ||
/// does not accept negative inputs when the result type is unsigned | ||
template <typename T> | ||
optionalt<T> string2optional(const std::string &str, int base) | ||
{ | ||
return wrap_string_conversion([&]() { | ||
return narrow_or_throw_out_of_range<T>(string2optional_base<T>(str, base)); | ||
}); | ||
} | ||
|
||
#endif // CPROVER_UTIL_STRING2INT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for string2int.h | ||
|
||
Author: Diffblue Ltd. | ||
|
||
\*******************************************************************/ | ||
|
||
#include <testing-utils/use_catch.h> | ||
#include <util/string2int.h> | ||
|
||
TEST_CASE( | ||
"converting optionally to a valid integer should succeed", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_int("13") == 13); | ||
REQUIRE(string2optional_int("-5") == -5); | ||
REQUIRE(string2optional_int("c0fefe", 16) == 0xc0fefe); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting invalid string to integer should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_int("thirteen") == nullopt); | ||
REQUIRE(string2optional_int("c0fefe") == nullopt); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting string out of range to integer should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE( | ||
string2optional_int("0xfffffffffffffffffffffffffffffffffffffffffff", 16) == | ||
nullopt); | ||
} | ||
|
||
TEST_CASE( | ||
"converting optionally to a valid unsigned should succeed", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_unsigned("13") == 13u); | ||
REQUIRE(string2optional_unsigned("c0fefe", 16) == 0xc0fefeu); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting invalid string to unsigned should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_unsigned("thirteen") == nullopt); | ||
REQUIRE(string2optional_unsigned("c0fefe") == nullopt); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting string out of range to unsigned should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE( | ||
string2optional_unsigned( | ||
"0xfffffffffffffffffffffffffffffffffffffffffff", 16) == nullopt); | ||
REQUIRE(string2optional_unsigned("-5") == nullopt); | ||
} | ||
|
||
TEST_CASE( | ||
"converting optionally to a valid size_t should succeed", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_size_t("13") == std::size_t{13}); | ||
REQUIRE(string2optional_size_t("c0fefe", 16) == std::size_t{0xc0fefe}); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting invalid string to size_t should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE(string2optional_size_t("thirteen") == nullopt); | ||
REQUIRE(string2optional_size_t("c0fefe") == nullopt); | ||
} | ||
|
||
TEST_CASE( | ||
"optionally converting string out of range to size_t should return nullopt", | ||
"[core][util][string2int]") | ||
{ | ||
REQUIRE( | ||
string2optional_size_t( | ||
"0xfffffffffffffffffffffffffffffffffffffffffff", 16) == nullopt); | ||
REQUIRE(string2optional_size_t("-5") == nullopt); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be templates, as is done for
numeric_cast
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll write one