VirtualBox

Changeset 102006 in vbox for trunk/src/libs/xpcom18a4


Ignore:
Timestamp:
Nov 8, 2023 7:17:10 PM (15 months ago)
Author:
vboxsync
Message:

libs/xpcom: Use RTStrFormatR64() and RTStrToFloatEx() instead of PR_strtod() and PR_dtoa() in nsStringObsolete.cpp (preparation for removal of prdtoa.h), kinda untested, bugref:10545

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/xpcom/string/src/nsStringObsolete.cpp

    r31259 r102006  
    5151#include "nsCRT.h"
    5252#include "nsUTF8Utils.h"
    53 #include "prdtoa.h"
    5453#include "prprf.h"
    55 #ifdef VBOX_USE_IPRT_IN_XPCOM
    56 # include <iprt/mem.h>
    57 #endif
     54
     55#include <iprt/assert.h>
     56#include <iprt/errcore.h>
     57#include <iprt/mem.h>
     58#include <iprt/string.h>
    5859
    5960/* ***** BEGIN RICKG BLOCK *****
     
    803804    return kNotFound;
    804805  }
    805 
    806 /**
    807  * This is a copy of |PR_cnvtf| with a bug fixed.  (The second argument
    808  * of PR_dtoa is 2 rather than 1.)
    809  *
    810  * XXXdarin if this is the right thing, then why wasn't it fixed in NSPR?!?
    811  */
    812 void
    813 Modified_cnvtf(char *buf, int bufsz, int prcsn, double fval)
    814 {
    815   PRIntn decpt, sign, numdigits;
    816   char *num, *nump;
    817   char *bufp = buf;
    818   char *endnum;
    819 
    820   /* If anything fails, we store an empty string in 'buf' */
    821 #ifdef VBOX_USE_IPRT_IN_XPCOM
    822   num = (char*)RTMemAlloc(bufsz);
    823 #else
    824   num = (char*)malloc(bufsz);
    825 #endif
    826   if (num == NULL) {
    827     buf[0] = '\0';
    828     return;
    829   }
    830   if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, bufsz)
    831       == PR_FAILURE) {
    832     buf[0] = '\0';
    833     goto done;
    834   }
    835   numdigits = endnum - num;
    836   nump = num;
    837 
    838   /*
    839    * The NSPR code had a fancy way of checking that we weren't dealing
    840    * with -0.0 or -NaN, but I'll just use < instead.
    841    * XXX Should we check !isnan(fval) as well?  Is it portable?  We
    842    * probably don't need to bother since NAN isn't portable.
    843    */
    844   if (sign && fval < 0.0f) {
    845     *bufp++ = '-';
    846   }
    847 
    848   if (decpt == 9999) {
    849     while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */
    850     goto done;
    851   }
    852 
    853   if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) {
    854     *bufp++ = *nump++;
    855     if (numdigits != 1) {
    856       *bufp++ = '.';
    857     }
    858 
    859     while (*nump != '\0') {
    860       *bufp++ = *nump++;
    861     }
    862     *bufp++ = 'e';
    863     PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1);
    864   }
    865   else if (decpt >= 0) {
    866     if (decpt == 0) {
    867       *bufp++ = '0';
    868     }
    869     else {
    870       while (decpt--) {
    871         if (*nump != '\0') {
    872           *bufp++ = *nump++;
    873         }
    874         else {
    875           *bufp++ = '0';
    876         }
    877       }
    878     }
    879     if (*nump != '\0') {
    880       *bufp++ = '.';
    881       while (*nump != '\0') {
    882         *bufp++ = *nump++;
    883       }
    884     }
    885     *bufp++ = '\0';
    886   }
    887   else if (decpt < 0) {
    888     *bufp++ = '0';
    889     *bufp++ = '.';
    890     while (decpt++) {
    891       *bufp++ = '0';
    892     }
    893 
    894     while (*nump != '\0') {
    895       *bufp++ = *nump++;
    896     }
    897     *bufp++ = '\0';
    898   }
    899 done:
    900 #ifdef VBOX_USE_IPRT_IN_XPCOM
    901   RTMemFree(num);
    902 #else
    903   free(num);
    904 #endif
    905 }
    906806
    907807  /**
     
    11411041    if (mLength > 0)
    11421042      {
    1143         char *conv_stopped;
    1144         const char *str = mData;
    1145         // Use PR_strtod, not strtod, since we don't want locale involved.
    1146         res = (float)PR_strtod(str, &conv_stopped);
    1147         if (conv_stopped == str+mLength)
     1043        int vrc = RTStrToFloatEx(mData, NULL /*ppstNext*/, 0 /*cchMax*/, &res);
     1044        if (vrc == VINF_SUCCESS)
    11481045          *aErrorCode = (PRInt32) NS_OK;
    1149         else // Not all the string was scanned
     1046        else // Illegal value
    11501047          *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
    11511048      }
     
    11651062    if (mLength > 0 && mLength < sizeof(buf))
    11661063      {
    1167         char *conv_stopped;
    11681064        const char *str = ToCString(buf, sizeof(buf));
    1169         // Use PR_strtod, not strtod, since we don't want locale involved.
    1170         res = (float)PR_strtod(str, &conv_stopped);
    1171         if (conv_stopped == str+mLength)
     1065        int vrc = RTStrToFloatEx(str, NULL /*ppstNext*/, 0 /*cchMax*/, &res);
     1066        if (vrc == VINF_SUCCESS)
    11721067          *aErrorCode = (PRInt32) NS_OK;
    1173         else // Not all the string was scanned
     1068        else // Illegal value
    11741069          *aErrorCode = (PRInt32) NS_ERROR_ILLEGAL_VALUE;
    11751070      }
     
    13091204  {
    13101205    char buf[40];
    1311     // Use Modified_cnvtf, which is locale-insensitive, instead of the
    1312     // locale-sensitive PR_snprintf or sprintf(3)
    1313     Modified_cnvtf(buf, sizeof(buf), 6, aFloat);
     1206    RTFLOAT64U r64;
     1207    r64.rd = aFloat;
     1208    ssize_t cch = RTStrFormatR64(buf, sizeof(buf), &r64, 0, 6 /*cchPrecision*/, 0 /*fFlags*/);
     1209    Assert(cch > 0);
    13141210    Append(buf);
    13151211  }
     
    13191215  {
    13201216    char buf[40];
    1321     // Use Modified_cnvtf, which is locale-insensitive, instead of the
    1322     // locale-sensitive PR_snprintf or sprintf(3)
    1323     Modified_cnvtf(buf, sizeof(buf), 6, aFloat);
     1217    RTFLOAT64U r64;
     1218    r64.rd = aFloat;
     1219    ssize_t cch = RTStrFormatR64(buf, sizeof(buf), &r64, 0, 6 /*cchPrecision*/, 0 /*fFlags*/);
     1220    Assert(cch > 0);
    13241221    AppendWithConversion(buf);
    13251222  }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette