VirtualBox

Changeset 33610 in vbox for trunk/include


Ignore:
Timestamp:
Oct 29, 2010 2:42:36 PM (14 years ago)
Author:
vboxsync
Message:

iprt::MiniString: Fixed bug in compare("") and equals("") caused by m_psz=NULL when m_cch=0 and RTStr[I]Cmp regarding NULL and "" as different things. Added format+va_list constructor.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/ministring.h

    r33605 r33610  
    100100
    101101    /**
     102     * Create a new string given the format string and its arguments.
     103     *
     104     * @param   a_pszFormat     Pointer to the format string (UTF-8),
     105     *                          @see pg_rt_str_format.
     106     * @param   a_va            Argument vector containing the arguments
     107     *                          specified by the format string.
     108     * @sa      printfV
     109     */
     110    MiniString(const char *a_pszFormat, va_list a_va)
     111        : m_psz(NULL),
     112          m_cch(0),
     113          m_cbAllocated(0)
     114
     115    {
     116        printfV(a_pszFormat, a_va);
     117    }
     118
     119    /**
    102120     * Destructor.
    103121     */
     
    214232     * Assigns the output of the string format operation (RTStrPrintf).
    215233     *
    216      * @param   pszFormat       The format string.
     234     * @param   pszFormat       Pointer to the format string,
     235     *                          @see pg_rt_str_format.
    217236     * @param   ...             Ellipsis containing the arguments specified by
    218237     *                          the format string.
     
    227246     * Assigns the output of the string format operation (RTStrPrintfV).
    228247     *
    229      * @param   pszFormat       The format string.
     248     * @param   pszFormat       Pointer to the format string,
     249     *                          @see pg_rt_str_format.
    230250     * @param   va              Argument vector containing the arguments
    231251     *                          specified by the format string.
     
    461481
    462482    /**
    463      * Compares the member string to pcsz.
    464      * @param pcsz
    465      * @param cs Whether comparison should be case-sensitive.
    466      * @return
    467      */
    468     int compare(const char *pcsz, CaseSensitivity cs = CaseSensitive) const
    469     {
    470         if (m_psz == pcsz)
    471             return 0;
    472         if (m_psz == NULL)
    473             return -1;
    474         if (pcsz == NULL)
    475             return 1;
     483     * Compares the member string to a C-string.
     484     *
     485     * @param   pcszThat    The string to compare with.
     486     * @param   cs          Whether comparison should be case-sensitive.
     487     * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
     488     *          if larger.
     489     */
     490    int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
     491    {
     492        /* This klugde is for m_cch=0 and m_psz=NULL.  pcsz=NULL and psz=""
     493           are treated the same way so that str.compare(str2.c_str()) works. */
     494        if (length() == 0)
     495            return pcszThat == NULL || *pcszThat != '\0';
    476496
    477497        if (cs == CaseSensitive)
    478             return ::RTStrCmp(m_psz, pcsz);
    479         else
    480             return ::RTStrICmp(m_psz, pcsz);
    481     }
    482 
     498            return ::RTStrCmp(m_psz, pcszThat);
     499        return ::RTStrICmp(m_psz, pcszThat);
     500    }
     501
     502    /**
     503     * Compares the member string to another MiniString.
     504     *
     505     * @param   pcszThat    The string to compare with.
     506     * @param   cs          Whether comparison should be case-sensitive.
     507     * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
     508     *          if larger.
     509     */
    483510    int compare(const MiniString &that, CaseSensitivity cs = CaseSensitive) const
    484511    {
    485         return compare(that.m_psz, cs);
     512        if (cs == CaseSensitive)
     513            return ::RTStrCmp(m_psz, that.m_psz);
     514        return ::RTStrICmp(m_psz, that.m_psz);
    486515    }
    487516
     
    506535    bool equals(const char *pszThat) const
    507536    {
     537        /* This klugde is for m_cch=0 and m_psz=NULL.  pcsz=NULL and psz=""
     538           are treated the same way so that str.equals(str2.c_str()) works. */
     539        if (length() == 0)
     540            return pszThat == NULL || *pszThat == '\0';
    508541        return RTStrCmp(pszThat, m_psz) == 0;
    509542    }
     
    530563    bool equalsIgnoreCase(const char *pszThat) const
    531564    {
     565        /* This klugde is for m_cch=0 and m_psz=NULL.  pcsz=NULL and psz=""
     566           are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
     567        if (length() == 0)
     568            return pszThat == NULL || *pszThat == '\0';
    532569        return RTStrICmp(pszThat, m_psz) == 0;
    533570    }
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