Add convenience functions to Encoder and Decoder to process a string and return the...
authorMikko Rasa <tdb@tdb.fi>
Mon, 23 Mar 2009 11:12:27 +0000 (11:12 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 23 Mar 2009 11:12:27 +0000 (11:12 +0000)
Fix UTF-8 detection in Codecs::detect_codec

source/codec.cpp
source/codec.h

index 521dfea2094c3eb2c8a0cffe96eca98c54fb4c70..42315e52e9f7179540bcb5836021328f470cee2b 100644 (file)
@@ -46,6 +46,14 @@ void Codec::Encoder::encode(const ustring &str, string &buf)
                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)
@@ -70,6 +78,13 @@ void Codec::Decoder::decode(const string &str, ustring &buf)
        }
 }
 
+ustring Codec::Decoder::decode(const string &str)
+{
+       ustring buf;
+       decode(str, buf);
+       return buf;
+}
+
 UnicodeChar Codec::Decoder::error(const string &msg)
 {
        switch(err_mode)
@@ -131,13 +146,21 @@ Codec *detect_codec(const string &str)
                        else if((c&0xC0)==0xC0)
                        {
                                if(utf8_mb)
+                               {
                                        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)
index d3737400803b700a8dd325cf5283e9adb660422d..e8b172eb5af068004d6ad051642ced663a2b9608 100644 (file)
@@ -79,6 +79,8 @@ public:
                */
                virtual void encode(const ustring &str, std::string &buf);
 
+               std::string encode(const ustring &);
+
                /**
                Procuces a sequence of bytes that will bring the encoder back to the
                initial state.
@@ -143,6 +145,8 @@ public:
                */
                virtual void decode(const std::string &str, ustring &buf);
 
+               ustring decode(const std::string &);
+
                /**
                Resets the decoder to the initial state.
                */