From 4dc0ddb04ac9782e01af62f88efeb8dfba3f28bf Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 30 Nov 2010 13:18:15 -0500 Subject: [PATCH] Bug 608914 part 1. Remove the nsString overloads of AppendInt and move AppendFloat up to nsAString. r=bsmedberg --- xpcom/string/public/nsTString.h | 28 ---- xpcom/string/public/nsTSubstring.h | 11 ++ xpcom/string/src/nsStringObsolete.cpp | 223 -------------------------- xpcom/string/src/nsTSubstring.cpp | 109 +++++++++++++ 4 files changed, 120 insertions(+), 251 deletions(-) diff --git a/xpcom/string/public/nsTString.h b/xpcom/string/public/nsTString.h index bbe4a78a9c4..07c3c002007 100644 --- a/xpcom/string/public/nsTString.h +++ b/xpcom/string/public/nsTString.h @@ -384,34 +384,6 @@ class nsTString_CharT : public nsTSubstring_CharT NS_COM void AppendWithConversion( const nsTAString_IncompatibleCharT& aString ); NS_COM void AppendWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 ); - using nsTSubstring_CharT::AppendInt; - - /** - * Append the given integer to this string - * @param aInteger The integer to append - * @param aRadix The radix to use; can be 8, 10 or 16. - * @deprecated Use AppendInt( PRInt32 aInteger ) or - * AppendInt( PRUint32 aInteger, PRInt32 aRadix = 10 ) - */ - NS_COM void AppendInt( PRInt32 aInteger, PRInt32 aRadix ); //radix=8,10 or 16 - - /** - * Append the given 64-bit integer to this string. - * @param aInteger The integer to append - * @param aRadix The radix to use; can be 8, 10 or 16. - * @deprecated Use AppendInt( PRInt64 aInteger ) or - * AppendInt( PRUint64 aInteger, PRInt32 aRadix = 10 ) - */ - NS_COM void AppendInt( PRInt64 aInteger, PRInt32 aRadix ); - - /** - * Append the given float to this string - */ - - NS_COM void AppendFloat( float aFloat ); - - NS_COM void AppendFloat( double aFloat ); - #endif // !MOZ_STRING_WITH_OBSOLETE_API diff --git a/xpcom/string/public/nsTSubstring.h b/xpcom/string/public/nsTSubstring.h index fb8bc841a4c..a9d97eccd87 100644 --- a/xpcom/string/public/nsTSubstring.h +++ b/xpcom/string/public/nsTSubstring.h @@ -418,6 +418,17 @@ class nsTSubstring_CharT AppendPrintf( fmt, aInteger ); } + /** + * Append the given float to this string + */ + void AppendFloat( float aFloat ) + { DoAppendFloat(aFloat, 6); } + void AppendFloat( double aFloat ) + { DoAppendFloat(aFloat, 15); } + private: + NS_COM void NS_FASTCALL DoAppendFloat( double aFloat, int digits ); + public: + // AppendLiteral must ONLY be applied to an actual literal string. // Do not attempt to use it with a regular char* pointer, or with a char // array variable. Use AppendASCII for those. diff --git a/xpcom/string/src/nsStringObsolete.cpp b/xpcom/string/src/nsStringObsolete.cpp index 9dd13d7e6a7..355c4284e21 100644 --- a/xpcom/string/src/nsStringObsolete.cpp +++ b/xpcom/string/src/nsStringObsolete.cpp @@ -51,7 +51,6 @@ #include "nsCRT.h" #include "nsUTF8Utils.h" #include "prdtoa.h" -#include "prprf.h" /* ***** BEGIN RICKG BLOCK ***** * @@ -800,99 +799,6 @@ RFindCharInSet( const CharT* data, PRUint32 dataLen, const SetCharT* set ) return kNotFound; } -/** - * This is a copy of |PR_cnvtf| with a bug fixed. (The second argument - * of PR_dtoa is 2 rather than 1.) - * - * XXX(darin): if this is the right thing, then why wasn't it fixed in NSPR?!? - */ -void -Modified_cnvtf(char *buf, int bufsz, int prcsn, double fval) -{ - PRIntn decpt, sign, numdigits; - char *num, *nump; - char *bufp = buf; - char *endnum; - - /* If anything fails, we store an empty string in 'buf' */ - num = (char*)malloc(bufsz); - if (num == NULL) { - buf[0] = '\0'; - return; - } - if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, bufsz) - == PR_FAILURE) { - buf[0] = '\0'; - goto done; - } - numdigits = endnum - num; - nump = num; - - /* - * The NSPR code had a fancy way of checking that we weren't dealing - * with -0.0 or -NaN, but I'll just use < instead. - * XXX Should we check !isnan(fval) as well? Is it portable? We - * probably don't need to bother since NAN isn't portable. - */ - if (sign && fval < 0.0f) { - *bufp++ = '-'; - } - - if (decpt == 9999) { - while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */ - goto done; - } - - if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) { - *bufp++ = *nump++; - if (numdigits != 1) { - *bufp++ = '.'; - } - - while (*nump != '\0') { - *bufp++ = *nump++; - } - *bufp++ = 'e'; - PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1); - } - else if (decpt >= 0) { - if (decpt == 0) { - *bufp++ = '0'; - } - else { - while (decpt--) { - if (*nump != '\0') { - *bufp++ = *nump++; - } - else { - *bufp++ = '0'; - } - } - } - if (*nump != '\0') { - *bufp++ = '.'; - while (*nump != '\0') { - *bufp++ = *nump++; - } - } - *bufp++ = '\0'; - } - else if (decpt < 0) { - *bufp++ = '0'; - *bufp++ = '.'; - while (decpt++) { - *bufp++ = '0'; - } - - while (*nump != '\0') { - *bufp++ = *nump++; - } - *bufp++ = '\0'; - } -done: - free(num); -} - /** * this method changes the meaning of |offset| and |count|: * @@ -1171,133 +1077,4 @@ nsString::AppendWithConversion( const nsACString& aData ) AppendASCIItoUTF16(aData, *this); } - - /** - * nsTString::AppendInt - */ - -void -nsCString::AppendInt( PRInt32 aInteger, PRInt32 aRadix ) - { - char buf[20]; - const char* fmt; - switch (aRadix) { - case 8: - fmt = "%o"; - break; - case 10: - fmt = "%d"; - break; - default: - NS_ASSERTION(aRadix == 16, "Invalid radix!"); - fmt = "%x"; - } - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - Append(buf); - } - -void -nsString::AppendInt( PRInt32 aInteger, PRInt32 aRadix ) - { - char buf[20]; - const char* fmt; - switch (aRadix) { - case 8: - fmt = "%o"; - break; - case 10: - fmt = "%d"; - break; - default: - NS_ASSERTION(aRadix == 16, "Invalid radix!"); - fmt = "%x"; - } - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - AppendASCIItoUTF16(buf, *this); - } - -void -nsCString::AppendInt( PRInt64 aInteger, PRInt32 aRadix ) - { - char buf[30]; - const char* fmt; - switch (aRadix) { - case 8: - fmt = "%llo"; - break; - case 10: - fmt = "%lld"; - break; - default: - NS_ASSERTION(aRadix == 16, "Invalid radix!"); - fmt = "%llx"; - } - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - Append(buf); - } - -void -nsString::AppendInt( PRInt64 aInteger, PRInt32 aRadix ) - { - char buf[30]; - const char* fmt; - switch (aRadix) { - case 8: - fmt = "%llo"; - break; - case 10: - fmt = "%lld"; - break; - default: - NS_ASSERTION(aRadix == 16, "Invalid radix!"); - fmt = "%llx"; - } - PR_snprintf(buf, sizeof(buf), fmt, aInteger); - AppendASCIItoUTF16(buf, *this); - } - - /** - * nsTString::AppendFloat - */ - -void -nsCString::AppendFloat( float aFloat ) - { - char buf[40]; - // Use Modified_cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - Modified_cnvtf(buf, sizeof(buf), 6, aFloat); - Append(buf); - } - -void -nsString::AppendFloat( float aFloat ) - { - char buf[40]; - // Use Modified_cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - Modified_cnvtf(buf, sizeof(buf), 6, aFloat); - AppendWithConversion(buf); - } - -void -nsCString::AppendFloat( double aFloat ) - { - char buf[40]; - // Use Modified_cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - Modified_cnvtf(buf, sizeof(buf), 15, aFloat); - Append(buf); - } - -void -nsString::AppendFloat( double aFloat ) - { - char buf[40]; - // Use Modified_cnvtf, which is locale-insensitive, instead of the - // locale-sensitive PR_snprintf or sprintf(3) - Modified_cnvtf(buf, sizeof(buf), 15, aFloat); - AppendWithConversion(buf); - } - #endif // !MOZ_STRING_WITH_OBSOLETE_API diff --git a/xpcom/string/src/nsTSubstring.cpp b/xpcom/string/src/nsTSubstring.cpp index 1f31013e6c2..7fb742388c7 100644 --- a/xpcom/string/src/nsTSubstring.cpp +++ b/xpcom/string/src/nsTSubstring.cpp @@ -35,6 +35,7 @@ * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ +#include "prdtoa.h" #ifdef XPCOM_STRING_CONSTRUCTOR_OUT_OF_LINE nsTSubstring_CharT::nsTSubstring_CharT( char_type *data, size_type length, @@ -742,3 +743,111 @@ void nsTSubstring_CharT::AppendPrintf( const char* format, ...) AppendASCII(buf, len); va_end(ap); } + + +/* hack to make sure we define Modified_cnvtf only once */ +#ifdef CharT_is_PRUnichar +/** + * This is a copy of |PR_cnvtf| with a bug fixed. (The second argument + * of PR_dtoa is 2 rather than 1.) + * + * XXX(darin): if this is the right thing, then why wasn't it fixed in NSPR?!? + */ +static void +Modified_cnvtf(char *buf, int bufsz, int prcsn, double fval) +{ + PRIntn decpt, sign, numdigits; + char *num, *nump; + char *bufp = buf; + char *endnum; + + /* If anything fails, we store an empty string in 'buf' */ + num = (char*)malloc(bufsz); + if (num == NULL) { + buf[0] = '\0'; + return; + } + if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, bufsz) + == PR_FAILURE) { + buf[0] = '\0'; + goto done; + } + numdigits = endnum - num; + nump = num; + + /* + * The NSPR code had a fancy way of checking that we weren't dealing + * with -0.0 or -NaN, but I'll just use < instead. + * XXX Should we check !isnan(fval) as well? Is it portable? We + * probably don't need to bother since NAN isn't portable. + */ + if (sign && fval < 0.0f) { + *bufp++ = '-'; + } + + if (decpt == 9999) { + while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */ + goto done; + } + + if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) { + *bufp++ = *nump++; + if (numdigits != 1) { + *bufp++ = '.'; + } + + while (*nump != '\0') { + *bufp++ = *nump++; + } + *bufp++ = 'e'; + PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1); + } + else if (decpt >= 0) { + if (decpt == 0) { + *bufp++ = '0'; + } + else { + while (decpt--) { + if (*nump != '\0') { + *bufp++ = *nump++; + } + else { + *bufp++ = '0'; + } + } + } + if (*nump != '\0') { + *bufp++ = '.'; + while (*nump != '\0') { + *bufp++ = *nump++; + } + } + *bufp++ = '\0'; + } + else if (decpt < 0) { + *bufp++ = '0'; + *bufp++ = '.'; + while (decpt++) { + *bufp++ = '0'; + } + + while (*nump != '\0') { + *bufp++ = *nump++; + } + *bufp++ = '\0'; + } +done: + free(num); +} +#endif /* CharT_is_PRUnichar */ + +void +nsTSubstring_CharT::DoAppendFloat( double aFloat, int digits ) +{ + char buf[40]; + // Use Modified_cnvtf, which is locale-insensitive, instead of the + // locale-sensitive PR_snprintf or sprintf(3) + Modified_cnvtf(buf, sizeof(buf), digits, aFloat); + AppendASCII(buf); +} +