Define wxStrtox() overloads taking nullptr

Instead of specializing wxStrtoxCharType and then testing whether endptr
is null, just define separate, and simpler, overloads of wxStrtox()
functions taking nullptr_t -- we can avoid the unnecessary test
completely in this case, as nullptr is, by definition, always null
anyhow.

Also add a test of using wxStrtol() with nullptr too.

This should fix the build with older gcc and MSVS versions.
This commit is contained in:
Vadim Zeitlin
2020-02-05 03:52:51 +01:00
parent 1650ea7030
commit 00cdab77c5
2 changed files with 32 additions and 12 deletions

View File

@@ -844,16 +844,6 @@ template<> struct wxStrtoxCharType<int>
return NULL;
}
};
#ifdef wxHAS_NULLPTR_T
template<> struct wxStrtoxCharType<std::nullptr_t>
{
typedef const char* Type;
static char** AsPointer(std::nullptr_t)
{
return nullptr;
}
};
#endif
template<typename T>
inline double wxStrtod(const wxString& nptr, T endptr)
@@ -877,6 +867,25 @@ template<typename T>
inline double wxStrtod(const wxCStrData& nptr, T endptr)
{ return wxStrtod(nptr.AsString(), endptr); }
#ifdef wxHAS_NULLPTR_T
inline double wxStrtod(const wxString& nptr, nullptr_t)
{ return wxStrtod(nptr.wx_str(), static_cast<wxStringCharType**>(NULL)); };
inline double wxStrtod(const wxCStrData& nptr, nullptr_t)
{ return wxStrtod(nptr.AsString(), static_cast<wxStringCharType**>(NULL)); };
#define WX_STRTOX_DEFINE_NULLPTR_OVERLOADS(rettype, name) \
inline rettype name(const wxString& nptr, nullptr_t, int base) \
{ return name(nptr.wx_str(), static_cast<wxStringCharType**>(NULL), \
base); }; \
inline rettype name(const wxCStrData& nptr, nullptr_t, int base) \
{ return name(nptr.AsString(), static_cast<wxStringCharType**>(NULL), \
base); };
#else // !wxHAS_NULLPTR_T
#define WX_STRTOX_DEFINE_NULLPTR_OVERLOADS(rettype, name)
#endif // wxHAS_NULLPTR_T/!wxHAS_NULLPTR_T
#define WX_STRTOX_FUNC(rettype, name, implA, implW) \
/* see wxStrtod() above for explanation of this code: */ \
@@ -902,7 +911,8 @@ inline double wxStrtod(const wxCStrData& nptr, T endptr)
} \
template<typename T> \
inline rettype name(const wxCStrData& nptr, T endptr, int base) \
{ return name(nptr.AsString(), endptr, base); }
{ return name(nptr.AsString(), endptr, base); } \
WX_STRTOX_DEFINE_NULLPTR_OVERLOADS(rettype, name)
WX_STRTOX_FUNC(long, wxStrtol, wxCRT_StrtolA, wxCRT_StrtolW)
WX_STRTOX_FUNC(unsigned long, wxStrtoul, wxCRT_StrtoulA, wxCRT_StrtoulW)

View File

@@ -234,10 +234,11 @@ TEST_CASE("CRT::Strnlen", "[crt][strnlen]")
CHECK( wxStrnlen(L"1234" L"\0" L"5678", 12) == 4 );
}
TEST_CASE("CRT::Strtod", "[crt][strtod]")
TEST_CASE("CRT::Strtox", "[crt][strtod][strtol]")
{
const wxString s = "123@";
const double d = 123.0;
const long l = 123;
SECTION("char")
{
@@ -245,6 +246,10 @@ TEST_CASE("CRT::Strtod", "[crt][strtod]")
CHECK( wxStrtod(s, &end) == d );
REQUIRE( end );
CHECK( *end == '@' );
CHECK( wxStrtol(s, &end, 10) == l );
REQUIRE( end );
CHECK( *end == '@' );
}
SECTION("wchar_t")
@@ -253,6 +258,10 @@ TEST_CASE("CRT::Strtod", "[crt][strtod]")
CHECK( wxStrtod(s, &end) == d );
REQUIRE( end );
CHECK( *end == L'@' );
CHECK( wxStrtol(s, &end, 10) == l );
REQUIRE( end );
CHECK( *end == L'@' );
}
SECTION("other")
@@ -260,6 +269,7 @@ TEST_CASE("CRT::Strtod", "[crt][strtod]")
CHECK( wxStrtod(s, 0) == d );
#ifdef wxHAS_NULLPTR_T
CHECK( wxStrtod(s, nullptr) == d );
CHECK( wxStrtol(s, nullptr, 10) == l );
#endif
}
}