VirtualBox

Ignore:
Timestamp:
Aug 3, 2016 4:11:43 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
109533
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/utf-16.cpp

    r62477 r62930  
    299299
    300300
    301 RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidSet, char chReplacement)
    302 {
    303     size_t cReplacements = 0;
     301/**
     302 * Helper for RTUtf16PurgeComplementSet.
     303 *
     304 * @returns true if @a Cp is valid, false if not.
     305 * @param   Cp              The code point to validate.
     306 * @param   puszValidPairs  Pair of valid code point sets.
     307 * @param   cValidPairs     Number of pairs.
     308 */
     309DECLINLINE(bool) rtUtf16PurgeIsInSet(RTUNICP Cp, PCRTUNICP puszValidPairs, uint32_t cValidPairs)
     310{
     311    while (cValidPairs-- > 0)
     312    {
     313        if (   Cp >= puszValidPairs[0]
     314            && Cp <= puszValidPairs[1])
     315            return true;
     316        puszValidPairs += 2;
     317    }
     318    return false;
     319}
     320
     321
     322RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidPairs, char chReplacement)
     323{
    304324    AssertReturn(chReplacement && (unsigned)chReplacement < 128, -1);
    305     /* Validate the encoding. */
     325
     326    /*
     327     * Calc valid pairs and check that we've got an even number.
     328     */
     329    uint32_t cValidPairs = 0;
     330    while (puszValidPairs[cValidPairs * 2])
     331    {
     332        AssertReturn(puszValidPairs[cValidPairs * 2 + 1], -1);
     333        AssertMsg(puszValidPairs[cValidPairs * 2] <= puszValidPairs[cValidPairs * 2 + 1],
     334                  ("%#x vs %#x\n", puszValidPairs[cValidPairs * 2], puszValidPairs[cValidPairs * 2 + 1]));
     335        cValidPairs++;
     336    }
     337
     338    /*
     339     * Do the replacing.
     340     */
     341    ssize_t cReplacements = 0;
    306342    for (;;)
    307343    {
     344        PRTUTF16 pwszCur = pwsz;
    308345        RTUNICP Cp;
    309         PCRTUNICP pCp;
    310         PRTUTF16 pwszOld = pwsz;
    311         if (RT_FAILURE(RTUtf16GetCpEx((PCRTUTF16 *)&pwsz, &Cp)))
     346        int rc = RTUtf16GetCpEx((PCRTUTF16 *)&pwsz, &Cp);
     347        if (RT_SUCCESS(rc))
     348        {
     349            if (Cp)
     350            {
     351                if (!rtUtf16PurgeIsInSet(Cp, puszValidPairs, cValidPairs))
     352                {
     353                    for (; pwszCur != pwsz; ++pwszCur)
     354                        *pwszCur = chReplacement;
     355                    ++cReplacements;
     356                }
     357            }
     358            else
     359                break;
     360        }
     361        else
    312362            return -1;
    313         if (!Cp)
    314             break;
    315         for (pCp = puszValidSet; *pCp; pCp += 2)
    316         {
    317             AssertReturn(*(pCp + 1), -1);
    318             if (*pCp <= Cp && *(pCp + 1) >= Cp) /* No, I won't do * and ++. */
    319                 break;
    320         }
    321         if (!*pCp)
    322         {
    323             for (; pwszOld != pwsz; ++pwszOld)
    324                 *pwszOld = chReplacement;
    325             ++cReplacements;
    326         }
    327363    }
    328364    return cReplacements;
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