Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2002-10-19 16:25:26


>From: "Michel André" <michel.andre_at_[hidden]>

> "Mattias Flodin" <flodin_at_[hidden]> wrote in message
> news:20021019075857.GA12452_at_krona.cs.umu.se...
> >> /// convert a wstring to a string
> >> std::string to_string(const std::wstring& str);
> >> std::string to_string(const wchar_t* str);
> >>
> >> /// convert a string to a wstring
> >> std::string to_wstring(const std::string& str);
> >> std::string to_wstring(const char* str);
>
> >These are very useful, but may be hard to make portable. But I guess you
> >could claim that as long as it works on all platforms that have a sense
> >of wide-character strings, it is portable.
>
> Isn't wchar_t and std::wstring a part of the standard? I haven't got the
> text at hand right now but I think they are a part of the standard even
> without looking.
>
> The code looks something like this from the top of my head (I havent go
the
> actual code availble):
>
> std::wstring to_wstring(const std::string& source)
> {
> std::wstring result(source.size(), char(0));
> typedef std::ctype<wchar_t> ctype_t;
> const ctype_t& ct = std::use_facet<ctype_t>(std::locale());
> ct.widen(source.data(), source.data() + source.size(),
> &(*result.begin()));
> return result;
> }
>
> std::string to_string(const std::wstring& source)
> {
> std::string result(source.size(), wchar_t(0));
> typedef std::ctype<wchar_t> ctype_t;
> const ctype_t& ct = std::use_facet<ctype_t>(std::locale());
> ct.narrow(source.data(), source.data() + source.size(),'@',
> &(*result.begin()));
> return result;
> }
>
> >In any case, shouldn't they be part of the operation of lexical_cast?.
> >I.e. lexical_cast<wstring>(string()) would perform the same function as
> >your to_wstring would.
>
> Yes of course lexical cast should be able to perform the function as well.
> But that would require a newer more configurable lexical_cast with
> specializations/traits/policies for specific conversion. But thats
underway
> isn't it?

Yes. The proposal here
(http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/) can be
extended by the user to handle any user defined conversion, via
specialisation, or partial specialisation.

Using the code above:

#include <boost/lexical_cast.hpp>

namespace boost {
namespace detail {

  template<>
  struct lexical_cast_impl<std::string,std::wstring>
  {
    static std::string do_cast(const std::wstring &source)
    {
     std::string result(source.size(), wchar_t(0));
     typedef std::ctype<wchar_t> ctype_t;
     const ctype_t& ct = std::use_facet<ctype_t>(std::locale());
     ct.narrow(source.data(), source.data() +
source.size(),'@',&(*result.begin()));
     return result;
    }
  };

  template<>
  struct lexical_cast_impl<std::wstring,std::string>
  {
    static std::wstring do_cast(const std::string &source)
    {
      std::wstring result(source.size(), char(0));
      typedef std::ctype<wchar_t> ctype_t;
      const ctype_t& ct = std::use_facet<ctype_t>(std::locale());
      ct.widen(source.data(), source.data() +
source.size(),&(*result.begin()));
      return result;
    }
  };

}
}

int main()
{
std::string str=boost::lexical_cast<std::string>(std::wstring(L"Test"));
std::wstring wstr=boost::lexical_cast<std::wstring>(std::string("Test"));

std::cout << str << '\n';
std::wcout << wstr << '\n';
}

Regards,

Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk