VirtualBox

Changeset 60409 in vbox


Ignore:
Timestamp:
Apr 10, 2016 3:40:07 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
106495
Message:

com::Bstr: Added compareUtf8() for optimal comparsions with UTF-8 strings. Also added a few equals() and equalsIgnoreCase() method for convenience.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/string.h

    r58110 r60409  
    230230
    231231    /**
     232     * Compares this string to an UTF-8 C style string.
     233     *
     234     * @retval  0 if equal
     235     * @retval -1 if this string is smaller than the UTF-8 one.
     236     * @retval  1 if the UTF-8 string is smaller than this.
     237     *
     238     * @param   a_pszRight  The string to compare with.
     239     * @param   a_enmCase   Whether comparison should be case-sensitive.
     240     */
     241    int compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase = CaseSensitive) const;
     242
     243    /** Java style compare method.
     244     * @returns true if @a a_pszRight equals this string.
     245     * @param   a_pszRight The (UTF-8) string to compare with. */
     246    bool equals(const char *a_pszRight) const           { return compareUtf8(a_pszRight, CaseSensitive) == 0; }
     247
     248    /** Java style case-insensitive compare method.
     249     * @returns true if @a a_pszRight equals this string.
     250     * @param   a_pszRight The (UTF-8) string to compare with. */
     251    bool equalsIgnoreCase(const char *a_pszRight) const { return compareUtf8(a_pszRight, CaseInsensitive) == 0; }
     252
     253    /** Java style compare method.
     254     * @returns true if @a a_rThat equals this string.
     255     * @param   a_rThat     The other Bstr instance to compare with. */
     256    bool equals(const Bstr &a_rThat) const              { return compare(a_rThat.m_bstr, CaseSensitive) == 0; }
     257    /** Java style case-insensitive compare method.
     258     * @returns true if @a a_rThat equals this string.
     259     * @param   a_rThat     The other Bstr instance to compare with. */
     260    bool equalsIgnoreCase(const Bstr &a_rThat) const    { return compare(a_rThat.m_bstr, CaseInsensitive) == 0; }
     261
     262    /** Java style compare method.
     263     * @returns true if @a a_pThat equals this string.
     264     * @param   a_pThat    The native const BSTR to compare with. */
     265    bool equals(CBSTR a_pThat) const                    { return compare(a_pThat, CaseSensitive) == 0; }
     266    /** Java style case-insensitive compare method.
     267     * @returns true if @a a_pThat equals this string.
     268     * @param   a_pThat    The native const BSTR to compare with. */
     269    bool equalsIgnoreCase(CBSTR a_pThat) const          { return compare(a_pThat, CaseInsensitive) == 0; }
     270
     271    /** Java style compare method.
     272     * @returns true if @a a_pThat equals this string.
     273     * @param   a_pThat    The native BSTR to compare with. */
     274    bool equals(BSTR a_pThat) const                     { return compare(a_pThat, CaseSensitive) == 0; }
     275    /** Java style case-insensitive compare method.
     276     * @returns true if @a a_pThat equals this string.
     277     * @param   a_pThat    The native BSTR to compare with. */
     278    bool equalsIgnoreCase(BSTR a_pThat) const           { return compare(a_pThat, CaseInsensitive) == 0; }
     279
     280    /**
    232281     * Returns true if the member string has no length.
    233282     * This is true for instances created from both NULL and "" input strings.
  • trunk/src/VBox/Main/glue/string.cpp

    r60254 r60409  
    2121#include <iprt/path.h>
    2222#include <iprt/log.h>
     23#include <iprt/string.h>
     24#include <iprt/uni.h>
    2325
    2426namespace com
     
    7173}
    7274
     75int Bstr::compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase /*= CaseSensitive*/) const
     76{
     77    PCRTUTF16 pwszLeft = m_bstr;
     78
     79    /*
     80     * Special case for null/empty strings.  Unlike RTUtf16Cmp we
     81     * treat null and empty equally.
     82     */
     83    if (!pwszLeft)
     84        return !a_pszRight || *a_pszRight == '\0' ? 0 : -1;
     85    if (!a_pszRight)
     86        return *pwszLeft == '\0'                  ? 0 :  1;
     87
     88    /*
     89     * Compare with a UTF-8 string by enumerating them char by char.
     90     */
     91    for (;;)
     92    {
     93        RTUNICP ucLeft;
     94        int rc = RTUtf16GetCpEx(&pwszLeft, &ucLeft);
     95        AssertRCReturn(rc, 1);
     96
     97        RTUNICP ucRight;
     98        rc = RTStrGetCpEx(&a_pszRight, &ucRight);
     99        AssertRCReturn(rc, -1);
     100        if (ucLeft == ucRight)
     101        {
     102            if (ucLeft)
     103                continue;
     104            return 0;
     105        }
     106
     107        if (a_enmCase == CaseInsensitive)
     108        {
     109            if (RTUniCpToUpper(ucLeft) == RTUniCpToUpper(ucRight))
     110                continue;
     111            if (RTUniCpToLower(ucLeft) == RTUniCpToLower(ucRight))
     112                continue;
     113        }
     114
     115        return ucLeft < ucRight ? -1 : 1;
     116    }
     117}
     118
    73119
    74120/* static */
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