VirtualBox

Changeset 36501 in vbox


Ignore:
Timestamp:
Apr 1, 2011 1:40:21 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
70912
Message:

IPRT: add join/split and + operators to the string class

Location:
trunk
Files:
3 edited

Legend:

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

    r36426 r36501  
    3030#include <iprt/string.h>
    3131#include <iprt/stdarg.h>
     32#include <iprt/cpp/list.h>
    3233
    3334#include <new>
     
    3536namespace iprt
    3637{
     38
     39/**
     40 * @defgroup grp_rt_cpp_string     C++ String support
     41 * @ingroup grp_rt_cpp
     42 * @{
     43 */
    3744
    3845/**
     
    808815    int toInt(uint32_t &i) const;
    809816
     817    /** Splitting behavior regarding empty sections in the string. */
     818    enum SplitMode
     819    {
     820        KeepEmptyParts,  /**< Empty parts are added as empty strings to the result list. */
     821        RemoveEmptyParts /**< Empty parts are skipped. */
     822    };
     823
     824    /**
     825     * Splits a string separated by strSep into its parts.
     826     *
     827     * @param   strSep       The separator to search for.
     828     * @param   mode         How should empty parts be handled.
     829     * @returns separated strings as string list.
     830     */
     831    iprt::list<iprt::MiniString, iprt::MiniString*> split(const iprt::MiniString &strSep, SplitMode mode = RemoveEmptyParts);
     832
     833    /**
     834     * Joins a list of strings together using the provided separator.
     835     *
     836     * @param   list         The list to join.
     837     * @param   strSep       The separator used for joining.
     838     * @returns joined string.
     839     */
     840    static iprt::MiniString join(const iprt::list<iprt::MiniString, iprt::MiniString*> &list, const iprt::MiniString &strSep = "");
     841       
    810842protected:
    811843
     
    883915};
    884916
    885 } // namespace iprt
     917/** @} */
     918
     919} /* namespace iprt */
     920
     921/**
     922 * @addtogroup grp_rt_cpp_string
     923 * @{
     924 */
     925
     926/**
     927 * @relates iprt::MiniString
     928 *
     929 * Concatenate two strings.
     930 *
     931 * @param   one        String one.
     932 * @param   other      String two.
     933 * @returns the concatenate string.
     934 */
     935RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &one, const iprt::MiniString &other);
     936
     937/**
     938 * @relates iprt::MiniString
     939 *
     940 * Concatenate two strings.
     941 *
     942 * @param   one        String one.
     943 * @param   pcszOther  String two.
     944 * @returns the concatenate string.
     945 */
     946RTDECL(const iprt::MiniString) operator+(const iprt::MiniString &one, const char *pcszOther);
     947
     948/**
     949 * @relates iprt::MiniString
     950 *
     951 * Concatenate two strings.
     952 *
     953 * @param   pcszOne    String one.
     954 * @param   other      String two.
     955 * @returns the concatenate string.
     956 */
     957RTDECL(const iprt::MiniString) operator+(const char *pcszOne, const iprt::MiniString &other);
     958
     959/** @} */
    886960
    887961#endif
  • trunk/src/VBox/Runtime/common/string/ministring.cpp

    r35567 r36501  
    332332}
    333333
     334iprt::list<iprt::MiniString, iprt::MiniString*> MiniString::split(const iprt::MiniString &strSep, SplitMode mode /* = RemoveEmptyParts */)
     335{
     336    iprt::list<iprt::MiniString> res;
     337    if (!m_psz)
     338        return res;
     339    if (strSep.isEmpty())
     340    {
     341        res.append(iprt::MiniString(m_psz));
     342        return res;
     343    }
     344
     345    size_t cch = m_cch;
     346    char *pszTmp = m_psz;
     347    while(cch > 0)
     348    {
     349        char *pszNext = strstr(pszTmp, strSep.c_str());
     350        if (!pszNext)
     351            break;
     352        size_t chNext = pszNext - pszTmp;
     353        if (   chNext > 0
     354            || mode == KeepEmptyParts)
     355            res.append(iprt::MiniString(pszTmp, chNext));
     356        pszTmp += chNext + strSep.length();
     357        cch    -= chNext + strSep.length();
     358    }
     359    /* Some characters left at the end? */
     360    if (cch > 0)
     361        res.append(iprt::MiniString(pszTmp, cch));
     362
     363    return res;
     364}
     365
     366/* static */
     367iprt::MiniString MiniString::join(const iprt::list<iprt::MiniString, iprt::MiniString*> &list, const iprt::MiniString &strSep /* = "" */)
     368{
     369    MiniString res;
     370    if (list.size() > 1)
     371    {
     372        for(size_t i = 0; i < list.size() - 1; ++i)
     373            res += list.at(i) + strSep;
     374        res += list.last();
     375    }
     376
     377    return res;
     378}
     379
     380const iprt::MiniString operator+(const iprt::MiniString &one, const iprt::MiniString &other)
     381{
     382    iprt::MiniString res(one);
     383
     384    return res += other;
     385}
     386
     387const iprt::MiniString operator+(const iprt::MiniString &one, const char *pcszOther)
     388{
     389    iprt::MiniString res(one);
     390
     391    return res += pcszOther;
     392}
     393
     394const iprt::MiniString operator+(const char *pcszOne, const iprt::MiniString &other)
     395{
     396    iprt::MiniString res(pcszOne);
     397
     398    return res += other;
     399}
     400
  • trunk/src/VBox/Runtime/testcase/tstIprtMiniString.cpp

    r35568 r36501  
    256256    CHECK_EQUAL(strTest.substr(pos), "ßäbcdef");
    257257
     258    /* split */
     259    iprt::list<iprt::MiniString> spList1 = iprt::MiniString("##abcdef##abcdef####abcdef##").split("##", iprt::MiniString::RemoveEmptyParts);
     260    RTTESTI_CHECK(spList1.size() == 3);
     261    for (size_t i = 0; i < spList1.size(); ++i)
     262        RTTESTI_CHECK(spList1.at(i) == "abcdef");
     263    iprt::list<iprt::MiniString> spList2 = iprt::MiniString("##abcdef##abcdef####abcdef##").split("##", iprt::MiniString::KeepEmptyParts);
     264    RTTESTI_CHECK_RETV(spList2.size() == 5);
     265    RTTESTI_CHECK(spList2.at(0) == "");
     266    RTTESTI_CHECK(spList2.at(1) == "abcdef");
     267    RTTESTI_CHECK(spList2.at(2) == "abcdef");
     268    RTTESTI_CHECK(spList2.at(3) == "");
     269    RTTESTI_CHECK(spList2.at(4) == "abcdef");
     270    iprt::list<iprt::MiniString> spList3 = iprt::MiniString().split("##", iprt::MiniString::KeepEmptyParts);
     271    RTTESTI_CHECK(spList3.size() == 0);
     272    iprt::list<iprt::MiniString> spList4 = iprt::MiniString().split("");
     273    RTTESTI_CHECK(spList4.size() == 0);
     274    iprt::list<iprt::MiniString> spList5 = iprt::MiniString("abcdef").split("");
     275    RTTESTI_CHECK_RETV(spList5.size() == 1);
     276    RTTESTI_CHECK(spList5.at(0) == "abcdef");
     277
     278    /* join */
     279    iprt::list<iprt::MiniString> jnList;
     280    strTest = iprt::MiniString::join(jnList);
     281    RTTESTI_CHECK(strTest == "");
     282    strTest = iprt::MiniString::join(jnList, "##");
     283    RTTESTI_CHECK(strTest == "");
     284    for (size_t i = 0; i < 5; ++i)
     285        jnList.append("abcdef");
     286    strTest = iprt::MiniString::join(jnList);
     287    RTTESTI_CHECK(strTest == "abcdefabcdefabcdefabcdefabcdef");
     288    strTest = iprt::MiniString::join(jnList, "##");
     289    RTTESTI_CHECK(strTest == "abcdef##abcdef##abcdef##abcdef##abcdef");
     290
    258291    /* special constructor and assignment arguments */
    259292    iprt::MiniString StrCtor1("");
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette