// all the complication come from the fact that this function, for
// historical reasons, must behave in 2 subtly different ways when it's
// called with a fixed number of characters and when it's called for the
- // entire NUL-terminated string: in the former case (srcEnd == NULL) we
+ // entire NUL-terminated string: in the former case (srcEnd != NULL) we
// must count all characters we convert, NUL or not; but in the latter we
// do not count the trailing NUL -- but still count all the NULs inside the
// string
if ( !srcEnd )
{
// we convert just one chunk in this case as this is the entire
- // string anyhow
+ // string anyhow (and we don't count the trailing NUL in this case)
break;
}
- // advance the input pointer past the end of this chunk
+ // advance the input pointer past the end of this chunk: notice that we
+ // will always stop before srcEnd because we know that the chunk is
+ // always properly NUL-terminated
while ( NotAllNULs(src, nulLen) )
{
// notice that we must skip over multiple bytes here as we suppose
src += nulLen;
}
- src += nulLen; // skipping over its terminator as well
+ // if the buffer ends before this NUL, we shouldn't count it in our
+ // output so skip the code below
+ if ( src == srcEnd )
+ break;
+
+ // do count this terminator as it's inside the buffer we convert
+ dstWritten++;
+ if ( dst )
+ dst++;
+
+ src += nulLen; // skip the terminator itself
- // note that ">=" (and not just "==") is needed here as the terminator
- // we skipped just above could be inside or just after the buffer
- // delimited by srcEnd
if ( src >= srcEnd )
break;
-
- // if we got here then this wasn't the last chunk in this string and
- // hence we must count an extra char for L'\0' even when converting a
- // fixed number of characters
- if ( srcEnd )
- {
- dstWritten++;
- if ( dst )
- dst++;
- }
}
return dstWritten;
return wxCONV_FAILED;
dstWritten += lenChunk;
- if ( src+lenChunk < srcEnd || isNulTerminated )
+ if ( src + lenChunk < srcEnd || isNulTerminated )
dstWritten += lenNul;
if ( dst )
return wxCONV_FAILED;
dst += lenChunk;
- if ( src+lenChunk < srcEnd || isNulTerminated )
+ if ( src + lenChunk < srcEnd || isNulTerminated )
dst += lenNul;
}
}
return wxCharBuffer();
}
+const wxWCharBuffer wxMBConv::cMB2WC(const wxScopedCharBuffer& buf) const
+{
+ const size_t srcLen = buf.length();
+ if ( srcLen )
+ {
+ const size_t dstLen = ToWChar(NULL, 0, buf, srcLen);
+ if ( dstLen != wxCONV_FAILED )
+ {
+ wxWCharBuffer wbuf(dstLen);
+ wbuf.data()[dstLen] = L'\0';
+ if ( ToWChar(wbuf.data(), dstLen, buf, srcLen) != wxCONV_FAILED )
+ return wbuf;
+ }
+ }
+
+ return wxWCharBuffer();
+}
+
+const wxCharBuffer wxMBConv::cWC2MB(const wxScopedWCharBuffer& wbuf) const
+{
+ const size_t srcLen = wbuf.length();
+ if ( srcLen )
+ {
+ const size_t dstLen = FromWChar(NULL, 0, wbuf, srcLen);
+ if ( dstLen != wxCONV_FAILED )
+ {
+ wxCharBuffer buf(dstLen);
+ buf.data()[dstLen] = '\0';
+ if ( FromWChar(buf.data(), dstLen, wbuf, srcLen) != wxCONV_FAILED )
+ return buf;
+ }
+ }
+
+ return wxCharBuffer();
+}
+
// ----------------------------------------------------------------------------
// wxMBConvLibc
// ----------------------------------------------------------------------------