Add ways to substitute strtof/strtod/strtold with custom functions

The standard strtof/strtod/strtold are by default locale-aware. There
are a number of situations where we'd rather want to be able to use
locale-independent parsing functions. This can be done by setting the
ARGPARSE_CUSTOM_STRTOF/ARGPARSE_CUSTOM_STRTOD/ARGPARSE_CUSTOM_STRTOLD
macros to the appropriate value.
This commit is contained in:
Even Rouault 2024-03-12 10:22:58 +01:00
parent 20f0196321
commit c1fb3c0005
No known key found for this signature in database
GPG Key ID: 33EBBFC47B3DD87D

View File

@ -58,6 +58,18 @@ SOFTWARE.
#include <vector>
#endif
#ifndef ARGPARSE_CUSTOM_STRTOF
#define ARGPARSE_CUSTOM_STRTOF strtof
#endif
#ifndef ARGPARSE_CUSTOM_STRTOD
#define ARGPARSE_CUSTOM_STRTOD strtod
#endif
#ifndef ARGPARSE_CUSTOM_STRTOLD
#define ARGPARSE_CUSTOM_STRTOLD strtold
#endif
namespace argparse {
namespace details { // namespace for helper methods
@ -347,9 +359,10 @@ template <class T> struct parse_number<T> {
namespace {
template <class T> inline const auto generic_strtod = nullptr;
template <> inline const auto generic_strtod<float> = strtof;
template <> inline const auto generic_strtod<double> = strtod;
template <> inline const auto generic_strtod<long double> = strtold;
template <> inline const auto generic_strtod<float> = ARGPARSE_CUSTOM_STRTOF;
template <> inline const auto generic_strtod<double> = ARGPARSE_CUSTOM_STRTOD;
template <>
inline const auto generic_strtod<long double> = ARGPARSE_CUSTOM_STRTOLD;
} // namespace