bug 240106, add 64-bit version of nsC?String::AppendInt

r=darin sr=dbaron
This commit is contained in:
cbiesinger%web.de 2004-04-13 19:13:49 +00:00
Родитель 76d1b26cbc
Коммит 66ec9ed0ab
3 изменённых файлов: 135 добавлений и 36 удалений

Просмотреть файл

@ -434,9 +434,23 @@ class nsTString_CharT : public nsTSubstring_CharT
/**
* Append the given integer to this string
*/
NS_COM void AppendInt( PRInt32 aInteger, PRInt32 aRadix=kRadix10 ); //radix=8,10 or 16
/**
* Append the given unsigned integer to this string
*/
NS_COM void AppendInt( PRUint32 aInteger, PRInt32 aRadix = kRadix10 )
{
return AppendInt(PRInt32(aInteger), aRadix);
}
/**
* 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.
*/
NS_COM void AppendInt( PRInt64 aInteger, PRInt32 aRadix=kRadix10 );
/**
* Append the given float to this string
*/

Просмотреть файл

@ -800,36 +800,6 @@ RFindCharInSet( const CharT* data, PRUint32 dataLen, const SetCharT* set )
return kNotFound;
}
// this is rickg's original code from AppendInt
static void
IntegerToCString( PRInt32 aInteger, PRInt32 aRadix, char *aBuf )
{
PRUint32 theInt=(PRUint32)aInteger;
PRInt32 radices[] = {1000000000,268435456};
PRInt32 mask1=radices[16==aRadix];
PRInt32 charpos=0;
if(aInteger<0) {
theInt*=-1;
if(10==aRadix) {
aBuf[charpos++]='-';
}
else theInt=(int)~(theInt-1);
}
PRBool isfirst=PR_TRUE;
while(mask1>=1) {
PRInt32 theDiv=theInt/mask1;
if((theDiv) || (!isfirst)) {
aBuf[charpos++]="0123456789abcdef"[theDiv];
isfirst=PR_FALSE;
}
theInt-=theDiv*mask1;
mask1/=aRadix;
}
}
/**
* This is a copy of |PR_cnvtf| with a bug fixed. (The second argument
* of PR_dtoa is 2 rather than 1.)
@ -1283,19 +1253,82 @@ nsString::InsertWithConversion( const char* aData, PRUint32 aOffset, PRInt32 aCo
void
nsCString::AppendInt( PRInt32 aInteger, PRInt32 aRadix )
{
char buf[] = {'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
IntegerToCString(aInteger, aRadix, buf);
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[] = {'0',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
IntegerToCString(aInteger, aRadix, buf);
AppendWithConversion(buf);
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

Просмотреть файл

@ -464,6 +464,57 @@ PRBool test_substring()
return PR_TRUE;
}
PRBool test_appendint64()
{
nsCString str;
PRInt64 max = LL_MaxInt();
static const char max_expected[] = "9223372036854775807";
PRInt64 min = LL_MinInt();
static const char min_expected[] = "-9223372036854775808";
static const char min_expected_oct[] = "1000000000000000000000";
PRInt64 maxint_plus1 = LL_INIT(1, 0);
static const char maxint_plus1_expected[] = "4294967296";
static const char maxint_plus1_expected_x[] = "100000000";
str.AppendInt(max);
if (!str.Equals(max_expected)) {
fprintf(stderr, "Error appending LL_MaxInt(): Got %s\n", str.get());
return PR_FALSE;
}
str.Truncate();
str.AppendInt(min);
if (!str.Equals(min_expected)) {
fprintf(stderr, "Error appending LL_MinInt(): Got %s\n", str.get());
return PR_FALSE;
}
str.Truncate();
str.AppendInt(min, 8);
if (!str.Equals(min_expected_oct)) {
fprintf(stderr, "Error appending LL_MinInt() (oct): Got %s\n", str.get());
return PR_FALSE;
}
str.Truncate();
str.AppendInt(maxint_plus1);
if (!str.Equals(maxint_plus1_expected)) {
fprintf(stderr, "Error appending PR_UINT32_MAX + 1: Got %s\n", str.get());
return PR_FALSE;
}
str.Truncate();
str.AppendInt(maxint_plus1, 16);
if (!str.Equals(maxint_plus1_expected_x)) {
fprintf(stderr, "Error appending PR_UINT32_MAX + 1 (hex): Got %s\n", str.get());
return PR_FALSE;
}
return PR_TRUE;
}
//----
typedef PRBool (*TestFunc)();
@ -498,6 +549,7 @@ tests[] =
{ "test_empty_assign", test_empty_assign },
{ "test_set_length", test_set_length },
{ "test_substring", test_substring },
{ "test_appendint64", test_appendint64 },
{ nsnull, nsnull }
};