bug 190499, xpath number 2 string conversion buggy for small numbers, changed implementation to use moz string api, r=sicking, sr=jag, a=asa

This commit is contained in:
axel%pike.org 2005-11-02 07:39:30 +00:00
Родитель 99abd54c49
Коммит fbb5d58bc3
1 изменённых файлов: 52 добавлений и 25 удалений

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

@ -253,34 +253,61 @@ void Double::toString(double aValue, nsAString& aDest)
return;
}
int bufsize;
if (fabs(aValue) > 1)
bufsize = (int)log10(fabs(aValue)) + 30;
else
bufsize = 30;
char* buf = new char[bufsize];
if (!buf) {
NS_ASSERTION(0, "out of memory");
return;
}
// Mantissa length is 17, so this is plenty
const int buflen = 20;
char buf[buflen];
PRIntn intDigits, sign;
char* endp;
PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, bufsize-1);
if (sign)
aDest.Append(PRUnichar('-'));
PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, buflen - 1);
// compute length
PRInt32 length = endp - buf;
if (length > intDigits) {
// decimal point needed
++length;
if (intDigits < 1) {
// leading zeros, -intDigits + 1
length += 1 - intDigits;
}
}
else {
// trailing zeros, total length given by intDigits
length = intDigits;
}
if (aValue < 0)
++length;
PRUint32 oldlength = aDest.Length();
aDest.SetLength(oldlength + length); // grow the string
nsAString::iterator dest;
aDest.BeginWriting(dest).advance(PRInt32(oldlength));
if (aValue < 0) {
*dest = '-'; ++dest;
}
int i;
for (i = 0; i < endp - buf; i++) {
if (i == intDigits)
aDest.Append(PRUnichar('.'));
aDest.Append(PRUnichar(buf[i]));
// leading zeros
if (intDigits < 1) {
*dest = '0'; ++dest;
*dest = '.'; ++dest;
for (i = 0; i > intDigits; --i) {
*dest = '0'; ++dest;
}
}
// mantissa
int firstlen = PR_MIN(intDigits, endp - buf);
for (i = 0; i < firstlen; i++) {
*dest = buf[i]; ++dest;
}
if (i < endp - buf) {
if (i > 0) {
*dest = '.'; ++dest;
}
for (; i < endp - buf; i++) {
*dest = buf[i]; ++dest;
}
}
// trailing zeros
for (; i < intDigits; i++) {
*dest = '0'; ++dest;
}
for (; i < intDigits; i++)
aDest.Append(PRUnichar('0'));
delete [] buf;
}