From 619dcb095efac5a79b6bd1e7c65d62d651ef48fc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 28 Oct 2006 15:24:07 +0000 Subject: [PATCH] extracted common code of ToLong and ToULong in a separate template helper git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42577 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/string.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/common/string.cpp b/src/common/string.cpp index 418fc9f077..62d81aab29 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -1675,32 +1675,33 @@ int wxString::Find(const wxChar *pszSub) const // conversion to numbers // ---------------------------------------------------------------------------- -bool wxString::ToLong(long *val, int base) const -{ - wxCHECK_MSG( val, false, _T("NULL pointer in wxString::ToLong") ); +// the implementation of all the functions below is exactly the same so factor +// it out +template +bool wxStringToIntType(const wxChar *start, + T *val, + int base, + T (*func)(const wxChar *, wxChar **, int)) +{ + wxCHECK_MSG( val, false, _T("NULL output pointer") ); wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); - const wxChar *start = c_str(); wxChar *end; - *val = wxStrtol(start, &end, base); + *val = (*func)(start, &end, base); // return true only if scan was stopped by the terminating NUL and if the // string was not empty to start with and no under/overflow occurred return !*end && (end != start) && (errno != ERANGE); } -bool wxString::ToULong(unsigned long *val, int base) const +bool wxString::ToLong(long *val, int base) const { - wxCHECK_MSG( val, false, _T("NULL pointer in wxString::ToULong") ); - wxASSERT_MSG( !base || (base > 1 && base <= 36), _T("invalid base") ); - - const wxChar *start = c_str(); - wxChar *end; - *val = wxStrtoul(start, &end, base); + return wxStringToIntType(c_str(), val, base, wxStrtol); +} - // return true only if scan was stopped by the terminating NUL and if the - // string was not empty to start with and no overflow occurred - return !*end && (end != start) && (errno != ERANGE); +bool wxString::ToULong(unsigned long *val, int base) const +{ + return wxStringToIntType(c_str(), val, base, wxStrtoul); } bool wxString::ToDouble(double *val) const -- 2.45.2