Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/util/string2int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ Author: Michael Tautschnig, michael.tautschnig@cs.ox.ac.uk

#include "invariant.h"

unsigned safe_string2unsigned(const std::string &str, int base)
unsigned safe_string2unsigned(const std::string_view &str, int base)
{
auto converted = string2optional<unsigned>(str, base);
CHECK_RETURN(converted.has_value());
return *converted;
}

std::size_t safe_string2size_t(const std::string &str, int base)
std::size_t safe_string2size_t(const std::string_view &str, int base)
{
auto converted = string2optional<std::size_t>(str, base);
CHECK_RETURN(converted.has_value());
Expand Down Expand Up @@ -56,19 +56,19 @@ unsigned long long int unsafe_string2unsignedlonglong(
return *string2optional<unsigned long long>(str, base);
}

std::optional<int> string2optional_int(const std::string &str, int base)
std::optional<int> string2optional_int(const std::string_view &str, int base)
{
return string2optional<int>(str, base);
}

std::optional<unsigned>
string2optional_unsigned(const std::string &str, int base)
string2optional_unsigned(const std::string_view &str, int base)
{
return string2optional<unsigned>(str, base);
}

std::optional<std::size_t>
string2optional_size_t(const std::string &str, int base)
string2optional_size_t(const std::string_view &str, int base)
{
return string2optional<std::size_t>(str, base);
}
25 changes: 13 additions & 12 deletions src/util/string2int.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,45 @@ Author: Michael Tautschnig, michael.tautschnig@cs.ox.ac.uk
#include <charconv>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>

// These check that the string is indeed a valid number,
// and fail an assertion otherwise.
// We use those for data types that C++11's std::stoi etc. do not
// cover.
unsigned safe_string2unsigned(const std::string &str, int base=10);
std::size_t safe_string2size_t(const std::string &str, int base=10);
unsigned safe_string2unsigned(const std::string_view &, int base = 10);
std::size_t safe_string2size_t(const std::string_view &, int base = 10);

// The below mimic C's atoi/atol: any errors are silently ignored.
// They are meant to replace atoi/atol.
int unsafe_string2int(const std::string &str, int base=10);
unsigned unsafe_string2unsigned(const std::string &str, int base=10);
std::size_t unsafe_string2size_t(const std::string &str, int base=10);
int unsafe_string2int(const std::string &, int base = 10);
unsigned unsafe_string2unsigned(const std::string &, int base = 10);
std::size_t unsafe_string2size_t(const std::string &, int base = 10);

// Same for atoll
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);
long long int unsafe_string2signedlonglong(const std::string &, int base = 10);
long long unsigned int
unsafe_string2unsignedlonglong(const std::string_view &, 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
std::optional<int> string2optional_int(const std::string &, int base = 10);
std::optional<int> string2optional_int(const std::string_view &, 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
std::optional<unsigned>
string2optional_unsigned(const std::string &, int base = 10);
string2optional_unsigned(const std::string_view &, 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
std::optional<std::size_t>
string2optional_size_t(const std::string &, int base = 10);
string2optional_size_t(const std::string_view &, int base = 10);

/// Convert a string to an integer, given the base of the representation,
/// works with signed and unsigned integer types,
Expand All @@ -62,7 +63,7 @@ string2optional_size_t(const std::string &, int base = 10);
/// rejects any trailing non-numerical suffix.
/// A prefix such as 0, 0x, 0X to change base is _not_ supported.
template <typename T>
std::optional<T> string2optional(const std::string &str, int base = 10)
std::optional<T> string2optional(const std::string_view &str, int base = 10)
{
PRECONDITION(base == 2 || base == 8 || base == 10 || base == 16);

Expand Down
Loading