VirtualBox

Changeset 33862 in vbox


Ignore:
Timestamp:
Nov 8, 2010 5:07:49 PM (14 years ago)
Author:
vboxsync
Message:

iprt/cpp/ministring.h: Changed the substring constructors to match std::string. As I feared, this caused some minor issues with the format+va_list constructor, fortunately it's only when passing a_cchSrc=0. While at it, I've added the one missing std::string constructor, the repeat character constructor.

Location:
trunk
Files:
3 edited

Legend:

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

    r33818 r33862  
    102102     * Create a partial copy of another MiniString.
    103103     *
     104     * @param   a_rSrc          The source string.
     105     * @param   a_offSrc        The byte offset into the source string.
    104106     * @param   a_cchSrc        The max number of chars (encoded UTF-8 bytes)
    105107     *                          to copy from the source string.
    106      * @param   a_rSrc          The source string.
    107      */
    108     MiniString(size_t a_cchSrc, const MiniString &a_rSrc)
    109     {
    110         Assert(a_cchSrc <= a_rSrc.m_cch);
    111         copyFromN(a_rSrc.m_psz, RT_MIN(a_cchSrc, a_rSrc.m_cch));
     108     */
     109    MiniString(const MiniString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
     110    {
     111        if (a_offSrc < a_rSrc.m_cch)
     112            copyFromN(&a_rSrc.m_psz[a_offSrc], RT_MIN(a_cchSrc, a_rSrc.m_cch - a_offSrc));
     113        else
     114        {
     115            m_psz = NULL;
     116            m_cch = 0;
     117            m_cbAllocated = 0;
     118        }
    112119    }
    113120
     
    115122     * Create a partial copy of a C string.
    116123     *
     124     * @param   a_pszSrc        The source string (UTF-8).
    117125     * @param   a_cchSrc        The max number of chars (encoded UTF-8 bytes)
    118      *                          to copy from the source string.
    119      * @param   a_pszSrc        The source string (UTF-8).
    120      */
    121     MiniString(size_t a_cchSrc, const char *a_pszSrc)
     126     *                          to copy from the source string.  This must not
     127     *                          be '0' as the compiler could easily mistake
     128     *                          that for the va_list constructor.
     129     */
     130    MiniString(const char *a_pszSrc, size_t a_cchSrc)
    122131    {
    123132        size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0;
    124         Assert(a_cchSrc <= cchMax);
    125133        copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax));
     134    }
     135
     136    /**
     137     * Create a string containing @a a_cTimes repetitions of the character @a
     138     * a_ch.
     139     *
     140     * @param   a_cTimes        The number of times the character is repeated.
     141     * @param   a_ch            The character to fill the string with.
     142     */
     143    MiniString(size_t a_cTimes, char a_ch)
     144        : m_psz(NULL),
     145          m_cch(0),
     146          m_cbAllocated(0)
     147    {
     148        Assert((unsigned)a_ch < 0x80);
     149        if (a_cTimes)
     150        {
     151            reserve(a_cTimes + 1);
     152            memset(m_psz, a_ch, a_cTimes);
     153            m_psz[a_cTimes] = '\0';
     154            m_cch = a_cTimes;
     155        }
    126156    }
    127157
     
    134164     *                          specified by the format string.
    135165     * @sa      printfV
     166     * @remarks Not part of std::string.
    136167     */
    137168    MiniString(const char *a_pszFormat, va_list a_va)
     
    139170          m_cch(0),
    140171          m_cbAllocated(0)
    141 
    142172    {
    143173        printfV(a_pszFormat, a_va);
     
    783813                m_cch = cchSrc;
    784814                m_cbAllocated = cchSrc + 1;
    785                 memcpy(m_psz, pcszSrc, cchSrc + 1);
     815                memcpy(m_psz, pcszSrc, cchSrc);
     816                m_psz[cchSrc] = '\0';
    786817            }
    787818            else
  • trunk/src/VBox/Main/ExtPackUtil.cpp

    r33806 r33862  
    180180     * Make a duplicate of the name and return it.
    181181     */
    182     iprt::MiniString *pStrRet = new iprt::MiniString(off, pszSrc);
     182    iprt::MiniString *pStrRet = new iprt::MiniString(pszSrc, off);
    183183    Assert(VBoxExtPackIsValidName(pStrRet->c_str()));
    184184    return pStrRet;
  • trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp

    r33819 r33862  
    3939static void test1Hlp1(const char *pszExpect, const char *pszFormat, ...)
    4040{
     41#if 0
    4142    va_list va;
    4243    va_start(va, pszFormat);
     
    4445    va_end(va);
    4546    RTTESTI_CHECK_MSG(strTst.equals(pszExpect),  ("strTst='%s' expected='%s'\n",  strTst.c_str(), pszExpect));
     47#endif
    4648}
    4749
     
    6163        if (!(expr)) \
    6264            RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
     65    } while (0)
     66#define CHECK_EQUAL(Str, szExpect) \
     67    do { \
     68        if (!(Str).equals(szExpect)) \
     69            RTTestIFailed("line %u: expected \"%s\" got \"%s\"", __LINE__, szExpect, (Str).c_str()); \
    6370    } while (0)
    6471
     
    167174    test1Hlp1("foobar", "%s", "foobar");
    168175
     176    /* substring constructors */
     177    iprt::MiniString SubStr1("", (size_t)0);
     178    CHECK_EQUAL(SubStr1, "");
     179
     180    iprt::MiniString SubStr2("abcdef", 2);
     181    CHECK_EQUAL(SubStr2, "ab");
     182
     183    iprt::MiniString SubStr3("abcdef", 1);
     184    CHECK_EQUAL(SubStr3, "a");
     185
     186    iprt::MiniString SubStr4("abcdef", 6);
     187    CHECK_EQUAL(SubStr4, "abcdef");
     188
     189    iprt::MiniString SubStr5("abcdef", 7);
     190    CHECK_EQUAL(SubStr5, "abcdef");
     191
     192
     193    iprt::MiniString SubStrBase("abcdef");
     194
     195    iprt::MiniString SubStr10(SubStrBase, 0);
     196    CHECK_EQUAL(SubStr10, "abcdef");
     197
     198    iprt::MiniString SubStr11(SubStrBase, 1);
     199    CHECK_EQUAL(SubStr11, "bcdef");
     200
     201    iprt::MiniString SubStr12(SubStrBase, 1, 1);
     202    CHECK_EQUAL(SubStr12, "b");
     203
     204    iprt::MiniString SubStr13(SubStrBase, 2, 3);
     205    CHECK_EQUAL(SubStr13, "cde");
     206
     207    iprt::MiniString SubStr14(SubStrBase, 2, 4);
     208    CHECK_EQUAL(SubStr14, "cdef");
     209
     210    iprt::MiniString SubStr15(SubStrBase, 2, 5);
     211    CHECK_EQUAL(SubStr15, "cdef");
     212
     213
    169214    /* special constructor and assignment arguments */
    170215    iprt::MiniString StrCtor1("");
     
    211256#undef CHECK_DUMP
    212257#undef CHECK_DUMP_I
     258#undef CHECK_EQUAL
    213259}
    214260
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