std::string is simply a typedef for a specialisation of the class
template std::basic_string, instantiated for the char type. Similarly
wstring is the same template class instantiated for wchar_t.
The wide char type would, I guess help with some aspects of unicode -
but it is not designed to support unicode as such.
HTH
Alec
In message <mailman.27.1207662...@lists.wxwidgets.org>,
Declan McMullen <declan....@gmail.com> writes
>Hi Guys,
>I'm slightly confused about something. Does std::string not support
>unicode ?
>I have stl enabled in my wxwidgets compilation. The reason I did it is
>because
>I have some backend classes that use std::string and I didnt want to be
>converting to
>and from std::string to wxString. Turning on stl meant my strings could be
>interchangeable.
>
>However I want to work with locales so I went to use the unicode build of
>wx widgets, this
>however fails to allow me to have interchangeable strings.
>
>So if I want to use unicode will I have to use wxString and perform a
>conversion?
>
>I've been looking through the internationalization section of the book but
>i've ended up confused :)
>
>Any guidance much appreciated.
--
Alec Ross
Hi Guys,
I'm slightly confused about something. Does std::string not support unicode ?
I have stl enabled in my wxwidgets compilation. The reason I did it is because
I have some backend classes that use std::string and I didnt want to be converting to
and from std::string to wxString. Turning on stl meant my strings could be interchangeable.
However I want to work with locales so I went to use the unicode build of wx widgets, this
however fails to allow me to have interchangeable strings.
So if I want to use unicode will I have to use wxString and perform a conversion?
I've been looking through the internationalization section of the book but i've ended up confused :)
Any guidance much appreciated.
--
http://www.computing.dcu.ie/~dmcmullen
declan....@computing.dcu.ie
School of Computing
Postgrad Bay A _______________________________________________
wx-users mailing list
wx-u...@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wx-users
DM> This guy actually wrote two nice little functions
DM> http://www.kangmaman.com/node/131
I think you could make it even more complicated but I must admit that at a
first glance I don't see how. I'd also strongly recommend deciding upon
your std::string encoding instead of blindly trying UTF-8 first and falling
back to ASCII later (and what happens if it's in Latin-1? or koi8-r?) --
and it also would be better if conversion in both directions were symmetric
which is not the case here. And I won't even mention nice pessimizations
like not passing strings via references and especially creating a temporary
object on a heap instead of just using wxConvUTF8...
In short, don't use the code above.
Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
_______________________________________________
wx-users mailing list
wx-u...@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wx-users
> string Utility::wx2std(wxString s)
> {
> char * newString = NULL;
> int length = s.Length();
> newString = new char[length];
> return strcpy( newString, (const char*)s.mb_str(wxConvUTF8) );
>
> }
>
memory leak
std::string Utility::wx2std(wxString s)
{
return std::string(s.mb_str(wxConvUTF8), s.Length());
}
--
malcom
http://malcom.pl
We are using the following in FlameRobin:
//-----------------------------------------------------------------------------
std::string wx2std(const wxString& input, wxMBConv* conv = 0)
{
if (input.empty())
return "";
if (!conv)
conv = wxConvCurrent;
const wxWX2MBbuf buf(input.mb_str(*conv));
// conversion may fail and return 0,
// which isn't a safe value to pass
// to std:string constructor
if (!buf)
return "";
return std::string(buf);
}
//-----------------------------------------------------------------------------
wxString std2wx(const std::string& input, wxMBConv* conv = 0)
{
if (input.empty())
return wxEmptyString;
if (!conv)
conv = wxConvCurrent;
return wxString(input.c_str(), *conv);
}
//-----------------------------------------------------------------------------
Any comments are appreciated.
--
Milan Babuskov
http://www.flamerobin.org
wxString::Length() returns the number characters in the
unconverted string, not the number of bytes so this will
only work for ASCII characters. Why don't you simply use:
wxString s1 = "test";
std::string s2 = s1.utf8_str();
// or
std::wstring s2 = s1.wc_str();
or is there something wrong with this?
Robert
MB> Declan McMullen wrote:
MB> > Any tips on how I should do it ?
MB>
MB> We are using the following in FlameRobin:
MB>
MB> //-----------------------------------------------------------------------------
MB> std::string wx2std(const wxString& input, wxMBConv* conv = 0)
MB> {
MB> if (input.empty())
MB> return "";
MB> if (!conv)
MB> conv = wxConvCurrent;
MB> const wxWX2MBbuf buf(input.mb_str(*conv));
MB> // conversion may fail and return 0,
MB> // which isn't a safe value to pass
MB> // to std:string constructor
MB> if (!buf)
MB> return "";
MB> return std::string(buf);
MB> }
MB> //-----------------------------------------------------------------------------
MB> wxString std2wx(const std::string& input, wxMBConv* conv = 0)
MB> {
MB> if (input.empty())
MB> return wxEmptyString;
MB> if (!conv)
MB> conv = wxConvCurrent;
MB> return wxString(input.c_str(), *conv);
MB> }
MB> //-----------------------------------------------------------------------------
MB>
MB> Any comments are appreciated.
This code is correct although I'm not sure why do you think it's necessary
to test for empty string explicitly, normally you should be able to remove
the checks for input.empty() without any ill effects.
We used to have some more complex code that allocated buffers and such,
so if string is empty it seemed efficient to not do all that. I guess it
could be removed now.
Thanks,
Declan McMullen wrote:
Any tips on how I should do it ?
We are using the following in FlameRobin:
//-----------------------------------------------------------------------------
std::string wx2std(const wxString& input, wxMBConv* conv = 0)
{
if (input.empty())
return "";
if (!conv)
conv = wxConvCurrent;
const wxWX2MBbuf buf(input.mb_str(*conv));
// conversion may fail and return 0,
// which isn't a safe value to pass
// to std:string constructor
if (!buf)
return "";
return std::string(buf);
}
//-----------------------------------------------------------------------------
wxString std2wx(const std::string& input, wxMBConv* conv = 0)
{
if (input.empty())
return wxEmptyString;
if (!conv)
conv = wxConvCurrent;
return wxString(input.c_str(), *conv);
}
//-----------------------------------------------------------------------------
Any comments are appreciated.
--
Milan Babuskov
http://www.flamerobin.org
_______________________________________________
wx-users mailing list
wx-u...@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wx-users
> Could you tell me what the second parameter is doing?
For calls where std::strings in the database character set need to be
converted to the system character set we pass the proper conversion object.
> And should I be passing a value to it or leave it defaulting to 0 ?
You can omit it if you only ever go from wxString to std::string and
back in the current system encoding.
HTH
--
Michael Hieke
_______________________________________________
wx-users mailing list
wx-u...@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wx-users