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; return;
} }
int bufsize; // Mantissa length is 17, so this is plenty
if (fabs(aValue) > 1) const int buflen = 20;
bufsize = (int)log10(fabs(aValue)) + 30; char buf[buflen];
else
bufsize = 30;
char* buf = new char[bufsize];
if (!buf) {
NS_ASSERTION(0, "out of memory");
return;
}
PRIntn intDigits, sign; PRIntn intDigits, sign;
char* endp; char* endp;
PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, bufsize-1); PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, buflen - 1);
if (sign) // compute length
aDest.Append(PRUnichar('-')); PRInt32 length = endp - buf;
if (length > intDigits) {
int i; // decimal point needed
for (i = 0; i < endp - buf; i++) { ++length;
if (i == intDigits) if (intDigits < 1) {
aDest.Append(PRUnichar('.')); // leading zeros, -intDigits + 1
aDest.Append(PRUnichar(buf[i])); 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;
// 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;
} }