Search code examples
c++cwidechar

Copy a multi-string into a buffer


I'm using a windows-api that returns a wide-char multi-string as result. The result is same as below:

L"apple\0banana\0orange\0\0"

Is there any standard function or good performance solution to copy this structure to a buffer?

copy_wide_char_multi_string(dst, src); // dst and src are wchar_t arrays

Solution

  • I've never bothered to work with wide character strings so consider this a guideline.

    You can implement an algorithm like the following:

    wchar_t * wide_string = L"something\0something else\0herp\0derp\0\0";
    int size = 0;
    
    int i = wcslen(wide_string + size);     // length of wide string
    size += i + 1;                          // length of wide string inc. null terminator
    while (true)
    {
        int i = wcslen(wide_string + size); // length of wide string
        size += i + 1;                      // length of wide string inc. null terminator
        if (i == 0) break;                  // if length was 0 (2 nulls in a row) break
    }
    ++size;                                 // count final null as part of size
    

    This will give you the size of the data in the buffer. Once you have that you can just use wmemcpy on it