VirtualBox

Changeset 62916 in vbox for trunk


Ignore:
Timestamp:
Aug 3, 2016 2:05:01 PM (8 years ago)
Author:
vboxsync
Message:

RTStrPurgeEncoding: Optimized it a little, adding debug assertion for bad pairs.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/vmm/pdmifs.h

    r62476 r62916  
    11971197     * @thread  Any thread.
    11981198     */
    1199     DECLR3CALLBACKMEMBER(int, pfnSetParameters,(PPDMICHARCONNECTOR pInterface, unsigned Bps, char chParity, unsigned cDataBits, unsigned cStopBits));
     1199    DECLR3CALLBACKMEMBER(int, pfnSetParameters,(PPDMICHARCONNECTOR pInterface, unsigned Bps, char chParity,
     1200                                                unsigned cDataBits, unsigned cStopBits));
    12001201
    12011202    /**
  • trunk/include/iprt/string.h

    r62473 r62916  
    726726
    727727/**
    728  * Sanitise a (valid) UTF-8 string by replacing all characters outside a white
     728 * Sanitizes a (valid) UTF-8 string by replacing all characters outside a white
    729729 * list in-place by an ASCII replacement character.  Multi-byte characters will
    730730 * be replaced byte by byte.
    731731 *
    732  * @returns The number of code points replaced, or a negative value if the
    733  *          string is not correctly encoded.  In this last case the string
    734  *          may be partially processed.
     732 * @returns The number of code points replaced.  In the case of an incorrectly
     733 *          encoded string -1 will be returned, and the string is not completely
     734 *          processed.  In the case of puszValidPairs having an odd number of
     735 *          code points, -1 will be also return but without any modification to
     736 *          the string.
    735737 * @param   psz            The string to sanitise.
    736  * @param   puszValidSet  A zero-terminated array of pairs of Unicode points.
     738 * @param   puszValidPairs A zero-terminated array of pairs of Unicode points.
    737739 *                         Each pair is the start and end point of a range,
    738740 *                         and the union of these ranges forms the white list.
    739741 * @param   chReplacement  The ASCII replacement character.
    740742 */
    741 RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidSet, char chReplacement);
     743RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidPairs, char chReplacement);
    742744
    743745/**
  • trunk/src/VBox/Runtime/common/string/utf-8.cpp

    r62477 r62916  
    364364
    365365
    366 RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidSet, char chReplacement)
    367 {
    368     size_t cReplacements = 0;
     366/**
     367 * Helper for RTStrPurgeComplementSet.
     368 *
     369 * @returns true if @a Cp is valid, false if not.
     370 * @param   Cp              The code point to validate.
     371 * @param   puszValidPairs  Pair of valid code point sets.
     372 * @param   cValidPairs     Number of pairs.
     373 */
     374DECLINLINE(bool) rtStrPurgeIsInSet(RTUNICP Cp, PCRTUNICP puszValidPairs, uint32_t cValidPairs)
     375{
     376    while (cValidPairs-- > 0)
     377    {
     378        if (   Cp >= puszValidPairs[0]
     379            && Cp <= puszValidPairs[1])
     380            return true;
     381        puszValidPairs += 2;
     382    }
     383    return false;
     384}
     385
     386
     387RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidPairs, char chReplacement)
     388{
    369389    AssertReturn(chReplacement && (unsigned)chReplacement < 128, -1);
     390
     391    /*
     392     * Calc valid pairs and check that we've got an even number.
     393     */
     394    uint32_t cValidPairs = 0;
     395    while (puszValidPairs[cValidPairs * 2])
     396    {
     397        AssertReturn(puszValidPairs[cValidPairs * 2 + 1], -1);
     398        AssertMsg(puszValidPairs[cValidPairs * 2] <= puszValidPairs[cValidPairs * 2 + 1],
     399                  ("%#x vs %#x\n", puszValidPairs[cValidPairs * 2], puszValidPairs[cValidPairs * 2 + 1]));
     400        cValidPairs++;
     401    }
     402
     403    /*
     404     * Do the replacing.
     405     */
     406    ssize_t cReplacements = 0;
    370407    for (;;)
    371408    {
    372         RTUNICP Cp;
    373         PCRTUNICP pCp;
    374         char *pszOld = psz;
    375         if (RT_FAILURE(RTStrGetCpEx((const char **)&psz, &Cp)))
     409        char    *pszCur = psz;
     410        RTUNICP  Cp;
     411        int rc = RTStrGetCpEx((const char **)&psz, &Cp);
     412        if (RT_SUCCESS(rc))
     413        {
     414            if (Cp)
     415            {
     416                if (!rtStrPurgeIsInSet(Cp, puszValidPairs, cValidPairs))
     417                {
     418                    for (; pszCur != psz; ++pszCur)
     419                        *pszCur = chReplacement;
     420                    ++cReplacements;
     421                }
     422            }
     423            else
     424                break;
     425        }
     426        else
    376427            return -1;
    377         if (!Cp)
    378             break;
    379         for (pCp = puszValidSet; *pCp; pCp += 2)
    380         {
    381             AssertReturn(*(pCp + 1), -1);
    382             if (*pCp <= Cp && *(pCp + 1) >= Cp) /* No, I won't do * and ++. */
    383                 break;
    384         }
    385         if (!*pCp)
    386         {
    387             for (; pszOld != psz; ++pszOld)
    388                 *pszOld = chReplacement;
    389             ++cReplacements;
    390         }
    391428    }
    392429    return cReplacements;
  • trunk/src/VBox/Runtime/testcase/tstUtf8.cpp

    r62722 r62916  
    874874        ssize_t cReplacements;
    875875        AssertRC(RTStrCopy(szCopy, RT_ELEMENTS(szCopy), aTests[i].pcszIn));
    876         cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet,
    877                                                 aTests[i].chReplacement);
     876        RTTestDisableAssertions(hTest);
     877        cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet, aTests[i].chReplacement);
     878        RTTestRestoreAssertions(hTest);
    878879        if (cReplacements != aTests[i].cExpected)
    879880            RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
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