]> git.tdb.fi Git - libs/core.git/blob - source/stringcodec/jisx0208.cpp
Use format instead of stringstream in Regex
[libs/core.git] / source / stringcodec / jisx0208.cpp
1 #include "jisx0208.h"
2
3 #include "jisx0208.table"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace StringCodec {
9
10 void JisX0208::Encoder::encode_char(unichar ucs, string &buf)
11 {
12         unsigned short jis = ucs_to_jisx0208(ucs);
13         if(jis)
14         {
15                 char jbuf[2] = {jis>>8, jis};
16                 buf.append(jbuf, 2);
17         }
18         else
19                 error(ucs, buf, invalid_character(ucs, "JIS X 0208"));
20 }
21
22 void JisX0208::Encoder::transliterate(unichar, string &buf)
23 {
24         buf.append("!)", 2);
25 }
26
27
28 unichar JisX0208::Decoder::decode_char(const string &str, string::const_iterator &i)
29 {
30         if(i==str.end())
31                 return -1;
32
33         string::const_iterator j = i;
34         Kuten jis;
35         jis.ku = *j++-0x20;
36
37         unichar result;
38         if(j==str.end())
39                 result = error(invalid_sequence(i, j, "incomplete JIS X 0208 character"));
40         else
41         {
42                 jis.ten = *j++-0x20;
43                 result = jisx0208_to_ucs(jis);
44                 if(result==-1)
45                         result = error(invalid_sequence(i, j, "invalid JIS X 0208 ku-ten"));
46                 if(result==0)
47                         result = error(invalid_sequence(i, j, "undefined JIS X 0208 character"));
48         }
49
50         i = j;
51         return result;
52 }
53
54
55 unichar jisx0208_to_ucs(Kuten jis)
56 {
57         if(jis.ku==0 || jis.ku>0x5E || jis.ten==0 || jis.ten>0x5E)
58                 return -1;
59
60         return jisx0208_to_ucs_table[jis.ku*94 + jis.ten - 95];
61 }
62
63 Kuten ucs_to_jisx0208(unichar c)
64 {
65         if(c<0 || c>0xFFFF)
66                 return Kuten();
67
68         unsigned i = 0;
69         for(unsigned bit=0x1000; bit; bit>>=1)
70         {
71                 if(i+bit>=ucs_to_jisx0208_table_size)
72                         continue;
73                 if(ucs_to_jisx0208_table[i+bit].ucs<=static_cast<unsigned short>(c))
74                         i += bit;
75         }
76
77         Kuten result;
78         if(ucs_to_jisx0208_table[i].ucs==static_cast<unsigned short>(c))
79         {
80                 result.ku = (ucs_to_jisx0208_table[i].jis>>8)+1;
81                 result.ten = ucs_to_jisx0208_table[i].jis+1;
82         }
83
84         return result;
85 }
86
87 } // namespace StringCodec
88 } // namespace Msp