VirtualBox

Changeset 35567 in vbox for trunk/include/iprt/cpp


Ignore:
Timestamp:
Jan 14, 2011 2:16:45 PM (14 years ago)
Author:
vboxsync
Message:

IPRT: fix rare crash in MiniString::substr(); rename substr() to substrCP() and add a substr that operates on bytes, not codepoints; more to come

File:
1 edited

Legend:

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

    r35128 r35567  
    185185     * String length in bytes.
    186186     *
    187      * Returns the length of the member string, which is equal to strlen(c_str()).
    188      * In other words, this does not count unicode codepoints but returns the number
    189      * of bytes.  This is always cached so calling this is cheap and requires no
     187     * Returns the length of the member string in bytes, which is equal to strlen(c_str()).
     188     * In other words, this does not count unicode codepoints; use utf8length() for that.
     189     * The byte length is always cached so calling this is cheap and requires no
    190190     * strlen() invocation.
    191191     *
     
    195195    {
    196196        return m_cch;
     197    }
     198
     199    /**
     200     * String length in UTF-8 codepoints.
     201     *
     202     * As opposed to length(), which returns the length in bytes, this counts the number
     203     * of UTF-8 codepoints. This is *not* cached so calling this is expensive.
     204     *
     205     * @returns Number of codepoints in the member string.
     206     */
     207    size_t utf8length() const
     208    {
     209        return m_psz ? RTStrUniLen(m_psz) : 0;
    197210    }
    198211
     
    652665     * Find the given substring.
    653666     *
    654      * Looks for pcszFind in "this" starting at "pos" and returns its position,
    655      * counting from the beginning of "this" at 0.
     667     * Looks for pcszFind in "this" starting at "pos" and returns its position
     668     * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
    656669     *
    657670     * @param   pcszFind        The substring to find.
     
    676689     * Returns a substring of "this" as a new Utf8Str.
    677690     *
    678      * Works exactly like its equivalent in std::string except that this interprets
    679      * pos and n as unicode codepoints instead of bytes.  With the default
    680      * parameters "0" and "npos", this always copies the entire string.
     691     * Works exactly like its equivalent in std::string. With the default
     692     * parameters "0" and "npos", this always copies the entire string. The
     693     * "pos" and "n" arguments represent bytes; it is the caller's responsibility
     694     * to ensure that the offsets do not copy invalid UTF-8 sequences. When
     695     * used in conjunction with find() and length(), this will work.
     696     *
     697     * @param   pos             Index of first byte offset to copy from "this", counting from 0.
     698     * @param   n               Number of bytes to copy, starting with the one at "pos".
     699     *                          The copying will stop if the null terminator is encountered before
     700     *                          n bytes have been copied.
     701     */
     702    iprt::MiniString substr(size_t pos = 0, size_t n = npos) const
     703    {
     704        return MiniString(*this, pos, n);
     705    }
     706
     707    /**
     708     * Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
     709     * this variant takes codepoint offsets instead of byte offsets.
    681710     *
    682711     * @param   pos             Index of first unicode codepoint to copy from
     
    686715     *                          terminator is encountered before n codepoints have
    687716     *                          been copied.
    688      *
    689      * @remarks This works on code points, not bytes!
    690      */
    691     iprt::MiniString substr(size_t pos = 0, size_t n = npos) const;
     717     */
     718    iprt::MiniString substrCP(size_t pos = 0, size_t n = npos) const;
    692719
    693720    /**
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