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