VirtualBox

Changeset 68123 in vbox for trunk


Ignore:
Timestamp:
Jul 26, 2017 2:31:35 PM (7 years ago)
Author:
vboxsync
Message:

RTCString: Added assign() methods.

Location:
trunk
Files:
2 edited

Legend:

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

    r68094 r68123  
    5050 * behave the same.  In both cases, RTCString allocates no memory, reports
    5151 * a zero length and zero allocated bytes for both, and returns an empty
    52  * C string from c_str().
     52 * C-style string from c_str().
    5353 *
    5454 * @note    RTCString ASSUMES that all strings it deals with are valid UTF-8.
     
    9292
    9393    /**
    94      * Creates a copy of a C string.
     94     * Creates a copy of a C-style string.
    9595     *
    9696     * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty.
     
    126126
    127127    /**
    128      * Create a partial copy of a C string.
     128     * Create a partial copy of a C-style string.
    129129     *
    130130     * @param   a_pszSrc        The source string (UTF-8).
     
    330330
    331331    /**
     332     * Assigns a copy of another RTCString.
     333     *
     334     * @param   a_rSrc          Reference to the source string.
     335     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     336     */
     337    RTCString &assign(const RTCString &a_rSrc);
     338
     339    /**
     340     * Assigns a copy of a C-style string.
     341     *
     342     * @param   a_pszSrc        Pointer to the C-style source string.
     343     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     344     * @remarks ASSUMES valid
     345     */
     346    RTCString &assign(const char *a_pszSrc);
     347
     348    /**
     349     * Assigns a partial copy of another RTCString.
     350     *
     351     * @param   a_rSrc          The source string.
     352     * @param   a_offSrc        The byte offset into the source string.
     353     * @param   a_cchSrc        The max number of chars (encoded UTF-8 bytes)
     354     *                          to copy from the source string.
     355     */
     356    RTCString &assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos);
     357
     358    /**
     359     * Assigns a partial copy of a C-style string.
     360     *
     361     * @param   a_pszSrc        The source string (UTF-8).
     362     * @param   a_cchSrc        The max number of chars (encoded UTF-8 bytes)
     363     *                          to copy from the source string.
     364     */
     365    RTCString &assign(const char *a_pszSrc, size_t a_cchSrc);
     366
     367    /**
     368     * Assigs a string containing @a a_cTimes repetitions of the character @a a_ch.
     369     *
     370     * @param   a_cTimes        The number of times the character is repeated.
     371     * @param   a_ch            The character to fill the string with.
     372     */
     373    RTCString &assign(size_t a_cTimes, char a_ch);
     374
     375    /**
    332376     * Assigns the output of the string format operation (RTStrPrintf).
    333377     *
     
    358402
    359403    /**
    360      * Appends the string @a that to @a this.
    361      *
    362      * @param   that            The string to append.
    363      *
    364      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    365      *
    366      * @returns Reference to the object.
    367      */
    368     RTCString &append(const RTCString &that);
    369 
    370     /**
    371      * Appends the string @a pszThat to @a this.
    372      *
    373      * @param   pszThat         The C string to append.
    374      *
    375      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    376      *
    377      * @returns Reference to the object.
    378      */
    379     RTCString &append(const char *pszThat);
     404     * Appends the string @a that to @a rThat.
     405     *
     406     * @param   rThat            The string to append.
     407     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     408     * @returns Reference to the object.
     409     */
     410    RTCString &append(const RTCString &rThat);
     411
     412    /**
     413     * Appends the string @a pszSrc to @a this.
     414     *
     415     * @param   pszSrc          The C-style string to append.
     416     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
     417     * @returns Reference to the object.
     418     */
     419    RTCString &append(const char *pszSrc);
    380420
    381421    /**
     
    386426     *                          not codepoint).
    387427     * @param   cchMax          The maximum number of bytes to append.
    388      *
    389      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    390      *
     428     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    391429     * @returns Reference to the object.
    392430     */
     
    396434     * Appends the first @a cchMax chars from string @a pszThat to @a this.
    397435     *
    398      * @param   pszThat         The C string to append.
     436     * @param   pszThat         The C-style string to append.
    399437     * @param   cchMax          The maximum number of bytes to append.
    400      *
    401      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    402      *
     438     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    403439     * @returns Reference to the object.
    404440     */
     
    409445     *
    410446     * @param   ch              The character to append.
    411      *
    412      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    413      *
     447     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    414448     * @returns Reference to the object.
    415449     */
     
    420454     *
    421455     * @param   uc              The unicode code point to append.
    422      *
    423      * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    424      *
     456     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
    425457     * @returns Reference to the object.
    426458     */
     
    430462     * Shortcut to append(), RTCString variant.
    431463     *
    432      * @param that              The string to append.
    433      *
     464     * @param   rThat           The string to append.
    434465     * @returns Reference to the object.
    435466     */
     
    442473     * Shortcut to append(), const char* variant.
    443474     *
    444      * @param pszThat           The C string to append.
    445      *
     475     * @param   pszThat         The C-style string to append.
    446476     * @returns                 Reference to the object.
    447477     */
     
    579609     * Returns the byte at the given index, or a null byte if the index is not
    580610     * smaller than length().  This does _not_ count codepoints but simply points
    581      * into the member C string.
     611     * into the member C-style string.
    582612     *
    583613     * @param   i       The index into the string buffer.
     
    592622
    593623    /**
    594      * Returns the contained string as a C-style const char* pointer.
    595      * This never returns NULL; if the string is empty, this returns a
    596      * pointer to static null byte.
     624     * Returns the contained string as a const C-style string pointer.
     625     *
     626     * This never returns NULL; if the string is empty, this returns a pointer to
     627     * static null byte.
    597628     *
    598629     * @returns const pointer to C-style string.
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r68094 r68123  
    5050
    5151
     52RTCString &RTCString::assign(const RTCString &a_rSrc)
     53{
     54    size_t const cchSrc = a_rSrc.length();
     55    if (cchSrc > 0)
     56    {
     57        reserve(cchSrc + 1);
     58        memcpy(m_psz, a_rSrc.c_str(), cchSrc);
     59        m_psz[cchSrc] = '\0';
     60        m_cch = cchSrc;
     61        return *this;
     62    }
     63    setNull();
     64    return *this;
     65
     66}
     67
     68RTCString &RTCString::assign(const char *a_pszSrc)
     69{
     70    if (a_pszSrc)
     71    {
     72        size_t cchSrc = strlen(a_pszSrc);
     73        if (cchSrc)
     74        {
     75            reserve(cchSrc + 1);
     76            memcpy(m_psz, a_pszSrc, cchSrc);
     77            m_psz[cchSrc] = '\0';
     78            m_cch = cchSrc;
     79            return *this;
     80        }
     81    }
     82    setNull();
     83    return *this;
     84}
     85
     86RTCString &RTCString::assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc /*= npos*/)
     87{
     88    AssertReturn(&a_rSrc != this, *this);
     89    if (a_offSrc < a_rSrc.length())
     90    {
     91        size_t cchMax = a_rSrc.length() - a_offSrc;
     92        if (a_cchSrc > cchMax)
     93            a_cchSrc = cchMax;
     94        reserve(a_cchSrc + 1);
     95        memcpy(m_psz, a_rSrc.c_str(), a_cchSrc);
     96        m_psz[a_cchSrc] = '\0';
     97        m_cch = a_cchSrc;
     98    }
     99    else
     100        setNull();
     101    return *this;
     102}
     103
     104RTCString &RTCString::assign(const char *a_pszSrc, size_t a_cchSrc)
     105{
     106    if (a_cchSrc)
     107    {
     108        a_cchSrc = RTStrNLen(a_pszSrc, a_cchSrc);
     109        reserve(a_cchSrc + 1);
     110        memcpy(m_psz, a_pszSrc, a_cchSrc);
     111        m_psz[a_cchSrc] = '\0';
     112        m_cch = a_cchSrc;
     113    }
     114    else
     115        setNull();
     116    return *this;
     117}
     118
     119RTCString &RTCString::assign(size_t a_cTimes, char a_ch)
     120{
     121    reserve(a_cTimes + 1);
     122    memset(m_psz, a_ch, a_cTimes);
     123    return *this;
     124}
     125
     126
    52127RTCString &RTCString::printf(const char *pszFormat, ...)
    53128{
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