Vadim Zeitlin
unread,Jun 28, 2012, 7:38:47 AM6/28/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to wx-...@googlegroups.com
On Thu, 28 Jun 2012 04:11:40 +0300 Martin Papik wrote:
MP> Common to all code:
MP> #define wxUSE_UNICODE 1
MP> char * str; str=strdup("test");
MP> wxString s;
MP>
MP> ### 1
MP> s.FromUTF8(str);
MP> s.FromUTF8(str,strlen(str));
MP> s.FromAscii(str);
MP> In all cases s will remain unchanged. Whatever string was there will remain
MP> untouched.
MP> I would expect it to contain the unicode representation of test. Behaviour
MP> exists both on wx-2.8.12 and wx-2.9.3.
MP> Is this a bug or am I severely misreading the documentation?
You are. Both FromAscii() and FromUTF8() are static functions returning
a new wxString, it doesn't make sense to call them like above.
MP> ### 2
MP> s = wxT("test");
MP> printf ("%s\n",s.c_str());
MP> on wx-2.8.12 gcc says: warning: format ‘%s’ expects argument of type
MP> ‘char*’, but argument 2 has type ‘const wxChar* {aka const wchar_t*}’
MP> [-Wformat]
MP> on wx-2.9.3 gcc says: warning: format ‘%s’ expects argument of type
MP> ‘char*’, but argument 2 has type ‘const wxChar* {aka const wchar_t*}’
MP> [-Wformat]
MP> +++ warning: format ‘%s’ expects argument of type ‘char*’, but
MP> argument 2 has type ‘const wxChar* {aka const wchar_t*}’ [-Wformat]
Use either
printf("%s\n", static_cast<const char*>(s.mb_str()));
or, preferred,
wxPrintf("%s\n", s);
This is discussed in docs/changes.txt, search for "wxString::c_str()".
MP> ### 3
MP> s = wxT("test");
MP> printf ("%s\n",(char*)s.c_str());
MP> on wx-2.8.12 works, but I would argue type casting shouldn't be needed.
It doesn't work. In Unicode build of 2.8 c_str() returns a wchar_t*
pointer, casting it to char* may make it compile but it definitely doesn't
make it work as expected.
MP> on wx-2.9.3 gcc says: error: invalid cast from type ‘wxCStrData’ to type
MP> ‘char*’ same if I cast it to (const char *)
No, not same. Casting to "char*" doesn't work, casting to "const char*"
does work, please check again.
MP> I didn't find any details about wxCStrData in the documentation for
MP> wx-2.9.3.
MP>
MP> ### 4
MP> s.Printf(wxT("%s"),str);
In 2.8 you can only pass wchar_t strings to Printf("%s") in Unicode build.
MP> on wx-2.9.3 the text is copied correctly
MP> s.Printf(wxT("%S"),str);
MP> on wx-2.8.12 the text is copied correctly
MP> on wx-2.9.3 I get an assertion --> src/common/strvararg.cpp(646): assert "n
MP> <= parser.nargs" failed in DoGetArgumentType(): more arguments than format
MP> string specifiers?
Don't use non-portable "%S" with wxPrintf() and similar functions.
MP> I would expect %s to be a char * and %S to be a wchar *
This is not how it works. In 2.9, "%s" can be either char* or wchar_t*
when using wx functions and "%S" is not supported.
MP> IMHO explicit casting should be avoided because it inhibits the compiler's
MP> ability to warn about errors.
Casting is only needed when using vararg functions which don't do any
error checking in the first place. The simplest solution is to use
wxPrintf() and such instead (they look like vararg functions but actually
they are pseudo-vararg templates and are type-safe). Another possibility is
to not use vararg functions at all.
In any case, definitely avoid printf() and other vararg functions in new
code using wxString, they can be used correctly with it but it has never
been convenient to use printf() with objects and still isn't. Using
wxPrintf() OTOH is very nice and you don't need neither the casts nor even
.c_str().
Regards,
VZ