* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / I18N / CJK / ISO2022JP.cs
index fea787bfb90185ba05e192ea7ae46aefc7b18b45..4447a7963990d9a3ac574ffb3b1aeaf937c9db16 100644 (file)
@@ -1,12 +1,15 @@
+//
+// ISO2022JP.cs
+//
+// Author:
+//     Atsushi Enomoto  <atsushi@ximian.com>
+//
 using System;
 using System.Text;
 using I18N.Common;
 
 namespace I18N.CJK
 {
-       // FIXME:
-       // find out what is the difference between 50220, 50221 and 50222.
-
        [Serializable]
        public class CP50220 : ISO2022JPEncoding
        {
@@ -49,10 +52,8 @@ namespace I18N.CJK
        [Serializable]
        public class ISO2022JPEncoding : MonoEncoding
        {
-               static JISConvert convert = JISConvert.Convert;
-
                public ISO2022JPEncoding (int codePage, bool allow1ByteKana, bool allowShiftIO)
-                       : base (codePage)
+                       : base (codePage, 932)
                {
                        this.allow_1byte_kana = allow1ByteKana;
                        this.allow_shift_io = allowShiftIO;
@@ -89,6 +90,11 @@ namespace I18N.CJK
                        return new ISO2022JPEncoder (this, allow_1byte_kana, allow_shift_io).GetByteCount (chars, charIndex, charCount, true);
                }
 
+               public unsafe override int GetByteCountImpl (char* chars, int count)
+               {
+                       return new ISO2022JPEncoder (this, allow_1byte_kana, allow_shift_io).GetByteCountImpl (chars, count, true);
+               }
+
                public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount)
                {
                        return new ISO2022JPEncoder (this, allow_1byte_kana, allow_shift_io).GetBytesImpl (chars, charCount, bytes, byteCount, true);
@@ -111,14 +117,14 @@ namespace I18N.CJK
                JISX0201
        }
 
-       internal class ISO2022JPEncoder : MonoEncoding.MonoEncoder
+       internal class ISO2022JPEncoder : MonoEncoder
        {
                static JISConvert convert = JISConvert.Convert;
 
                readonly bool allow_1byte_kana, allow_shift_io;
 
                ISO2022JPMode m = ISO2022JPMode.ASCII;
-               bool shifted_in;
+               bool shifted_in_count, shifted_in_conv;
 
                public ISO2022JPEncoder (MonoEncoding owner, bool allow1ByteKana, bool allowShiftIO)
                        : base (owner)
@@ -127,9 +133,10 @@ namespace I18N.CJK
                        this.allow_shift_io = allowShiftIO;
                }
 
-               public override int GetByteCount (char [] chars, int charIndex, int charCount, bool flush)
+               public unsafe override int GetByteCountImpl (char* chars, int charCount, bool flush)
                {
-                       int end = charIndex + charCount;
+                       int charIndex = 0;
+                       int end = charCount;
                        int value;
                        int byteCount = 0;
 
@@ -144,8 +151,8 @@ namespace I18N.CJK
 
                                if (ch >= 0x2010 && ch <= 0x9FA5)
                                {
-                                       if (shifted_in) {
-                                               shifted_in = false;
+                                       if (shifted_in_count) {
+                                               shifted_in_count = false;
                                                byteCount++; // shift_out
                                        }
                                        if (m != ISO2022JPMode.JISX0208)
@@ -156,8 +163,8 @@ namespace I18N.CJK
                                        value = ((int)(convert.cjkToJis[value])) |
                                                        (((int)(convert.cjkToJis[value + 1])) << 8);
                                } else if (ch >= 0xFF01 && ch <= 0xFF60) {
-                                       if (shifted_in) {
-                                               shifted_in = false;
+                                       if (shifted_in_count) {
+                                               shifted_in_count = false;
                                                byteCount++;
                                        }
                                        if (m != ISO2022JPMode.JISX0208)
@@ -170,9 +177,9 @@ namespace I18N.CJK
                                                        (((int)(convert.extraToJis[value + 1])) << 8);
                                } else if(ch >= 0xFF60 && ch <= 0xFFA0) {
                                        if (allow_shift_io) {
-                                               if (!shifted_in) {
+                                               if (!shifted_in_count) {
                                                        byteCount++;
-                                                       shifted_in = true;
+                                                       shifted_in_count = true;
                                                }
                                        }
                                        else if (m != ISO2022JPMode.JISX0201) {
@@ -181,8 +188,8 @@ namespace I18N.CJK
                                        }
                                        value = ch - 0xFF60 + 0xA0;
                                } else if (ch < 128) {
-                                       if (shifted_in) {
-                                               shifted_in = false;
+                                       if (shifted_in_count) {
+                                               shifted_in_count = false;
                                                byteCount++;
                                        }
                                        if (m != ISO2022JPMode.ASCII)
@@ -200,8 +207,8 @@ namespace I18N.CJK
                        }
                        // must end in ASCII mode
                        if (flush) {
-                               if (shifted_in) {
-                                       shifted_in = false;
+                               if (shifted_in_count) {
+                                       shifted_in_count = false;
                                        byteCount++;
                                }
                                if (m != ISO2022JPMode.ASCII)
@@ -246,9 +253,6 @@ namespace I18N.CJK
                {
                        int charIndex = 0;
                        int byteIndex = 0;
-#if NET_2_0
-                       EncoderFallbackBuffer buffer = null;
-#endif
 
                        int start = byteIndex;
                        int end = charIndex + charCount;
@@ -266,9 +270,9 @@ namespace I18N.CJK
 
                                if (ch >= 0x2010 && ch <= 0x9FA5)
                                {
-                                       if (shifted_in) {
+                                       if (shifted_in_conv) {
                                                bytes [byteIndex++] = 0x0F;
-                                               shifted_in = false;
+                                               shifted_in_conv = false;
                                                byteCount--;
                                        }
                                        switch (m) {
@@ -283,9 +287,9 @@ namespace I18N.CJK
                                        value = ((int)(convert.cjkToJis[value])) |
                                                        (((int)(convert.cjkToJis[value + 1])) << 8);
                                } else if (ch >= 0xFF01 && ch <= 0xFF60) {
-                                       if (shifted_in) {
+                                       if (shifted_in_conv) {
                                                bytes [byteIndex++] = 0x0F;
-                                               shifted_in = false;
+                                               shifted_in_conv = false;
                                                byteCount--;
                                        }
                                        switch (m) {
@@ -306,9 +310,9 @@ namespace I18N.CJK
                                        // so here we don't have to consider it.
 
                                        if (allow_shift_io) {
-                                               if (!shifted_in) {
+                                               if (!shifted_in_conv) {
                                                        bytes [byteIndex++] = 0x0E;
-                                                       shifted_in = true;
+                                                       shifted_in_conv = true;
                                                        byteCount--;
                                                }
                                        } else {
@@ -322,9 +326,9 @@ namespace I18N.CJK
                                        }
                                        value = ch - 0xFF40;
                                } else if (ch < 128) {
-                                       if (shifted_in) {
+                                       if (shifted_in_conv) {
                                                bytes [byteIndex++] = 0x0F;
-                                               shifted_in = false;
+                                               shifted_in_conv = false;
                                                byteCount--;
                                        }
                                        SwitchMode (bytes, ref byteIndex, ref byteCount, ref m, ISO2022JPMode.ASCII);
@@ -353,9 +357,9 @@ namespace I18N.CJK
                        }
                        if (flush) {
                                // must end in ASCII mode
-                               if (shifted_in) {
+                               if (shifted_in_conv) {
                                        bytes [byteIndex++] = 0x0F;
-                                       shifted_in = false;
+                                       shifted_in_conv = false;
                                        byteCount--;
                                }
                                if (m != ISO2022JPMode.ASCII)
@@ -368,7 +372,7 @@ namespace I18N.CJK
                public override void Reset ()
                {
                        m = ISO2022JPMode.ASCII;
-                       shifted_in = false;
+                       shifted_in_conv = shifted_in_count = false;
                }
 #endif
        }
@@ -379,7 +383,7 @@ namespace I18N.CJK
 
                readonly bool allow_shift_io;
                ISO2022JPMode m = ISO2022JPMode.ASCII;
-               bool shifted_in;
+               bool shifted_in_conv, shifted_in_count;
 
                public ISO2022JPDecoder (bool allow1ByteKana, bool allowShiftIO)
                {
@@ -396,15 +400,15 @@ namespace I18N.CJK
                                if (allow_shift_io) {
                                        switch (bytes [i]) {
                                        case 0x0F:
-                                               shifted_in = false;
+                                               shifted_in_count = false;
                                                continue;
                                        case 0x0E:
-                                               shifted_in = true;
+                                               shifted_in_count = true;
                                                continue;
                                        }
                                }
                                if (bytes [i] != 0x1B) {
-                                       if (!shifted_in && m == ISO2022JPMode.JISX0208) {
+                                       if (!shifted_in_count && m == ISO2022JPMode.JISX0208) {
                                                if (i + 1 == end)
                                                        break; // incomplete head of wide char
                                                else
@@ -453,16 +457,16 @@ namespace I18N.CJK
                                if (allow_shift_io) {
                                        switch (bytes [i]) {
                                        case 0x0F:
-                                               shifted_in = false;
+                                               shifted_in_conv = false;
                                                continue;
                                        case 0x0E:
-                                               shifted_in = true;
+                                               shifted_in_conv = true;
                                                continue;
                                        }
                                }
 
                                if (bytes [i] != 0x1B) {
-                                       if (shifted_in || m == ISO2022JPMode.JISX0201) {
+                                       if (shifted_in_conv || m == ISO2022JPMode.JISX0201) {
                                                // half-kana
                                                if (bytes [i] < 0x60)
                                                        chars [charIndex++] = (char) (bytes [i] + 0xFF40);
@@ -524,7 +528,7 @@ namespace I18N.CJK
                public override void Reset ()
                {
                        m = ISO2022JPMode.ASCII;
-                       shifted_in = false;
+                       shifted_in_count = shifted_in_conv = false;
                }
 #endif
        }