1. use a manifest constant wxNO_LEN instead of -1 for lengths everywhere

2. reimplemented UTF-16/32 conversions using To/FromWChar() API instead
   of MB2WC/WC2MB for Windows (or rather SIZEOF_WCHAR_T == 2 platforms),
   the first tangible result is that reading UTF-32BE text streams now
   works too
3. more off by 1 fixes


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38585 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2006-04-05 22:12:45 +00:00
parent e6a87338f0
commit 467e04791c
4 changed files with 511 additions and 415 deletions

View File

@ -33,10 +33,10 @@ public:
// override the base class virtual function(s) to use our m_conv
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = -1) const;
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = -1) const;
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
virtual size_t GetMBNulLen() const { return m_conv->GetMBNulLen(); }

View File

@ -1,11 +1,12 @@
///////////////////////////////////////////////////////////////////////////////
// Name: strconv.h
// Purpose: conversion routines for char sets any Unicode
// Author: Robert Roebling, Ove Kaaven
// Author: Ove Kaaven, Robert Roebling, Vadim Zeitlin
// Modified by:
// Created: 29/01/98
// RCS-ID: $Id$
// Copyright: (c) 1998 Ove Kaaven, Robert Roebling, Vadim Zeitlin
// Copyright: (c) 1998 Ove Kaaven, Robert Roebling
// (c) 1998-2006 Vadim Zeitlin
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
@ -31,6 +32,10 @@
// the error value returned by wxMBConv methods
#define wxCONV_FAILED ((size_t)-1)
// the default value for some length parameters meaning that the string is
// NUL-terminated
#define wxNO_LEN ((size_t)-1)
// ----------------------------------------------------------------------------
// wxMBConv (abstract base class for conversions)
// ----------------------------------------------------------------------------
@ -54,12 +59,13 @@ public:
// characters, not bytes) of the converted string including any trailing
// L'\0' or (possibly multiple) '\0'(s). If the conversion fails or if
// there is not enough space for everything, including the trailing NUL
// character(s), in the output buffer, (size_t)-1 is returned.
// character(s), in the output buffer, wxCONV_FAILED is returned.
//
// In the special case when dstLen is 0 (outputBuf may be NULL then) the
// return value is the length of the needed buffer but nothing happens
// otherwise. If srcLen is -1, the entire string, up to and including the
// trailing NUL(s), is converted, otherwise exactly srcLen bytes are.
// otherwise. If srcLen is wxNO_LEN, the entire string, up to and
// including the trailing NUL(s), is converted, otherwise exactly srcLen
// bytes are.
//
// Typical usage:
//
@ -70,10 +76,10 @@ public:
// conv.ToWChar(wbuf, dstLen, src);
//
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = -1) const;
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = -1) const;
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
// Convenience functions for translating NUL-terminated strings: returns
@ -261,7 +267,16 @@ private:
class WXDLLIMPEXP_BASE wxMBConvUTF16Base : public wxMBConv
{
public:
virtual size_t GetMBNulLen() const { return 2; }
enum { BYTES_PER_CHAR = 2 };
virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
protected:
// return the length of the buffer using srcLen if it's not wxNO_LEN and
// computing the length ourselves if it is; also checks that the length is
// even if specified as we need an entire number of UTF-16 characters and
// returns wxNO_LEN which indicates error if it is odd
static size_t GetLength(const char *src, size_t srcLen);
};
// ----------------------------------------------------------------------------
@ -271,8 +286,15 @@ public:
class WXDLLIMPEXP_BASE wxMBConvUTF16LE : public wxMBConvUTF16Base
{
public:
#if SIZEOF_WCHAR_T == 2
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
#else
virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
#endif
virtual wxMBConv *Clone() const { return new wxMBConvUTF16LE; }
};
@ -283,8 +305,15 @@ public:
class WXDLLIMPEXP_BASE wxMBConvUTF16BE : public wxMBConvUTF16Base
{
public:
#if SIZEOF_WCHAR_T == 2
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
#else
virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
#endif
virtual wxMBConv *Clone() const { return new wxMBConvUTF16BE; }
};
@ -295,7 +324,15 @@ public:
class WXDLLIMPEXP_BASE wxMBConvUTF32Base : public wxMBConv
{
public:
virtual size_t GetMBNulLen() const { return 4; }
enum { BYTES_PER_CHAR = 4 };
virtual size_t GetMBNulLen() const { return BYTES_PER_CHAR; }
protected:
// this is similar to wxMBConvUTF16Base method with the same name except
// that, of course, it verifies that length is divisible by 4 if given and
// not by 2
static size_t GetLength(const char *src, size_t srcLen);
};
// ----------------------------------------------------------------------------
@ -305,8 +342,15 @@ public:
class WXDLLIMPEXP_BASE wxMBConvUTF32LE : public wxMBConvUTF32Base
{
public:
#if SIZEOF_WCHAR_T == 2
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
#else
virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
#endif
virtual wxMBConv *Clone() const { return new wxMBConvUTF32LE; }
};
@ -317,8 +361,15 @@ public:
class WXDLLIMPEXP_BASE wxMBConvUTF32BE : public wxMBConvUTF32Base
{
public:
#if SIZEOF_WCHAR_T == 2
virtual size_t ToWChar(wchar_t *dst, size_t dstLen,
const char *src, size_t srcLen = wxNO_LEN) const;
virtual size_t FromWChar(char *dst, size_t dstLen,
const wchar_t *src, size_t srcLen = wxNO_LEN) const;
#else
virtual size_t MB2WC(wchar_t *outputBuf, const char *psz, size_t outputSize) const;
virtual size_t WC2MB(char *outputBuf, const wchar_t *psz, size_t outputSize) const;
#endif
virtual wxMBConv *Clone() const { return new wxMBConvUTF32BE; }
};

File diff suppressed because it is too large Load Diff

View File

@ -1013,13 +1013,7 @@ wxString::wxString(const char *psz, const wxMBConv& conv, size_t nLength)
{
if ( nLength == npos )
{
nLength = (size_t)-1;
}
else if ( nLength == length() )
{
// this is important to avoid copying the string in cMB2WC: we're
// already NUL-terminated so we can pass this NUL with the data
nLength++;
nLength = wxNO_LEN;
}
size_t nLenWide;
@ -1048,13 +1042,7 @@ wxString::wxString(const wchar_t *pwz, const wxMBConv& conv, size_t nLength)
{
if ( nLength == npos )
{
nLength = (size_t)-1;
}
else if ( nLength == length() )
{
// this is important to avoid copying the string in cMB2WC: we're
// already NUL-terminated so we can pass this NUL with the data
nLength++;
nLength = wxNO_LEN;
}
size_t nLenMB;