added some 'wxString::' portions to make referenced functions auto-linked by Doxygen
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56554 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -116,9 +116,9 @@ char character buffer. Of course, the latter only works if the string contains
|
|||||||
data representable in the current locale encoding. This will always be the case
|
data representable in the current locale encoding. This will always be the case
|
||||||
if the string had been initially constructed from a narrow string or if it
|
if the string had been initially constructed from a narrow string or if it
|
||||||
contains only 7-bit ASCII data but otherwise this conversion is not guaranteed
|
contains only 7-bit ASCII data but otherwise this conversion is not guaranteed
|
||||||
to succeed. And as with @c FromUTF8() example above, you can always use @c
|
to succeed. And as with wxString::FromUTF8() example above, you can always use
|
||||||
ToUTF8() to retrieve the string contents in UTF-8 encoding -- this, unlike
|
wxString::ToUTF8() to retrieve the string contents in UTF-8 encoding -- this,
|
||||||
converting to @c char* using the current locale, never fails
|
unlike converting to @c char* using the current locale, never fails.
|
||||||
|
|
||||||
To summarize, Unicode support in wxWidgets is mostly transparent for the
|
To summarize, Unicode support in wxWidgets is mostly transparent for the
|
||||||
application and if you use wxString objects for storing all the character data
|
application and if you use wxString objects for storing all the character data
|
||||||
@@ -156,8 +156,8 @@ n:
|
|||||||
string iterators instead if possible or replace this expression with
|
string iterators instead if possible or replace this expression with
|
||||||
@code s.c_str() + n @endcode otherwise.
|
@code s.c_str() + n @endcode otherwise.
|
||||||
|
|
||||||
Another class of problems is related to the fact that the value returned by @c
|
Another class of problems is related to the fact that the value returned by
|
||||||
c_str() itself is also not just a pointer to a buffer but a value of helper
|
@c c_str() itself is also not just a pointer to a buffer but a value of helper
|
||||||
class wxCStrData which is implicitly convertible to both narrow and wide
|
class wxCStrData which is implicitly convertible to both narrow and wide
|
||||||
strings. Again, this mostly will be unnoticeable but can result in some
|
strings. Again, this mostly will be unnoticeable but can result in some
|
||||||
problems:
|
problems:
|
||||||
@@ -211,11 +211,11 @@ results if the string contents isn't convertible to the current locale.
|
|||||||
To be precise, the conversion will always succeed if the string was created
|
To be precise, the conversion will always succeed if the string was created
|
||||||
from a narrow string initially. It will also succeed if the current encoding is
|
from a narrow string initially. It will also succeed if the current encoding is
|
||||||
UTF-8 as all Unicode strings are representable in this encoding. However
|
UTF-8 as all Unicode strings are representable in this encoding. However
|
||||||
initializing the string using FromUTF8() method and then accessing it as a char
|
initializing the string using wxString::FromUTF8() method and then accessing it
|
||||||
string via its c_str() method is a recipe for disaster as the program may work
|
as a char string via its wxString::c_str() method is a recipe for disaster as the
|
||||||
perfectly well during testing on Unix systems using UTF-8 locale but completely
|
program may work perfectly well during testing on Unix systems using UTF-8 locale
|
||||||
fail under Windows where UTF-8 locales are never used because c_str() would
|
but completely fail under Windows where UTF-8 locales are never used because
|
||||||
return an empty string.
|
wxString::c_str() would return an empty string.
|
||||||
|
|
||||||
The simplest way to ensure that this doesn't happen is to avoid conversions to
|
The simplest way to ensure that this doesn't happen is to avoid conversions to
|
||||||
@c char* completely by using wxString throughout your program. However if the
|
@c char* completely by using wxString throughout your program. However if the
|
||||||
@@ -281,24 +281,25 @@ data loss problems due to conversion as discussed in the previous section.
|
|||||||
Even though wxWidgets always uses Unicode internally, not all the other
|
Even though wxWidgets always uses Unicode internally, not all the other
|
||||||
libraries and programs do and even those that do use Unicode may use a
|
libraries and programs do and even those that do use Unicode may use a
|
||||||
different encoding of it. So you need to be able to convert the data to various
|
different encoding of it. So you need to be able to convert the data to various
|
||||||
representations and the wxString methods ToAscii(), ToUTF8() (or its synonym
|
representations and the wxString methods wxString::ToAscii(), wxString::ToUTF8()
|
||||||
utf8_str()), mb_str(), c_str() and wc_str() can be used for this. The first of
|
(or its synonym wxString::utf8_str()), wxString::mb_str(), wxString::c_str() and
|
||||||
them should be only used for the string containing 7-bit ASCII characters only,
|
wxString::wc_str() can be used for this.
|
||||||
anything else will be replaced by some substitution character. mb_str()
|
The first of them should be only used for the string containing 7-bit ASCII characters
|
||||||
converts the string to the encoding used by the current locale and so can
|
only, anything else will be replaced by some substitution character.
|
||||||
return an empty string if the string contains characters not representable in
|
wxString::mb_str() converts the string to the encoding used by the current locale
|
||||||
it as explained in @ref overview_unicode_data_loss. The same applies to c_str()
|
and so can return an empty string if the string contains characters not representable in
|
||||||
if its result is used as a narrow string. Finally, ToUTF8() and wc_str()
|
it as explained in @ref overview_unicode_data_loss. The same applies to wxString::c_str()
|
||||||
|
if its result is used as a narrow string. Finally, wxString::ToUTF8() and wxString::wc_str()
|
||||||
functions never fail and always return a pointer to char string containing the
|
functions never fail and always return a pointer to char string containing the
|
||||||
UTF-8 representation of the string or wchar_t string.
|
UTF-8 representation of the string or wchar_t string.
|
||||||
|
|
||||||
wxString also provides two convenience functions: From8BitData() and
|
wxString also provides two convenience functions: wxString::From8BitData() and
|
||||||
To8BitData(). They can be used to create wxString from arbitrary binary data
|
wxString::To8BitData(). They can be used to create a wxString from arbitrary binary
|
||||||
without supposing that it is in current locale encoding, and then get it back,
|
data without supposing that it is in current locale encoding, and then get it back,
|
||||||
again, without any conversion or, rather, undoing the conversion used by
|
again, without any conversion or, rather, undoing the conversion used by
|
||||||
From8BitData(). Because of this you should only use From8BitData() for the
|
wxString::From8BitData(). Because of this you should only use wxString::From8BitData()
|
||||||
strings created using To8BitData(). Also notice that in spite of the
|
for the strings created using wxString::To8BitData(). Also notice that in spite
|
||||||
availability of these functions, wxString is not the ideal class for storing
|
of the availability of these functions, wxString is not the ideal class for storing
|
||||||
arbitrary binary data as they can take up to 4 times more space than needed
|
arbitrary binary data as they can take up to 4 times more space than needed
|
||||||
(when using @c wchar_t internal representation on the systems where size of
|
(when using @c wchar_t internal representation on the systems where size of
|
||||||
wide characters is 4 bytes) and you should consider using wxMemoryBuffer
|
wide characters is 4 bytes) and you should consider using wxMemoryBuffer
|
||||||
@@ -307,28 +308,28 @@ instead.
|
|||||||
Final word of caution: most of these functions may return either directly the
|
Final word of caution: most of these functions may return either directly the
|
||||||
pointer to internal string buffer or a temporary wxCharBuffer or wxWCharBuffer
|
pointer to internal string buffer or a temporary wxCharBuffer or wxWCharBuffer
|
||||||
object. Such objects are implicitly convertible to char and wchar_t pointers,
|
object. Such objects are implicitly convertible to char and wchar_t pointers,
|
||||||
respectively, and so the result of, for example, ToUTF8() can always be passed
|
respectively, and so the result of, for example, wxString::ToUTF8() can always be
|
||||||
directly to a function taking @c const @c char*. However code such as
|
passed directly to a function taking @c const @c char*. However code such as
|
||||||
@code
|
@code
|
||||||
const char *p = s.ToUTF8();
|
const char *p = s.ToUTF8();
|
||||||
...
|
...
|
||||||
puts(p); // or call any other function taking const char *
|
puts(p); // or call any other function taking const char *
|
||||||
@endcode
|
@endcode
|
||||||
does @b not work because the temporary buffer returned by ToUTF8() is destroyed
|
does @b not work because the temporary buffer returned by wxString::ToUTF8() is
|
||||||
and @c p is left pointing nowhere. To correct this you may use
|
destroyed and @c p is left pointing nowhere. To correct this you may use
|
||||||
@code
|
@code
|
||||||
wxCharBuffer p(s.ToUTF8());
|
wxCharBuffer p(s.ToUTF8());
|
||||||
puts(p);
|
puts(p);
|
||||||
@endcode
|
@endcode
|
||||||
which does work but results in an unnecessary copy of string data in the build
|
which does work but results in an unnecessary copy of string data in the build
|
||||||
configurations when ToUTF8() returns the pointer to internal string buffer. If
|
configurations when wxString::ToUTF8() returns the pointer to internal string buffer.
|
||||||
this inefficiency is important you may write
|
If this inefficiency is important you may write
|
||||||
@code
|
@code
|
||||||
const wxUTF8Buf p(s.ToUTF8());
|
const wxUTF8Buf p(s.ToUTF8());
|
||||||
puts(p);
|
puts(p);
|
||||||
@endcode
|
@endcode
|
||||||
where @c wxUTF8Buf is the type corresponding to the real return type of
|
where @c wxUTF8Buf is the type corresponding to the real return type of wxString::ToUTF8().
|
||||||
ToUTF8(). Similarly, wxWX2WCbuf can be used for the return type of wc_str().
|
Similarly, wxWX2WCbuf can be used for the return type of wxString::wc_str().
|
||||||
But, once again, none of these cryptic types is really needed if you just pass
|
But, once again, none of these cryptic types is really needed if you just pass
|
||||||
the return value of any of the functions mentioned in this section to another
|
the return value of any of the functions mentioned in this section to another
|
||||||
function directly.
|
function directly.
|
||||||
|
Reference in New Issue
Block a user