🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1775

Re: basic_string conversions

#1775
From: Nathan Myers
Date: Mon, 21 Aug 1995 07:37
69 lines
2924 bytes
In article <ncmDDJ83M.Dvu@netcom.com>, Nathan Myers <ncm@netcom.com> wrote:
>In article <MATT.95Aug17235138@physics2.berkeley.edu>,
>Steve F. Cipolli <dscope!sfc@uu.psi.com> wrote:
>> What is the recommended method of converting basic_string's of one
>> parameterized type to another, and from another parameterized type itself?

>Encodings for conversion between wide and narrow strings differ,
>and you should say which encoding you mean.
>
>Unfortunately the C functions stop when they hit a NUL character.
>
>The correct mechanism to use is the locale facilities:

I wrote a bad code example, here.  Compare to the much
better code following.

>  std::wstring
>  widen(const std::string& s, const std::locale& loc = std::locale())
>  {
>    using namespace std;
>    const char* from = s.data();
>    const char* from_end = from + s.length();
>    char* to = new wchar_t[s.length()];  // should be big enough.
>    char* to_end = to + s.length();
>    try {
>      typedef codecvt<char,wchar_t,mbstate_t> Cvt;
>      if (use_facet<Cvt>(loc).convert(
>            from, from_end, from,
>	    to,   to_end,   to_end, mbstate_t) != codecvt_base::ok)
>	throw runtime_error();
>    } catch (...) {
>      delete [] to;
>      throw;
>    }
>    wstring w(to, to_end);
>    delete [] to;
>    return w;
>  }

I'm kind of embarrassed... To use the language and the standard
library effectively, the code above should have been:

std::wstring
widen(const std::string& s, const std::locale& loc = std::locale())
{
using namespace std;
vector<wchar_t> to(s.length());
const char* from_end;
const wchar_t* to_end;
if (use_facet< codecvt<char,wchar_t,mbstate_t> >(loc).convert(
s.data(), s.data()+s.length(), from_end,
to.begin(), to.end(), to_end, mbstate_t()) != codecvt_base::ok)
throw runtime_error();
return wstring(to, to_end);
}

This is lots shorter and cleaner, and the difference illustrates
a very general principle in designing with exceptions.

>[In case anybody didn't notice, the above code is written to
> work when Standard implementations are available.  Until then,
> you'll need ifdefs, at least.]

Nathan Myers
myersn@roguewave.com
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <ncmDDnGr2.K0E@netcom.com>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <MATT.95Aug17235138@physics2.berkeley.edu> <ncmDDJ83M.Dvu@netcom.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 21 Aug 95 07:37:11 GMT
Approved: fjh@cs.mu.oz.au
Return-Path: <daemon@ncar.UCAR.EDU>
Newsgroups: comp.std.c++
From: ncm@netcom.com (Nathan Myers)
Subject: Re: basic_string conversions
Message-ID: <ncmDDnGr2.K0E@netcom.com>
Cc: ncm@netcom.com
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <MATT.95Aug17235138@physics2.berkeley.edu> <ncmDDJ83M.Dvu@netcom.com>
X-Original-Date: Mon, 21 Aug 1995 07:28:13 GMT