]> git.tdb.fi Git - libs/core.git/blobdiff - source/iso646fi.cpp
Add a function to perform simple character mapping
[libs/core.git] / source / iso646fi.cpp
index cfdae24301a2bb886f31745669c404c8ce5734cd..779eadf0d887fa89b30b60b8e33a3669d31cf821 100644 (file)
@@ -5,37 +5,40 @@ Copyright © 2006-2007 Mikko Rasa
 Distributed under the LGPL
 */
 
+#include "codecutils.h"
 #include "iso646fi.h"
 
 using namespace std;
 
+namespace {
+
+const unsigned map_size=9;
+
+const int mapping[map_size*2]=
+{
+       0xC4, 0x5B,
+       0xC5, 0x5D,
+       0xD6, 0x5C,
+       0xDC, 0x5E,
+       0xE4, 0x7B,
+       0xE5, 0x7D,
+       0xE9, 0x60,
+       0xF6, 0x7C,
+       0xFC, 0x7E
+};
+
+} // namespace
+
 namespace Msp {
 namespace Codecs {
 
 void Iso646Fi::Encoder::encode_char(UnicodeChar ch, string &buf)
 {
-       if((ch>=0 && ch<=0x5A) || ch==0x5F || (ch>=0x61 && ch<=0x7A))
-               buf+=ch;
-       else if(ch==0xC4)
-               buf+=0x5B;
-       else if(ch==0xC5)
-               buf+=0x5D;
-       else if(ch==0xD6)
-               buf+=0x5C;
-       else if(ch==0xDC)
-               buf+=0x5E;
-       else if(ch==0xE4)
-               buf+=0x7B;
-       else if(ch==0xE5)
-               buf+=0x7D;
-       else if(ch==0xE9)
-               buf+=0x60;
-       else if(ch==0xF6)
-               buf+=0x7C;
-       else if(ch==0xFC)
-               buf+=0x7E;
-       else
+       int tch=transform_mapping_or_direct(mapping, map_size, ch, false);
+       if(tch<0 || tch>0x7F)
                error(ch, buf, "Can't express character in ISO-646-FI");
+       else
+               buf+=tch;
 }
 
 void Iso646Fi::Encoder::transliterate(UnicodeChar, string &buf)
@@ -50,29 +53,13 @@ UnicodeChar Iso646Fi::Decoder::decode_char(const string &str, string::const_iter
                return error("No input");
 
        unsigned char ch=*i;
-       UnicodeChar result=-1;
-       if(ch==0x5B)
-               result=0xC4;
-       else if(ch==0x5C)
-               result=0xD6;
-       else if(ch==0x5D)
-               result=0xC5;
-       else if(ch==0x5E)
-               result=0xDC;
-       else if(ch==0x60)
-               result=0xE9;
-       else if(ch==0x7B)
-               result=0xE4;
-       else if(ch==0x7C)
-               result=0xF6;
-       else if(ch==0x7D)
-               result=0xE5;
-       else if(ch==0x7E)
-               result=0xFC;
-       else if(ch<=0x7F)
-               result=ch;
-       else
+       int tch=(ch<=0x7F ? transform_mapping_or_direct(mapping, map_size, ch, true) : -1);
+
+       UnicodeChar result;
+       if(tch==-1)
                result=error("Undefined ISO-646-FI character");
+       else
+               result=tch;
 
        ++i;
        return result;