mardi 29 mars 2016

How to format wide char string via vswprintf on OSX (want to return std::wstring)

I need implement a function to format wide char string and return std::wstring. My implementation is:

std::wstring format(const wchar_t* fmt, ...)
{
    std::wstring ret;

    va_list va;
    va_start(va, fmt);

    int size = vswprintf(nullptr, 0, fmt, va);
    if (size > 0)
    {
        ret.resize(size + 1);
        vswprintf(&ret[0], size + 1, fmt, va);
    }

    va_end(va);
    return ret;
}

it works well on windows, but unfortunately it doesn't work on osx because vswprintf(nullptr, 0, fmt, va) will return -1.

only std can be used and I can't assume the string length is in a certain range.

I think there should be some way to do that, but I can't find, can you help? thanks

Aucun commentaire:

Enregistrer un commentaire