bool Codec::detect(const string &str) const
{
- Decoder *dec=create_decoder();
- bool result=true;
+ Decoder *dec = create_decoder();
+ bool result = true;
try
{
for(string::const_iterator i=str.begin(); i!=str.end(); )
}
catch(const CodecError &)
{
- result=false;
+ result = false;
}
delete dec;
encode_char(*i, buf);
}
+string Codec::Encoder::encode(const ustring &str)
+{
+ string buf;
+ encode(str, buf);
+ sync(buf);
+ return buf;
+}
+
void Codec::Encoder::error(UnicodeChar ch, string &buf, const string &msg)
{
switch(err_mode)
{
for(string::const_iterator i=str.begin(); i!=str.end();)
{
- UnicodeChar c=decode_char(str, i);
+ UnicodeChar c = decode_char(str, i);
if(c!=-1)
- buf+=c;
+ buf += c;
}
}
+ustring Codec::Decoder::decode(const string &str)
+{
+ ustring buf;
+ decode(str, buf);
+ return buf;
+}
+
UnicodeChar Codec::Decoder::error(const string &msg)
{
switch(err_mode)
for(string::const_iterator i=n.begin(); i!=n.end(); ++i)
{
if(isupper(*i))
- name+=tolower(*i);
+ name += tolower(*i);
else if(islower(*i) || isdigit(*i))
- name+=*i;
+ name += *i;
}
if(name=="ascii") return new Ascii;
Codec *detect_codec(const string &str)
{
- bool is_utf8=true;
- bool is_ascii=true;
- bool is_latin1=true;
- unsigned utf8_mb=0;
+ bool is_utf8 = true;
+ bool is_ascii = true;
+ bool is_latin1 = true;
+ unsigned utf8_mb = 0;
for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
{
- unsigned char c=*i;
+ unsigned char c = *i;
if(c&0x80)
{
- is_ascii=false;
+ is_ascii = false;
if((c&0xC0)==0x80)
{
if((c&0xE0)==0x80)
- is_latin1=false;
+ is_latin1 = false;
if(utf8_mb)
--utf8_mb;
else
- is_utf8=false;
+ is_utf8 = false;
}
else if((c&0xC0)==0xC0)
{
if(utf8_mb)
- is_utf8=false;
+ {
+ is_utf8 = false;
+ utf8_mb = 0;
+ }
else
{
for(utf8_mb=1; (c>>(6-utf8_mb))&1; ++utf8_mb) ;
}
}
}
+ else if(utf8_mb)
+ {
+ is_utf8 = false;
+ utf8_mb = 0;
+ }
}
if(is_ascii)