std::string and unicode

Milan Babuskov milanb at panonnet.net
Thu Apr 10 02:21:58 PDT 2008


Vadim Zeitlin wrote:
> 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,

-- 
Milan Babuskov
http://www.flamerobin.org


More information about the wx-users mailing list