Bug 608914 part 1. Remove the nsString overloads of AppendInt and move AppendFloat up to nsAString. r=bsmedberg
authorBoris Zbarsky <bzbarsky@mit.edu>
Tue, 30 Nov 2010 13:18:15 -0500
changeset 58382 f61c6c4ba5be28ff8990d8ac8a54d0ebf2f95821
parent 58381 fa8acee0abe8536989611f586f245ded675c6937
child 58383 51125803eb3196b71ff4655c17770793a265a222
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersbsmedberg
bugs608914
milestone2.0b8pre
Bug 608914 part 1. Remove the nsString overloads of AppendInt and move AppendFloat up to nsAString. r=bsmedberg
xpcom/string/public/nsTString.h
xpcom/string/public/nsTSubstring.h
xpcom/string/src/nsStringObsolete.cpp
xpcom/string/src/nsTSubstring.cpp
--- a/xpcom/string/public/nsTString.h
+++ b/xpcom/string/public/nsTString.h
@@ -379,44 +379,16 @@ class nsTString_CharT : public nsTSubstr
          */
 
       NS_COM void AssignWithConversion( const nsTAString_IncompatibleCharT& aString );
       NS_COM void AssignWithConversion( const incompatible_char_type* aData, PRInt32 aLength=-1 );
 
       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
 
 
     protected:
 
       explicit
       nsTString_CharT( PRUint32 flags )
         : substring_type(flags) {}
--- a/xpcom/string/public/nsTSubstring.h
+++ b/xpcom/string/public/nsTSubstring.h
@@ -413,16 +413,27 @@ class nsTSubstring_CharT
       void AppendInt( PRUint64 aInteger )
                  { AppendPrintf( "%llu", aInteger ); }
       void AppendInt( PRUint64 aInteger, int aRadix )
         {
           const char *fmt = aRadix == 10 ? "%llu" : aRadix == 8 ? "%llo" : "%llx";
           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.
 #ifdef NS_DISABLE_LITERAL_TEMPLATE
       void AppendLiteral( const char* str )
                   { AppendASCII(str); }
 #else
       template<int N>
--- a/xpcom/string/src/nsStringObsolete.cpp
+++ b/xpcom/string/src/nsStringObsolete.cpp
@@ -46,17 +46,16 @@
 #if MOZ_STRING_WITH_OBSOLETE_API
 
 #include "nsDependentString.h"
 #include "nsDependentSubstring.h"
 #include "nsReadableUtils.h"
 #include "nsCRT.h"
 #include "nsUTF8Utils.h"
 #include "prdtoa.h"
-#include "prprf.h"
 
 /* ***** BEGIN RICKG BLOCK *****
  *
  * NOTE: This section of code was extracted from rickg's bufferRoutines.h file.
  *       For the most part it remains unmodified.  We want to eliminate (or at
  *       least clean up) this code at some point.  If you find the formatting
  *       in this section somewhat inconsistent, don't blame me! ;-)
  */
@@ -795,109 +794,16 @@ RFindCharInSet( const CharT* data, PRUin
               return iter - data; // found it!  return index of the found char.
 
             setChar = *(++charInSet);
           }
       }
     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|:
    * 
    * upon return,
    *   |offset| specifies start of search range
    *   |count| specifies length of search range
    */ 
 static void
@@ -1166,138 +1072,9 @@ nsCString::AppendWithConversion( const n
   }
 
 void
 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
--- a/xpcom/string/src/nsTSubstring.cpp
+++ b/xpcom/string/src/nsTSubstring.cpp
@@ -30,16 +30,17 @@
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * 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,
                                         PRUint32 flags)
   : mData(data),
     mLength(length),
     mFlags(flags)
   {
@@ -737,8 +738,116 @@ void nsTSubstring_CharT::AppendPrintf( c
   {
     char buf[32];
     va_list ap;
     va_start(ap, format);
     PRUint32 len = PR_vsnprintf(buf, sizeof(buf), format, ap);
     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);
+}
+