]> git.saurik.com Git - wxWidgets.git/commitdiff
extracted common code of ToLong and ToULong in a separate template helper
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 28 Oct 2006 15:24:07 +0000 (15:24 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 28 Oct 2006 15:24:07 +0000 (15:24 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42577 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/string.cpp

index 418fc9f0779f585285fb2d8586606351d6b138cf..62d81aab2982b68c573a05646dc9c920a9e6ca43 100644 (file)
@@ -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 <typename T>
+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