diff --git a/xpcom/string/public/nsTString.h b/xpcom/string/public/nsTString.h index 08c0997f6a7..57b9e9307b2 100644 --- a/xpcom/string/public/nsTString.h +++ b/xpcom/string/public/nsTString.h @@ -410,6 +410,8 @@ class nsTString_CharT : public nsTSubstring_CharT * 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/src/nsStringObsolete.cpp b/xpcom/string/src/nsStringObsolete.cpp index 31752f66182..9dd13d7e6a7 100644 --- a/xpcom/string/src/nsStringObsolete.cpp +++ b/xpcom/string/src/nsStringObsolete.cpp @@ -1261,7 +1261,7 @@ nsString::AppendInt( PRInt64 aInteger, PRInt32 aRadix ) */ void -nsCString::AppendFloat( double aFloat ) +nsCString::AppendFloat( float aFloat ) { char buf[40]; // Use Modified_cnvtf, which is locale-insensitive, instead of the @@ -1271,7 +1271,7 @@ nsCString::AppendFloat( double aFloat ) } void -nsString::AppendFloat( double aFloat ) +nsString::AppendFloat( float aFloat ) { char buf[40]; // Use Modified_cnvtf, which is locale-insensitive, instead of the @@ -1280,4 +1280,24 @@ nsString::AppendFloat( double 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/tests/TestStrings.cpp b/xpcom/tests/TestStrings.cpp index fedf12f33eb..8ece2656372 100644 --- a/xpcom/tests/TestStrings.cpp +++ b/xpcom/tests/TestStrings.cpp @@ -513,6 +513,32 @@ PRBool test_appendint64() } + return PR_TRUE; + } + +PRBool test_appendfloat() + { + nsCString str; + double bigdouble = 11223344556.66; + static const char double_expected[] = "11223344556.66"; + static const char float_expected[] = "0.01"; + + // AppendFloat is used to append doubles, therefore the precision must be + // large enough (see bug 327719) + str.AppendFloat( bigdouble ); + if (!str.Equals(double_expected)) { + fprintf(stderr, "Error appending a big double: Got %s\n", str.get()); + return PR_FALSE; + } + + str.Truncate(); + // AppendFloat is used to append floats (bug 327719 #27) + str.AppendFloat( 0.1f * 0.1f ); + if (!str.Equals(float_expected)) { + fprintf(stderr, "Error appending a float: Got %s\n", str.get()); + return PR_FALSE; + } + return PR_TRUE; } @@ -710,6 +736,7 @@ tests[] = { "test_set_length", test_set_length }, { "test_substring", test_substring }, { "test_appendint64", test_appendint64 }, + { "test_appendfloat", test_appendfloat }, { "test_findcharinset", test_findcharinset }, { "test_rfindcharinset", test_rfindcharinset }, { "test_stringbuffer", test_stringbuffer },