开发者

How do you concatenate two wchar_t* together?

I have a base wchar_t* and I'm looking to append a开发者_如何学编程nother one onto the end. How do I do it? I cannot use deprecated functions as I am treating warnings as errors.


Why not use a std::wstring in the first place:

wchar_t *ws1 = foo(), *ws2 = bar();
std::wstring s(ws1);
s += std::wstring(ws2);
std::wcout << s << std::endl;

If needed, std::wstring::c_str() gives you access to the result as a const wchar_t*.


 #include <wchar.h>

 wchar_t *wcsncat(wchar_t *ws1, const wchar_t *ws2, size_t n); 

The wcsncat() function appends no more than the first n characters of the string pointed to by ws2 to the end of the string pointed to by ws1. If a NULL character appears in ws2 before n characters, all characters up to the NULL character are appended to ws1. The first character of ws2 overwrites the terminating NULL character of ws1. A NULL terminating character is always appended to the result, and if the objects used for copying overlap, the behavior is undefined.

ws1

Is the null-terminated destination string.

ws2

Is the null-terminated source string.

n

Is the number of characters to append.


The most portable way to do this is wcsncat as mentioned above, but it sounds like you're committed to the "secure CRT" features of Visual C++ 2005 and later. (Only Microsoft has "deprecated" those functions.) If that's the case, use wcsncat_s, declared in string.h.


Using the wstrncat/wcsncat functions is good, but I think the best version of these safe string functions are the 'l' ones created by Open BSD, i.e. strlcat and wstrlcat. With the 'n' versions, you can end up with a string that doesn't have a null terminator so you can still have security issues. Also certain implementations will zero out the unused space in the buffer which can slow things down a bit.

The wikipedia page has some more information on these functions: Strlcpy et al.. The only problem is these are not in the standard libraries so you have to include the code in your project yourself.

Here's the source to a wstrlcat function:

/*
 * Appends src to string dst of size siz (unlike strncat, siz is the
 * full size of dst, not space left).  At most siz-1 characters
 * will be copied.  Always NUL terminates (unless siz = siz, truncation occurred.
 */
size_t wstrlcat(wchar_t *dst, const wchar_t *src, size_t siz)
{
        wchar_t *d = dst;
        const wchar_t *s = src;
        size_t n = siz;
        size_t dlen;

        /* Find the end of dst and adjust bytes left but don't go past end */
        while(n-- != 0 && *d != L'\0') {
                d++;
        }

        dlen = d - dst;
        n = siz - dlen;

        if (n == 0) {
                return(dlen + wcslen(s));
        }

        while(*s != L'\0')
        {
                if(n != 1)
                {
                        *d++ = *s;
                        n--;
                }
                s++;
        }

        *d = '\0';
        return(dlen + (s - src));        /* count does not include NUL */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜