VirtualBox

Changeset 80496 in vbox


Ignore:
Timestamp:
Aug 29, 2019 11:58:21 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
132989
Message:

IPRT/string.h: Added added two more human readable size formatting variants for skipping the 'i' in 'KiB' and such. Made the human readable size formatting use the space format flag to specify a space between the value and the unit.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/string.h

    r80358 r80496  
    14771477 *                            decimal point by default, can do 0-3 via precision
    14781478 *                            field.  No rounding when calculating fraction.
     1479 *                            The space flag add a space between the value and
     1480 *                            unit.
     1481 *      - \%RhcB            - Same a \%Rhcb only the 'i' is skipped in the unit.
    14791482 *      - \%Rhci            - SI variant of \%Rhcb, fraction is rounded.
    14801483 *      - \%Rhub            - Human readable number formatting, using
     
    14821485 *                            integer as input. Does one decimal point by
    14831486 *                            default, can do 0-3 via precision field.  No
    1484  *                            rounding when calculating fraction.
     1487 *                            rounding when calculating fraction.  The space
     1488 *                            flag add a space between the value and unit.
     1489 *      - \%RhuB            - Same a \%Rhub only the 'i' is skipped in the unit.
    14851490 *      - \%Rhui            - SI variant of \%Rhub, fraction is rounded.
    14861491 *
  • trunk/src/VBox/Runtime/common/string/strformatrt.cpp

    r76553 r80496  
    989989#endif /* IN_RING3 */
    990990
     991                    /*
     992                     * Human readable sizes.
     993                     */
    991994                    case 'c':
    992995                    case 'u':
     
    9971000                        uint64_t    uFraction = 0;
    9981001                        const char *pszPrefix = NULL;
    999                         unsigned    cchFixedPart;
    10001002                        char        ch2 = *(*ppszFormat)++;
    1001                         AssertMsgReturn(ch2 == 'b' || ch2 == 'i', ("invalid type '%.10s'!\n", pszFormatOrg), 0);
     1003                        AssertMsgReturn(ch2 == 'b' || ch2 == 'B' || ch2 == 'i', ("invalid type '%.10s'!\n", pszFormatOrg), 0);
    10021004                        uValue = va_arg(*pArgs, uint64_t);
    10031005
    10041006                        if (!(fFlags & RTSTR_F_PRECISION))
    1005                             cchPrecision = 1;
     1007                            cchPrecision = 1; /** @todo default to flexible decimal point. */
    10061008                        else if (cchPrecision > 3)
    10071009                            cchPrecision = 3;
     
    10091011                            cchPrecision = 0;
    10101012
    1011                         cchFixedPart = cchPrecision + (cchPrecision != 0) + (ch == 'c');
    1012 
    1013                         if (ch2 == 'b')
     1013                        if (ch2 == 'b' || ch2 == 'B')
    10141014                        {
    10151015                            static const struct
     
    10401040                                    uValue  >>= s_aUnits[i].cShift;
    10411041                                    pszPrefix = s_aUnits[i].pszPrefix;
    1042                                     cchFixedPart += 2;
    10431042                                    break;
    10441043                                }
     
    10751074                                    }
    10761075                                    pszPrefix = s_aUnits[i].pszPrefix;
    1077                                     cchFixedPart += 1;
    10781076                                    break;
    10791077                                }
     
    10891087                                                         RTSTR_F_ZEROPAD | RTSTR_F_WIDTH);
    10901088                            }
     1089                            if (fFlags & RTSTR_F_BLANK)
     1090                                szBuf[cchBuf++] = ' ';
    10911091                            szBuf[cchBuf++] = *pszPrefix++;
    1092                             if (*pszPrefix)
     1092                            if (*pszPrefix && ch2 != 'B')
    10931093                                szBuf[cchBuf++] = *pszPrefix;
    10941094                        }
     1095                        else if (fFlags & RTSTR_F_BLANK)
     1096                            szBuf[cchBuf++] = ' ';
    10951097                        if (ch == 'c')
    10961098                            szBuf[cchBuf++] = 'B';
  • trunk/src/VBox/Runtime/testcase/tstRTStrFormat.cpp

    r76553 r80496  
    775775    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6678345), 42);
    776776    CHECKSTR("    6.3MiB42");
     777    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710886), 42);
     778    CHECKSTR("    6.3MiB42");
     779    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhcb%u", UINT64_C(6710887), 42);
     780    CHECKSTR("    6.4MiB42");
     781    cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10Rhcb%u", UINT64_C(6710887), 42);
     782    CHECKSTR("   6.4 MiB42");
     783    cch = RTStrPrintf(pszBuf, BUF_SIZE, "% 10RhcB%u", UINT64_C(6710887), 42);
     784    CHECKSTR("    6.4 MB42");
    777785
    778786    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhub%u", UINT64_C(6678345), 42);
    779787    CHECKSTR("     6.3Mi42");
     788    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10RhuB%u", UINT64_C(6678345), 42);
     789    CHECKSTR("      6.3M42");
    780790
    781791    cch = RTStrPrintf(pszBuf, BUF_SIZE, "%10Rhci%u", UINT64_C(6678345), 42);
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