VirtualBox

Ignore:
Timestamp:
May 23, 2009 3:42:57 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
47644
Message:

IPRT: Implemented thousand separators for the string formatting code and RTStrFormatNumber. (For instance %'u.)

Location:
trunk/src/VBox/Runtime/common/string
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/strformat.cpp

    r14066 r19942  
    170170static int rtStrFormatNumber(char *psz, KSIZE64 ullValue, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags)
    171171{
    172     const char *    pachDigits = "0123456789abcdef";
    173     char *          pszStart = psz;
     172    const char     *pachDigits = "0123456789abcdef";
     173    char           *pszStart = psz;
    174174    int             cchValue;
    175175    unsigned long   ul;
    176 #if 0
    177     unsigned long   ullow;
    178 #endif
    179176    int             i;
    180177    int             j;
    181178
    182 /** @todo Formatting of 64 bit numbers is broken, fix it! */
    183 
    184179    /*
    185      * Validate and addjust input...
     180     * Validate and adjust input...
    186181     */
    187 /** @todo r=bird: Dmitry, who is calling this code with uiBase == 0? */
    188     if (uiBase == 0)
    189         uiBase = 10;
    190     kASSERT((uiBase >= 2 || uiBase <= 16));
     182    Assert(uiBase >= 2 || uiBase <= 16);
    191183    if (fFlags & RTSTR_F_CAPITAL)
    192184        pachDigits = "0123456789ABCDEF";
    193185    if (fFlags & RTSTR_F_LEFT)
    194186        fFlags &= ~RTSTR_F_ZEROPAD;
     187    if (    (fFlags & RTSTR_F_THOUSAND_SEP)
     188        &&  (   uiBase != 10
     189             || (fFlags & RTSTR_F_ZEROPAD))) /** @todo implement RTSTR_F_ZEROPAD + RTSTR_F_THOUSAND_SEP. */
     190        fFlags &= ~RTSTR_F_THOUSAND_SEP;
    195191
    196192    /*
     
    218214        } while (ul);
    219215    }
     216    if (fFlags & RTSTR_F_THOUSAND_SEP)
     217    {
     218        if (cchValue <= 3)
     219            fFlags &= ~RTSTR_F_THOUSAND_SEP;
     220        else
     221            cchValue += cchValue / 3 - (cchValue % 3 == 0);
     222    }
    220223
    221224    /*
     
    281284    {
    282285        uint64_t    u64 = *(uint64_t *)(void *)&ullValue;
    283         do
    284         {
    285             psz[i--] = pachDigits[u64 % uiBase];
    286             u64 /= uiBase;
    287         } while (u64);
     286        if (fFlags & RTSTR_F_THOUSAND_SEP)
     287        {   
     288            do
     289            {
     290                if ((-i - 1) % 4 == 3)
     291                    psz[i--] = ' ';
     292                psz[i--] = pachDigits[u64 % uiBase];
     293                u64 /= uiBase;
     294            } while (u64);
     295        }
     296        else
     297        {   
     298            do
     299            {
     300                psz[i--] = pachDigits[u64 % uiBase];
     301                u64 /= uiBase;
     302            } while (u64);
     303        }
    288304    }
    289305    else
    290306    {
    291307        ul = (fFlags & RTSTR_F_VALSIGNED) && (ullValue.ulLo & 0x80000000) ? -(int32_t)ullValue.ulLo : ullValue.ulLo;
    292         do
     308        if (fFlags & RTSTR_F_THOUSAND_SEP)
    293309        {
    294             psz[i--] = pachDigits[ul % uiBase];
    295             ul /= uiBase;
    296         } while (ul);
     310            do
     311            {
     312                if ((-i - 1) % 4 == 3)
     313                    psz[i--] = ' ';
     314                psz[i--] = pachDigits[ul % uiBase];
     315                ul /= uiBase;
     316            } while (ul);
     317        }
     318        else
     319        {
     320            do
     321            {
     322                psz[i--] = pachDigits[ul % uiBase];
     323                ul /= uiBase;
     324            } while (ul);
     325        }
    297326    }
    298 
    299327
    300328    /*
     
    358386                    switch (*pszFormat++)
    359387                    {
    360                         case '#':   fFlags |= RTSTR_F_SPECIAL;   continue;
    361                         case '-':   fFlags |= RTSTR_F_LEFT;      continue;
    362                         case '+':   fFlags |= RTSTR_F_PLUS;      continue;
    363                         case ' ':   fFlags |= RTSTR_F_BLANK;     continue;
    364                         case '0':   fFlags |= RTSTR_F_ZEROPAD;   continue;
     388                        case '#':   fFlags |= RTSTR_F_SPECIAL;      continue;
     389                        case '-':   fFlags |= RTSTR_F_LEFT;         continue;
     390                        case '+':   fFlags |= RTSTR_F_PLUS;         continue;
     391                        case ' ':   fFlags |= RTSTR_F_BLANK;        continue;
     392                        case '0':   fFlags |= RTSTR_F_ZEROPAD;      continue;
     393                        case '\'':  fFlags |= RTSTR_F_THOUSAND_SEP; continue;
    365394                    }
    366395                    pszFormat--;
  • trunk/src/VBox/Runtime/common/string/strformatrt.cpp

    r13837 r19942  
    414414                    case RTSF_FP16:
    415415                    {
    416                         fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
     416                        fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
    417417                        cch = RTStrFormatNumber(&szBuf[0], u.fp16.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
    418418                        Assert(cch == 4);
     
    425425                    case RTSF_FP32:
    426426                    {
    427                         fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
     427                        fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
    428428                        cch = RTStrFormatNumber(&szBuf[0], u.fp32.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
    429429                        Assert(cch == 4);
     
    436436                    case RTSF_FP64:
    437437                    {
    438                         fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION);
     438                        fFlags &= ~(RTSTR_F_VALSIGNED | RTSTR_F_BIT_MASK | RTSTR_F_WIDTH | RTSTR_F_PRECISION | RTSTR_F_THOUSAND_SEP);
    439439                        cch = RTStrFormatNumber(&szBuf[0], u.fp64.sel, 16, 4, -1, fFlags | RTSTR_F_16BIT);
    440440                        Assert(cch == 4);
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