is the concept of codecvt_t::partial broken ?

Oncaphillis oncaphillis@snafu.de
Wed Sep 26 10:18:00 GMT 2012


Hi,
I'm writing yet another char encoding translator with the help of codecvt< >
and stumbled across the following problem. When I try to interprese a 
byte sequence
as ShiftJis I never get the result code codcvt_t::partial even if I feed 
a single
0xe0 as the only input which the iconv should interprete as an 
incomplete two byte
sequence. The same seems to hold true with other multi char encodings as 
well

Here's my code: "PARTIAL" never gets printed:

<snip>

         bool  ToWString(const std::string & str,std::wstring & ws,const 
std::string & rEnc="ja_JP.sjis")
         {
             typedef std::codecvt< wchar_t, char, std::mbstate_t > 
codecvt_t;

             std::locale loc(rEnc.c_str());

             const codecvt_t & fc = std::use_facet<codecvt_t>( loc );

             ws.clear();
             codecvt_t::result r=codecvt_t::ok;

             std::mbstate_t st=mbstate_t();

             const char *ca = str.c_str();
             const char *co = ca+str.length();
             const char *cn = ca;

             for(int i=0;i<str.length();i++)
             {
                 std::cerr << "0x" << std::hex << (int)str[i] << 
std::dec << " ";
             }
             std::cerr << std::endl;

             while(co!=cn)
             {
                 static const int l = 10;
                 static wchar_t wo[l];
                 wchar_t *wn=NULL;
                 if( (r=fc.in(st,ca,ca+1,cn,wo,wo+l,wn))==codecvt_t::error)
                 {
                     break;
                 }

                 if(r==codecvt_t::partial)
                 {
                     std::cerr << "PARTIAL" << std::endl;
                 }

                 ws+=std::wstring(wo,wn-wo);
                 ca=cn;
             }
             return (r==codecvt_t::ok || r==codecvt_t::noconv);
         }

<snip>



More information about the Libstdc++ mailing list