VirtualBox

Changeset 19942 in vbox for trunk/src


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
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/xml/Settings.cpp

    r16560 r19942  
    246246
    247247    stdx::char_auto_ptr result (new char [len]);
     248    if (aBase == 0)
     249        aBase = 10;
    248250    int vrc = RTStrFormatNumber (result.get(), aValue, aBase, 0, 0, flags);
    249251    if (RT_SUCCESS (vrc))
  • 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);
  • trunk/src/VBox/Runtime/testcase/tstStrFormat.cpp

    r14831 r19942  
    3333*******************************************************************************/
    3434#include <iprt/string.h>
     35
    3536#include <iprt/initterm.h>
     37#include <iprt/stream.h>
     38#include <iprt/test.h>
    3639#include <iprt/uuid.h>
    37 #include <iprt/string.h>
    38 #include <iprt/stream.h>
    39 
    40 /*******************************************************************************
    41 *   Global Variables                                                           *
    42 *******************************************************************************/
    43 static int g_cErrors = 0;
    4440
    4541
     
    5248    /* validate */
    5349    if (strncmp(pszType, "type", 4))
    54     {
    55         RTPrintf("tstStrFormat: pszType=%s expected 'typeN'\n", pszType);
    56         g_cErrors++;
    57     }
     50        RTTestIFailed("pszType=%s expected 'typeN'\n", pszType);
    5851
    5952    int iType = pszType[4] - '0';
    6053    if ((uintptr_t)pvUser != (uintptr_t)TstType + iType)
    61     {
    62         RTPrintf("tstStrFormat: pvValue=%p expected %p\n", pvUser, (void *)((uintptr_t)TstType + iType));
    63         g_cErrors++;
    64     }
     54        RTTestIFailed("pvValue=%p expected %p\n", pvUser, (void *)((uintptr_t)TstType + iType));
    6555
    6656    /* format */
     
    6858    cch += pfnOutput(pvArgOutput, "=", 1);
    6959    char szNum[64];
    70     size_t cchNum = RTStrFormatNumber(szNum, (uintptr_t)pvValue, 0, cchWidth, cchPrecision, fFlags);
     60    size_t cchNum = RTStrFormatNumber(szNum, (uintptr_t)pvValue, 10, cchWidth, cchPrecision, fFlags);
    7161    cch += pfnOutput(pvArgOutput, szNum, cchNum);
    7262    return cch;
     
    7666int main()
    7767{
    78     RTR3Init();
     68    int rc = RTR3Init();
     69    if (RT_FAILURE(rc))
     70        return 1;
     71    RTTEST      hTest;
     72    rc = RTTestCreate("tstStrFormat", &hTest);
     73    if (RT_FAILURE(rc))
     74        return 1;
     75    RTTestBanner(hTest);
    7976
    8077    uint32_t    u32 = 0x010;
    8178    uint64_t    u64 = 0x100;
    82     char        szStr[120];
     79#define BUF_SIZE    120
     80    char       *pszBuf = (char *)RTTestGuardedAllocHead(hTest, BUF_SIZE);
     81
     82    RTTestSub(hTest, "Basics");
    8383
    8484    /* simple */
    85     size_t cch = RTStrPrintf(szStr, sizeof(szStr), "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
    86     if (strcmp(szStr, "u32=16 u64=256 u64=0x100"))
    87     {
    88         RTPrintf("error: '%s'\n"
    89                "wanted 'u32=16 u64=256 u64=0x100'\n", szStr);
    90         g_cErrors++;
     85    size_t cch = RTStrPrintf(pszBuf, BUF_SIZE, "u32=%d u64=%lld u64=%#llx", u32, u64, u64);
     86    if (strcmp(pszBuf, "u32=16 u64=256 u64=0x100"))
     87    {
     88        RTTestIFailed("error: '%s'\n"
     89                      "wanted 'u32=16 u64=256 u64=0x100'\n", pszBuf);
    9190    }
    9291
    9392    /* just big. */
    9493    u64 = UINT64_C(0x7070605040302010);
    95     cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
    96     if (strcmp(szStr, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
    97     {
    98         RTPrintf("error: '%s'\n"
    99                  "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", szStr);
    100         RTPrintf("%d\n", (int)(u64 % 10));
    101         g_cErrors++;
     94    cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
     95    if (strcmp(pszBuf, "u64=0x7070605040302010 42=42 u64=8102081627430068240 42=42"))
     96    {
     97        RTTestIFailed("error: '%s'\n"
     98                      "wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf);
     99        RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
    102100    }
    103101
    104102    /* huge and negative. */
    105103    u64 = UINT64_C(0x8070605040302010);
    106     cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
     104    cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
    107105    /* Not sure if this is the correct decimal representation... But both */
    108     if (strcmp(szStr, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
    109     {
    110         RTPrintf("error: '%s'\n"
    111                  "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", szStr);
    112         RTPrintf("%d\n", (int)(u64 % 10));
    113         g_cErrors++;
     106    if (strcmp(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
     107    {
     108        RTTestIFailed("error: '%s'\n"
     109                      "wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf);
     110        RTTestIPrintf(RTTESTLVL_FAILURE, "%d\n", (int)(u64 % 10));
    114111    }
    115112
    116113    /* 64-bit value bug. */
    117114    u64 = 0xa0000000;
    118     cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
    119     if (strcmp(szStr, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
    120     {
    121         RTPrintf("error: '%s'\n"
    122                  "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", szStr);
    123         g_cErrors++;
    124     }
     115    cch = RTStrPrintf(pszBuf, BUF_SIZE, "u64=%#llx 42=%d u64=%lld 42=%d", u64, 42, u64, 42);
     116    if (strcmp(pszBuf, "u64=0xa0000000 42=42 u64=2684354560 42=42"))
     117        RTTestIFailed("error: '%s'\n"
     118                      "wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf);
    125119
    126120    /* uuid */
     
    129123    char szCorrect[RTUUID_STR_LENGTH];
    130124    RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
    131     cch = RTStrPrintf(szStr, sizeof(szStr), "%Vuuid", &Uuid);
    132     if (strcmp(szStr, szCorrect))
    133     {
    134         RTPrintf("error:    '%s'\n"
    135                  "expected: '%s'\n",
    136                  szStr, szCorrect);
    137         g_cErrors++;
    138     }
    139 
    140     /* allocation */
     125    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Vuuid", &Uuid);
     126    if (strcmp(pszBuf, szCorrect))
     127        RTTestIFailed("error:    '%s'\n"
     128                      "expected: '%s'\n",
     129                      pszBuf, szCorrect);
     130
     131    /*
     132     * allocation
     133     */
     134    RTTestSub(hTest, "RTStrAPrintf");
    141135    char *psz = (char *)~0;
    142136    int cch2 = RTStrAPrintf(&psz, "Hey there! %s%s", "This is a test", "!");
    143137    if (cch2 < 0)
    144     {
    145         RTPrintf("error: RTStrAPrintf failed, cch2=%d\n", cch2);
    146         g_cErrors++;
    147     }
     138        RTTestIFailed("RTStrAPrintf failed, cch2=%d\n", cch2);
    148139    else if (strcmp(psz, "Hey there! This is a test!"))
    149     {
    150         RTPrintf("error: RTStrAPrintf failed\n"
    151                  "got   : '%s'\n"
    152                  "wanted: 'Hey there! This is a test!'\n",
    153                psz);
    154         g_cErrors++;
    155     }
     140        RTTestIFailed("RTStrAPrintf failed\n"
     141                      "got   : '%s'\n"
     142                      "wanted: 'Hey there! This is a test!'\n",
     143                      psz);
    156144    else if ((int)strlen(psz) != cch2)
    157     {
    158         RTPrintf("error: RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz));
    159         g_cErrors++;
    160     }
     145        RTTestIFailed("RTStrAPrintf failed, cch2 == %d expected %u\n", cch2, strlen(psz));
    161146    RTStrFree(psz);
    162147
    163148#define CHECK42(fmt, arg, out) \
    164149    do { \
    165         cch = RTStrPrintf(szStr, sizeof(szStr), fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \
    166         if (strcmp(szStr, out " 42=42 " out " 42=42")) \
    167         { \
    168             RTPrintf("error(%d): format '%s'\n" \
    169                      "    output: '%s'\n"  \
    170                      "    wanted: '%s'\n", \
    171                      __LINE__, fmt, szStr, out " 42=42 " out " 42=42"); \
    172             g_cErrors++; \
    173         } \
     150        cch = RTStrPrintf(pszBuf, BUF_SIZE, fmt " 42=%d " fmt " 42=%d", arg, 42, arg, 42); \
     151        if (strcmp(pszBuf, out " 42=42 " out " 42=42")) \
     152            RTTestIFailed("at line %d: format '%s'\n" \
     153                          "    output: '%s'\n"  \
     154                          "    wanted: '%s'\n", \
     155                          __LINE__, fmt, pszBuf, out " 42=42 " out " 42=42"); \
    174156        else if (cch != sizeof(out " 42=42 " out " 42=42") - 1) \
    175         { \
    176             RTPrintf("error(%d): Invalid length %d returned, expected %u!\n", \
    177                      __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \
    178             g_cErrors++; \
    179         } \
     157            RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \
     158                          __LINE__, cch, sizeof(out " 42=42 " out " 42=42") - 1); \
    180159    } while (0)
     160
     161#define CHECKSTR(Correct) \
     162    if (strcmp(pszBuf, Correct)) \
     163        RTTestIFailed("error:    '%s'\n" \
     164                      "expected: '%s'\n", pszBuf, Correct); \
    181165
    182166    /*
    183167     * Runtime extensions.
    184168     */
     169    RTTestSub(hTest, "Runtime format types (%R*)");
    185170    CHECK42("%RGi", (RTGCINT)127, "127");
    186171    CHECK42("%RGi", (RTGCINT)-586589, "-586589");
     
    357342    RTUuidCreate(&Uuid);
    358343    RTUuidToStr(&Uuid, szCorrect, sizeof(szCorrect));
    359     cch = RTStrPrintf(szStr, sizeof(szStr), "%RTuuid", &Uuid);
    360     if (strcmp(szStr, szCorrect))
    361     {
    362         RTPrintf("error:    '%s'\n"
    363                  "expected: '%s'\n",
    364                  szStr, szCorrect);
    365         g_cErrors++;
    366     }
     344    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%RTuuid", &Uuid);
     345    if (strcmp(pszBuf, szCorrect))
     346        RTTestIFailed("error:    '%s'\n"
     347                      "expected: '%s'\n",
     348                      pszBuf, szCorrect);
    367349
    368350    CHECK42("%RTxint", (RTGCUINT)0x2345, "2345");
     
    395377    CHECK42("%RX8", 0x100, "0");
    396378
    397 #define CHECKSTR(Correct) \
    398     if (strcmp(szStr, Correct)) \
    399     { \
    400         RTPrintf("error:    '%s'\n" \
    401                  "expected: '%s'\n", szStr, Correct); \
    402         g_cErrors++; \
    403     }
     379    /*
     380     * Thousand separators.
     381     */
     382    RTTestSub(hTest, "Thousand Separators (%'*)");
     383
     384    RTStrFormatNumber(pszBuf,       1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1");              memset(pszBuf, '!', BUF_SIZE);
     385    RTStrFormatNumber(pszBuf,      10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10");             memset(pszBuf, '!', BUF_SIZE);
     386    RTStrFormatNumber(pszBuf,     100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100");            memset(pszBuf, '!', BUF_SIZE);
     387    RTStrFormatNumber(pszBuf,    1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000");          memset(pszBuf, '!', BUF_SIZE);
     388    RTStrFormatNumber(pszBuf,   10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000");         memset(pszBuf, '!', BUF_SIZE);
     389    RTStrFormatNumber(pszBuf,  100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000");        memset(pszBuf, '!', BUF_SIZE);
     390    RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000");      memset(pszBuf, '!', BUF_SIZE);
     391   
     392    CHECK42("%'u", 1,                              "1");
     393    CHECK42("%'u", 10,                            "10");
     394    CHECK42("%'u", 100,                          "100");
     395    CHECK42("%'u", 1000,                       "1 000");
     396    CHECK42("%'u", 10000,                     "10 000");
     397    CHECK42("%'u", 100000,                   "100 000");
     398    CHECK42("%'u", 1000000,                "1 000 000");
     399    CHECK42("%'RU64", _1T,         "1 099 511 627 776");
     400    CHECK42("%'RU64", _1E, "1 152 921 504 606 846 976");
    404401
    405402    /*
    406403     * String formatting.
    407404     */
     405    RTTestSub(hTest, "String formatting (%s)");
     406
    408407//            0         1         2         3         4         5         6         7
    409408//            0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
    410     cch = RTStrPrintf(szStr, sizeof(szStr), "%-10s %-30s %s", "cmd", "args", "description");
     409    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "args", "description");
    411410    CHECKSTR("cmd        args                           description");
    412411
    413     cch = RTStrPrintf(szStr, sizeof(szStr), "%-10s %-30s %s", "cmd", "", "description");
     412    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10s %-30s %s", "cmd", "", "description");
    414413    CHECKSTR("cmd                                       description");
    415414
    416415
    417     cch = RTStrPrintf(szStr, sizeof(szStr),  "%*s", 0, "");
     416    cch = RTStrPrintf(pszBuf, BUF_SIZE,  "%*s", 0, "");
    418417    CHECKSTR("");
    419418
     
    422421    static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
    423422
    424     cch = RTStrPrintf(szStr, sizeof(szStr), "%ls", s_wsz1);
     423    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz1);
    425424    CHECKSTR("hello world");
    426     cch = RTStrPrintf(szStr, sizeof(szStr), "%Ls", s_usz1);
     425    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz1);
    427426    CHECKSTR("hello world");
    428427
    429     cch = RTStrPrintf(szStr, sizeof(szStr), "%.5ls", s_wsz1);
     428    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5ls", s_wsz1);
    430429    CHECKSTR("hello");
    431     cch = RTStrPrintf(szStr, sizeof(szStr), "%.5Ls", s_usz1);
     430    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%.5Ls", s_usz1);
    432431    CHECKSTR("hello");
    433432
     
    435434     * Unicode string formatting.
    436435     */
     436    RTTestSub(hTest, "Unicode string formatting (%ls)");
    437437    static RTUTF16 s_wszEmpty[]  = { 0 }; //assumes ascii.
    438438    static RTUTF16 s_wszCmd[]    = { 'c', 'm', 'd', 0 }; //assumes ascii.
     
    442442//            0         1         2         3         4         5         6         7
    443443//            0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
    444     cch = RTStrPrintf(szStr, sizeof(szStr), "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc);
     444    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszArgs, s_wszDesc);
    445445    CHECKSTR("cmd        args                           description");
    446446
    447     cch = RTStrPrintf(szStr, sizeof(szStr), "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc);
     447    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%-10ls %-30ls %ls", s_wszCmd, s_wszEmpty, s_wszDesc);
    448448    CHECKSTR("cmd                                       description");
    449449
     
    454454    static char    s_sz2[]  = { 0xc5, 0xc6, 0xf8, 0 };///@todo multibyte tests.
    455455
    456     cch = RTStrPrintf(szStr, sizeof(szStr), "%ls", s_wsz2);
     456    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%ls", s_wsz2);
    457457    CHECKSTR(s_sz2);
    458     cch = RTStrPrintf(szStr, sizeof(szStr), "%Ls", s_usz2);
     458    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%Ls", s_usz2);
    459459    CHECKSTR(s_sz2);
    460460#endif
     
    463463     * Custom types.
    464464     */
    465 #define CHECK(expr)  do { if (!(expr)) { RTPrintf("tstEnv: error line %d: %s\n", __LINE__, #expr); g_cErrors++; } } while (0)
    466 #define CHECK_RC(expr, rc)  do { int rc2 = expr; if (rc2 != (rc)) { RTPrintf("tstEnv: error line %d: %s -> %Rrc expected %Rrc\n", __LINE__, #expr, rc2, rc); g_cErrors++; } } while (0)
    467 
    468     CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
    469     CHECK_RC(RTStrFormatTypeSetUser("type3",           (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
    470     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3]", (void *)1);
     465    RTTestSub(hTest, "Custom format types (%R[*])");
     466    RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
     467    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3",           (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
     468    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)1);
    471469    CHECKSTR("type3=1");
    472470
    473     CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
    474     CHECK_RC(RTStrFormatTypeSetUser("type1",           (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
    475     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1]", (void *)1, (void *)2);
     471    RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
     472    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1",           (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
     473    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)1, (void *)2);
    476474    CHECKSTR("type3=1 type1=2");
    477475
    478     CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
    479     CHECK_RC(RTStrFormatTypeSetUser("type4",           (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
    480     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
     476    RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
     477    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4",           (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
     478    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
    481479    CHECKSTR("type3=1 type1=2 type4=3");
    482480
    483     CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
    484     CHECK_RC(RTStrFormatTypeSetUser("type2",           (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
    485     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
     481    RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
     482    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2",           (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
     483    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
    486484    CHECKSTR("type3=1 type1=2 type4=3 type2=4");
    487485
    488     CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
    489     CHECK_RC(RTStrFormatTypeSetUser("type5",           (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
    490     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5);
     486    RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
     487    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5",           (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
     488    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5);
    491489    CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5");
    492490
    493     CHECK_RC(RTStrFormatTypeSetUser("type1",           (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
    494     CHECK_RC(RTStrFormatTypeSetUser("type2",           (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
    495     CHECK_RC(RTStrFormatTypeSetUser("type3",           (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
    496     CHECK_RC(RTStrFormatTypeSetUser("type4",           (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
    497     CHECK_RC(RTStrFormatTypeSetUser("type5",           (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
    498 
    499     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50);
     491    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type1",           (void *)((uintptr_t)TstType + 1)), VINF_SUCCESS);
     492    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type2",           (void *)((uintptr_t)TstType + 2)), VINF_SUCCESS);
     493    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type3",           (void *)((uintptr_t)TstType + 3)), VINF_SUCCESS);
     494    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type4",           (void *)((uintptr_t)TstType + 4)), VINF_SUCCESS);
     495    RTTESTI_CHECK_RC(RTStrFormatTypeSetUser("type5",           (void *)((uintptr_t)TstType + 5)), VINF_SUCCESS);
     496
     497    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50);
    500498    CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50");
    501499
    502     CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS);
    503     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
     500    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type2"), VINF_SUCCESS);
     501    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
    504502    CHECKSTR("type3=10 type1=20 type4=30 type5=40");
    505503
    506     CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS);
    507     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
     504    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type5"), VINF_SUCCESS);
     505    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
    508506    CHECKSTR("type3=10 type1=20 type4=30");
    509507
    510     CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS);
    511     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1]", (void *)10, (void *)20);
     508    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type4"), VINF_SUCCESS);
     509    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1]", (void *)10, (void *)20);
    512510    CHECKSTR("type3=10 type1=20");
    513511
    514     CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS);
    515     cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3]", (void *)10);
     512    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type1"), VINF_SUCCESS);
     513    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3]", (void *)10);
    516514    CHECKSTR("type3=10");
    517515
    518     CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
     516    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
    519517
    520518    /*
    521519     * Summarize and exit.
    522520     */
    523     if (!g_cErrors)
    524         RTPrintf("tstStrFormat: SUCCESS\n");
    525     else
    526         RTPrintf("tstStrFormat: FAILED - %d errors\n", g_cErrors);
    527     return !!g_cErrors;
     521    return RTTestSummaryAndDestroy(hTest);
    528522}
    529523
Note: See TracChangeset for help on using the changeset viewer.

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