// because we want the buffer to always be NUL-terminated, even if the
// input isn't (as otherwise the caller has no way to know its length)
wxWCharBuffer wbuf(dstLen);
- wbuf.data()[dstLen] = L'\0';
if ( ToWChar(wbuf.data(), dstLen, inBuff, inLen) != wxCONV_FAILED )
{
if ( outLen )
{
// skip the next char too as we decoded a surrogate
wp++;
+ if ( srcLen != wxNO_LEN )
+ srcLen--;
}
#else // wchar_t is UTF-32
code = *wp & 0x7fffffff;
size_t len = 0;
- while ((srcLen == wxNO_LEN ? *psz : srcLen--) && ((!buf) || (len < n)))
+ // The length can be either given explicitly or computed implicitly for the
+ // NUL-terminated strings.
+ const bool isNulTerminated = srcLen == wxNO_LEN;
+ while ((isNulTerminated ? *psz : srcLen--) && ((!buf) || (len < n)))
{
wxUint32 cc;
}
}
- if (srcLen == wxNO_LEN && buf && (len < n))
- *buf = 0;
+ if ( isNulTerminated )
+ {
+ // Add the trailing NUL in this case if we have a large enough buffer.
+ if ( buf && (len < n) )
+ *buf = 0;
+
+ // And count it in any case.
+ len++;
+ }
- return len + 1;
+ return len;
}
// ============================================================================