Compare commits

...

2 Commits

  1. 10
      CMakeLists.txt
  2. 11
      conversion/stringconversion.h

10
CMakeLists.txt

@ -197,6 +197,16 @@ if (NOT ENABLE_THREAD_LOCAL)
PROPERTY COMPILE_DEFINITIONS ${META_PROJECT_VARNAME}_NO_THREAD_LOCAL)
endif ()
# configure use of charconv
option(ENABLE_CHARCONV "enables use charconv for integer conversions" ON)
option(ENABLE_CHARCONV_FP "enables use charconv for floating point conversions" ON)
if (ENABLE_CHARCONV)
list(APPEND META_PUBLIC_COMPILE_DEFINITIONS ${META_PROJECT_VARNAME}_ENABLE_CHARCONV)
endif ()
if (ENABLE_CHARCONV_FP)
list(APPEND META_PUBLIC_COMPILE_DEFINITIONS ${META_PROJECT_VARNAME}_ENABLE_CHARCONV_FP)
endif ()
# include modules to apply configuration
include(BasicConfig)
include(WindowsResources)

11
conversion/stringconversion.h

@ -25,6 +25,10 @@
#include <ranges>
#endif
#if defined(CPP_UTILITIES_ENABLE_CHARCONV) || defined(CPP_UTILITIES_ENABLE_CHARCONV_FP)
#include <charconv>
#endif
namespace CppUtilities {
/*!
@ -442,6 +446,7 @@ template <typename IntegralType, class StringType = std::string, typename BaseTy
Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>> * = nullptr>
StringType numberToString(IntegralType number, BaseType base = 10)
{
const bool negative = number < 0;
std::size_t resSize;
if (negative) {
@ -537,7 +542,7 @@ IntegralType stringToNumber(const StringType &string, BaseType base = 10)
*/
template <typename IntegralType, class StringType, typename BaseType = IntegralType,
Traits::EnableIf<std::is_integral<IntegralType>, std::is_signed<IntegralType>, Traits::Not<std::is_scalar<std::decay_t<StringType>>>> * = nullptr>
IntegralType stringToNumber(const StringType &string, IntegralType base = 10)
IntegralType stringToNumber(const StringType &string, BaseType base = 10)
{
auto i = string.begin();
auto end = string.end();
@ -581,10 +586,10 @@ FloatingType stringToNumber(const StringType &string, int base = 10)
return result;
}
std::string errorMsg;
errorMsg.reserve(42 + string.size());
errorMsg.reserve(48 + string.size());
errorMsg += "The string \"";
errorMsg += string;
errorMsg += "\" is no valid floating number.";
errorMsg += "\" is no valid floating point number.";
throw ConversionException(errorMsg);
}

Loading…
Cancel
Save