X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstringcodec%2Fcodec.cpp;h=abec134b1a0fd00a25f936952368beba22c62742;hp=865d9c7de1cca44e64e64c0dccab8ad49c5a8c0b;hb=HEAD;hpb=017feade2799ddbecad62b9a7911bf4d3e229dad diff --git a/source/stringcodec/codec.cpp b/source/stringcodec/codec.cpp index 865d9c7..ff2bd63 100644 --- a/source/stringcodec/codec.cpp +++ b/source/stringcodec/codec.cpp @@ -6,6 +6,7 @@ #include "iso885915.h" #include "jisx0201.h" #include "jisx0208.h" +#include "utf16.h" #include "utf8.h" #include "windows1252.h" @@ -19,7 +20,7 @@ bool Codec::detect(const string &str) const Decoder *dec = create_decoder(IGNORE_ERRORS); bool result = true; - for(string::const_iterator i=str.begin(); (result && i!=str.end()); ) + for(auto i=str.begin(); (result && i!=str.end()); ) result = (dec->decode_char(str, i)!=-1); delete dec; @@ -29,8 +30,8 @@ bool Codec::detect(const string &str) const void Codec::Encoder::encode(const ustring &str, string &buf) { - for(ustring::const_iterator i=str.begin(); i!=str.end(); ++i) - encode_char(*i, buf); + for(unichar c: str) + encode_char(c, buf); } string Codec::Encoder::encode(const ustring &str) @@ -45,7 +46,7 @@ string Codec::Encoder::encode(const ustring &str) void Codec::Decoder::decode(const string &str, ustring &buf) { - for(string::const_iterator i=str.begin(); i!=str.end();) + for(auto i=str.begin(); i!=str.end();) { unichar c = decode_char(str, i); if(c!=-1) @@ -85,7 +86,7 @@ Codec *create_codec(const string &n) else if(em_str=="trans" || em_str=="transliterate") em = TRANSLITERATE; else - throw invalid_argument("invalid error mode"); + throw invalid_argument("StringCodec::create_codec"); } if(name=="ascii") return new Ascii(em); @@ -96,8 +97,11 @@ Codec *create_codec(const string &n) if(name=="jisx0201") return new JisX0201(em); if(name=="jisx0208") return new JisX0208(em); if(name=="utf8") return new Utf8(em); + if(name=="utf16") return new Utf16(em, Utf16::AUTO); + if(name=="utf16be") return new Utf16(em, Utf16::BIG); + if(name=="utf16le") return new Utf16(em, Utf16::LITTLE); if(name=="windows1252" || name=="cp1252") return new Windows1252(em); - throw invalid_argument("unknown string codec"); + throw invalid_argument("StringCodec::create_codec"); } Codec *detect_codec(const string &str) @@ -107,9 +111,8 @@ Codec *detect_codec(const string &str) bool is_latin1 = true; unsigned utf8_mb = 0; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(char c: str) { - unsigned char c = *i; if(c&0x80) { is_ascii = false;