X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2a31049201e9c1ff9f515eb9d144ae392f4b4e5a..243d4b360385d3364e96d5600f693139e359fb3b:/src/common/longlong.cpp diff --git a/src/common/longlong.cpp b/src/common/longlong.cpp index 6a0cc8e315..ba47b870f9 100644 --- a/src/common/longlong.cpp +++ b/src/common/longlong.cpp @@ -57,27 +57,6 @@ void *wxLongLongNative::asArray() const return temp; } -#if wxUSE_STD_IOSTREAM - -// input/output -ostream& operator<< (ostream& o, const wxLongLongNative& ll) -{ - char result[65]; - - memset(result, 'A', 64); - - result[64] = '\0'; - - for (int i = 0; i < 64; i++) - { - result[63 - i] = '0' + (char) ((ll.m_ll >> i) & 1); - } - - return o << result; -} - -#endif // wxUSE_STD_IOSTREAM - #endif // wxUSE_LONGLONG_NATIVE // ============================================================================ @@ -91,34 +70,26 @@ wxLongLongWx& wxLongLongWx::Assign(double d) { bool positive = d >= 0; d = fabs(d); - if ( d <= LONG_MAX ) + if ( d <= ULONG_MAX ) { m_hi = 0; m_lo = (long)d; } else { -#if 0 - m_lo = (long)d; - d -= m_lo; - d /= 0x1000; - d /= 0x1000; - d /= 0x100; - m_hi = (long)d; -#else - wxFAIL_MSG(_T("TODO")); -#endif + m_hi = (unsigned long)(d / (1.0 + (double)ULONG_MAX)); + m_lo = (unsigned long)(d - ((double)m_hi * (1.0 + (double)ULONG_MAX))); } - if ( !positive ) - m_hi = -m_hi; - #ifdef wxLONGLONG_TEST_MODE m_ll = (wxLongLong_t)d; Check(); #endif // wxLONGLONG_TEST_MODE + if ( !positive ) + Negate(); + return *this; } @@ -486,7 +457,7 @@ void wxLongLongWx::Divide(const wxLongLongWx& divisorIn, // Use of this program, for any purpose, is granted the author, Ian // Kaplan, as long as this copyright notice is included in the source // code or any source code derived from this program. The user assumes - // all responsibility for using this code. + // all responsibility for using this code. // init everything wxLongLongWx dividend = *this, @@ -599,7 +570,9 @@ wxLongLongWx& wxLongLongWx::operator/=(const wxLongLongWx& ll) Divide(ll, quotient, remainder); - return *this = quotient; + *this = quotient; + + return *this; } wxLongLongWx wxLongLongWx::operator%(const wxLongLongWx& ll) const @@ -632,27 +605,53 @@ void *wxLongLongWx::asArray(void) const return temp; } -#if wxUSE_STD_IOSTREAM +#endif // wxUSE_LONGLONG_WX -// input/output -ostream& operator<< (ostream& o, const wxLongLongWx& ll) +wxString +#if wxUSE_LONGLONG_NATIVE +wxLongLongNative::ToString() const +#else +wxLongLongWx::ToString() const +#endif { - char result[65]; + // TODO: this is awfully inefficient, anything better? + wxString result; - memset(result, 'A', 64); + wxLongLong ll = *this; - result[64] = '\0'; + bool neg; + if ( ll < 0 ) + { + ll.Negate(); + neg = TRUE; + } + else + { + neg = FALSE; + } - for (int i = 0; i < 32; i++) + while ( ll != 0 ) { - result[31 - i] = (char) ('0' + (int) ((ll.m_hi >> i) & 1)); - result[63 - i] = (char) ('0' + (int) ((ll.m_lo >> i) & 1)); + result.Prepend((wxChar)(_T('0') + (ll % 10).ToLong())); + ll /= 10; } - return o << result; + if ( result.empty() ) + result = _T('0'); + else if ( neg ) + result.Prepend(_T('-')); + + return result; } -#endif // wxUSE_STD_IOSTREAM -#endif // wxUSE_LONGLONG_NATIVE +#if wxUSE_STD_IOSTREAM + +// input/output +wxSTD ostream& operator<< (wxSTD ostream& o, const wxLongLong& ll) +{ + return o << ll.ToString(); +} + +#endif // wxUSE_STD_IOSTREAM #endif // wxUSE_LONGLONG