VirtualBox

Changeset 22736 in vbox for trunk/src/VBox/Runtime/common


Ignore:
Timestamp:
Sep 3, 2009 11:32:59 AM (15 years ago)
Author:
vboxsync
Message:

IPRT: Added RTStrDupN.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/string.cpp

    r21337 r22736  
    6565RTDECL(char *) RTStrDup(const char *pszString)
    6666{
    67     Assert(VALID_PTR(pszString));
     67    AssertPtr(pszString);
    6868    size_t cch = strlen(pszString) + 1;
    6969    char *psz = (char *)RTMemAlloc(cch);
     
    8585RTDECL(int)  RTStrDupEx(char **ppszString, const char *pszString)
    8686{
    87     Assert(VALID_PTR(ppszString));
    88     Assert(VALID_PTR(pszString));
     87    AssertPtr(ppszString);
     88    AssertPtr(pszString);
    8989
    9090    size_t cch = strlen(pszString) + 1;
     
    100100RT_EXPORT_SYMBOL(RTStrDupEx);
    101101
     102
     103/**
     104 * Allocates a new copy of the given UTF-8 substring.
     105 *
     106 * @returns Pointer to the allocated UTF-8 substring.
     107 * @param   pszString       UTF-8 string to duplicate.
     108 * @param   cchMax          The max number of chars to duplicate, not counting
     109 *                          the terminator.
     110 */
     111RTDECL(char *) RTStrDupN(const char *pszString, size_t cchMax)
     112{
     113    AssertPtr(pszString);
     114    char  *pszEnd = (char *)memchr(pszString, '\0', cchMax);
     115    size_t cch    = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
     116    char  *pszDst = (char *)RTMemAlloc(cch);
     117    if (pszDst)
     118    {
     119        memcpy(pszDst, pszString, cch);
     120        pszDst[cch] = '\0';
     121    }
     122    return pszDst;
     123}
     124RT_EXPORT_SYMBOL(RTStrDupN);
     125
     126
     127
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