This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix initialization of _M_grouping


Hi all,

My apologies for the delay in replying.  I claim a 3.74 kg (8lb 4oz) 
EXCUSE, as of 5:07 this morning.  His mother begs your indulgence.

On Tue, Jan 29, 2002 at 10:38:41AM -0800, Benjamin Kosnik wrote:
> I'm trying to figure out if this is what Nathan is talking about:
> 
> #include <locale>
> #include <sstream>
> 
> struct testpunct : std::numpunct<char> 
> {
>   //  char do_thousands_sep() const { return '0'; }
>   char do_thousands_sep() const { return '\0'; }
>   std::string do_grouping() const { return std::string("\004"); }
> };

Oops, "\002", I suppose.  (I suppose that propagates a typo from my
own previous posting.)
 
> int main()
> {
>   // "1\00023\00045"
>   // "1,0023,0045"
>   const std::string str("1\00023\00045", 7);
>   std::stringstream ss(str);

Hmm, don't you have to rewind the stringstream before you can read 
from it?  ISTR a NAD on the subject.

>   const std::locale loc(std::locale::classic(), new testpunct); 
>   ss.imbue(loc);
> 
>   long l;
>   ss >> l;
>   assert(l == 1023045);
>   return 0;
> }

I don't think I understand your example properly.  
So, just to be clear, this is what I think should pass:

#include <locale>
#include <sstream>
#include <streambuf>

struct testpunct : std::numpunct<char> 
{
  //  char do_thousands_sep() const { return '0'; }
  char do_thousands_sep() const { return '\0'; }
  std::string do_grouping() const { return std::string("\002"); }
};

int main()
{
  // "1\00023\00045"
  char s[] = { '1', '\0', '2', '3', '\0', '4', '5' };
  const std::string str(s, 7);
  std::stringstream ss(str);
  ss.rdbuf()->pubseekoff(0, ss.beg);

  const std::locale loc(std::locale::classic(), new testpunct); 
  ss.imbue(loc);

  long l;
  ss >> l;
  assert(l == 12345);
  return 0;
}

In other words, in C++ the NUL character has no more significance than 
any other non-whitespace control character, except where C strings are 
presented in an interface.  Any internal operations that treat NUL
differently result in bugs.

Nathan Myers
ncm at cantrip dot org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]