From 9163e8842f7cd775ac5c84b76148433c77cb2c82 Mon Sep 17 00:00:00 2001 From: Daniel Kroening Date: Mon, 11 May 2026 22:05:25 -0700 Subject: [PATCH] use std::string_view for safe_string2int The variants of safe_string2int now accept an std::string_view, instead of an std::string, in the hope of avoiding copies. --- src/util/string2int.cpp | 10 +++++----- src/util/string2int.h | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/util/string2int.cpp b/src/util/string2int.cpp index 02a90120a52..f2b4e372a61 100644 --- a/src/util/string2int.cpp +++ b/src/util/string2int.cpp @@ -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(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(str, base); CHECK_RETURN(converted.has_value()); @@ -56,19 +56,19 @@ unsigned long long int unsafe_string2unsignedlonglong( return *string2optional(str, base); } -std::optional string2optional_int(const std::string &str, int base) +std::optional string2optional_int(const std::string_view &str, int base) { return string2optional(str, base); } std::optional -string2optional_unsigned(const std::string &str, int base) +string2optional_unsigned(const std::string_view &str, int base) { return string2optional(str, base); } std::optional -string2optional_size_t(const std::string &str, int base) +string2optional_size_t(const std::string_view &str, int base) { return string2optional(str, base); } diff --git a/src/util/string2int.h b/src/util/string2int.h index f158323be52..bde6c318a94 100644 --- a/src/util/string2int.h +++ b/src/util/string2int.h @@ -15,44 +15,45 @@ Author: Michael Tautschnig, michael.tautschnig@cs.ox.ac.uk #include #include #include +#include #include // 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 string2optional_int(const std::string &, int base = 10); +std::optional 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 -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 -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, @@ -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 -std::optional string2optional(const std::string &str, int base = 10) +std::optional string2optional(const std::string_view &str, int base = 10) { PRECONDITION(base == 2 || base == 8 || base == 10 || base == 16);