Boost logo

Boost :

From: Michel André (michel.andre_at_[hidden])
Date: 2002-10-19 06:51:34


"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? And I still think the functions belong in an string algorithm
library.

Regards
/Michel


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