From 7005feb19904a7d0a3f3a466ea2f6fb17ab6a474 Mon Sep 17 00:00:00 2001 From: "rickg%netscape.com" Date: Mon, 20 Sep 1999 05:10:32 +0000 Subject: [PATCH] fixed crasher in bufferroutines, and eliminated 1 costly call to sprintf git-svn-id: svn://10.0.0.236/trunk@48319 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/obsolete/nsString.cpp | 29 ++++++++++++++------- mozilla/string/obsolete/nsString2.cpp | 29 ++++++++++++++------- mozilla/xpcom/ds/bufferRoutines.h | 13 +++++---- mozilla/xpcom/ds/nsString.cpp | 29 ++++++++++++++------- mozilla/xpcom/ds/nsString2.cpp | 29 ++++++++++++++------- mozilla/xpcom/string/obsolete/nsString.cpp | 29 ++++++++++++++------- mozilla/xpcom/string/obsolete/nsString2.cpp | 29 ++++++++++++++------- 7 files changed, 128 insertions(+), 59 deletions(-) diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index 1b9109be5d6..ca5837b4103 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -952,16 +952,27 @@ nsCString& nsCString::Append(char aChar) { * @return */ nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); } diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index 2049c9d1b17..071ae6baca5 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -1124,16 +1124,27 @@ nsString& nsString::Append(PRUnichar aChar) { * @return */ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); } diff --git a/mozilla/xpcom/ds/bufferRoutines.h b/mozilla/xpcom/ds/bufferRoutines.h index 285316532e9..b02ffdbb684 100644 --- a/mozilla/xpcom/ds/bufferRoutines.h +++ b/mozilla/xpcom/ds/bufferRoutines.h @@ -64,10 +64,12 @@ inline PRUnichar GetCharAt(const char* aString,PRUint32 anIndex) { * @param aCount is the number of chars to be "cut" */ void ShiftCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint32 aCount) { - char* dst = aDest+anOffset; - char* src = aDest+anOffset+aCount; + char* dst = aDest+anOffset; + char* src = aDest+anOffset+aCount; + char* end = aDest+aLength+1; + PRInt32 cnt = end-src; - memmove(dst,src,aLength-anOffset); + memmove(dst,src,cnt); } /** @@ -97,8 +99,10 @@ void ShiftDoubleCharsLeft(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint3 PRUnichar* root=(PRUnichar*)aDest; PRUnichar* dst = root+anOffset; PRUnichar* src = root+anOffset+aCount; + PRUnichar* end = root+aLength+1; + PRInt32 cnt = end-src; - memmove(dst,src,sizeof(PRUnichar)*(aLength-anOffset)); + memmove(dst,src,cnt*sizeof(PRUnichar)); } @@ -116,7 +120,6 @@ void ShiftDoubleCharsRight(char* aDest,PRUint32 aLength,PRUint32 anOffset,PRUint PRUnichar* dst = root+anOffset+aCount; memmove(dst,src,sizeof(PRUnichar)*(aLength-anOffset)); - } diff --git a/mozilla/xpcom/ds/nsString.cpp b/mozilla/xpcom/ds/nsString.cpp index 1b9109be5d6..ca5837b4103 100644 --- a/mozilla/xpcom/ds/nsString.cpp +++ b/mozilla/xpcom/ds/nsString.cpp @@ -952,16 +952,27 @@ nsCString& nsCString::Append(char aChar) { * @return */ nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); } diff --git a/mozilla/xpcom/ds/nsString2.cpp b/mozilla/xpcom/ds/nsString2.cpp index 2049c9d1b17..071ae6baca5 100644 --- a/mozilla/xpcom/ds/nsString2.cpp +++ b/mozilla/xpcom/ds/nsString2.cpp @@ -1124,16 +1124,27 @@ nsString& nsString::Append(PRUnichar aChar) { * @return */ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); } diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index 1b9109be5d6..ca5837b4103 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -952,16 +952,27 @@ nsCString& nsCString::Append(char aChar) { * @return */ nsCString& nsCString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); } diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index 2049c9d1b17..071ae6baca5 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -1124,16 +1124,27 @@ nsString& nsString::Append(PRUnichar aChar) { * @return */ nsString& nsString::Append(PRInt32 aInteger,PRInt32 aRadix) { - char* fmt = "%d"; - if (8 == aRadix) { - fmt = "%o"; - } else if (16 == aRadix) { - fmt = "%x"; + char buf[128]={0,0}; + char* buffer=buf; + + ldiv_t r; /* result of val / base */ + if (aRadix> 36 || aRadix< 2) { /* no conversion if wrong base */ + return *this; } - char buf[40]; - // *** XX UNCOMMENT THIS LINE - //PR_snprintf(buf, sizeof(buf), fmt, aInteger); - sprintf(buf,fmt,aInteger); + + if (aInteger < 0) + *buffer++ = '-'; + r = ldiv (labs(aInteger), aRadix); + + /* output digits of val/base first */ + if (r.quot > 0) + buffer = ltoa ( r.quot, buf, aRadix); + + /* output last digit */ + int len=strlen(buffer); + buf[len] = "0123456789abcdefghijklmnopqrstuvwxyz"[(int)r.rem]; + buf[len+1] =0; + return Append(buf); }