Merge pull request #2475 from sandyarmstrong/getcultureinfo-37848
[mono.git] / mcs / class / I18N / CJK / CP949.cs
index 144322a63a220418f723c091e569b49621a4affc..0b0ab4975a3b5615a594c35eed5bc4e139957a3d 100644 (file)
@@ -105,32 +105,60 @@ namespace I18N.CJK
     internal class KoreanEncoding : DbcsEncoding
     {
         // Constructor.
-        public KoreanEncoding (int codepage, bool useUHC) : base (codepage) {
+        public KoreanEncoding (int codepage, bool useUHC)
+            : base (codepage, 949) {
             this.useUHC = useUHC;
         }
 
         internal override DbcsConvert GetConvert ()
         {
-                return KSConvert.Convert;
+                return DbcsConvert.KS;
         }
 
         bool useUHC;
 
+#if !DISABLE_UNSAFE
+        // Get the bytes that result from encoding a character buffer.
+        public unsafe override int GetByteCountImpl (char* chars, int count)
+        {
+            int index = 0;
+            int length = 0;
+                       int end = count;
+            DbcsConvert convert = GetConvert ();
+
+            // 00 00 - FF FF
+            for (int i = 0; i < end; i++, charCount--) {
+                char c = chars[i];
+                if (c <= 0x80 || c == 0xFF) { // ASCII
+                    length++;
+                    continue;
+                }
+                byte b1 = convert.u2n[((int)c) * 2];
+                byte b2 = convert.u2n[((int)c) * 2 + 1];
+                if (b1 == 0 && b2 == 0) {
+                    // FIXME: handle fallback for GetByteCountImpl().
+                    length++;
+                }
+                else
+                    length += 2;
+            }
+            return length;
+        }
+
         // Get the bytes that result from encoding a character buffer.
         public unsafe override int GetBytesImpl (char* chars, int charCount,
                          byte* bytes, int byteCount)
         {
             int charIndex = 0;
             int byteIndex = 0;
+                       int end = charCount;
             DbcsConvert convert = GetConvert ();
-#if NET_2_0
             EncoderFallbackBuffer buffer = null;
-#endif
 
             // 00 00 - FF FF
             int origIndex = byteIndex;
-            while (charCount-- > 0) {
-                char c = chars[charIndex++];
+            for (int = charIndex; i < end; i++, charCount--) {
+                char c = chars[i];
                 if (c <= 0x80 || c == 0xFF) { // ASCII
                     bytes[byteIndex++] = (byte)c;
                     continue;
@@ -138,12 +166,8 @@ namespace I18N.CJK
                 byte b1 = convert.u2n[((int)c) * 2];
                 byte b2 = convert.u2n[((int)c) * 2 + 1];
                 if (b1 == 0 && b2 == 0) {
-#if NET_2_0
-                    HandleFallback (ref buffer, chars, ref charIndex, ref charCount,
-                        bytes, ref byteIndex, ref byteCount);
-#else
-                    bytes[byteIndex++] = (byte)'?';
-#endif
+                    HandleFallback (ref buffer, chars, ref i, ref charCount,
+                        bytes, ref byteIndex, ref byteCount, null);
                 } else {
                     bytes[byteIndex++] = b1;
                     bytes[byteIndex++] = b2;
@@ -151,8 +175,71 @@ namespace I18N.CJK
             }
             return byteIndex - origIndex;
         }
-
-        // Get the characters that result from decoding a byte buffer.
+#else
+               // Get the bytes that result from encoding a character buffer.
+               public override int GetByteCount(char[] chars, int index, int count)
+               {
+                       int length = 0;
+                       DbcsConvert convert = GetConvert();
+
+                       // 00 00 - FF FF
+                       while (count-- > 0)
+                       {
+                               char c = chars[index++];
+                               if (c <= 0x80 || c == 0xFF)
+                               { // ASCII
+                                       length++;
+                                       continue;
+                               }
+                               byte b1 = convert.u2n[((int)c) * 2];
+                               byte b2 = convert.u2n[((int)c) * 2 + 1];
+                               if (b1 == 0 && b2 == 0)
+                               {
+                                       // FIXME: handle fallback for GetByteCountImpl().
+                                       length++;
+                               }
+                               else
+                                       length += 2;
+                       }
+                       return length;
+               }
+
+               // Get the bytes that result from encoding a character buffer.
+               public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
+               {
+                       int byteCount = bytes.Length;
+                       int end = charIndex + charCount;
+
+                       DbcsConvert convert = GetConvert();
+                       EncoderFallbackBuffer buffer = null;
+
+                       // 00 00 - FF FF
+                       int origIndex = byteIndex;
+                       for (int i = charIndex; i < end; i++, charCount--)
+                       {
+                               char c = chars[i];
+                               if (c <= 0x80 || c == 0xFF)
+                               { // ASCII
+                                       bytes[byteIndex++] = (byte)c;
+                                       continue;
+                               }
+                               byte b1 = convert.u2n[((int)c) * 2];
+                               byte b2 = convert.u2n[((int)c) * 2 + 1];
+                               if (b1 == 0 && b2 == 0)
+                               {
+                                       HandleFallback (ref buffer, chars, ref i, ref charCount,
+                                               bytes, ref byteIndex, ref byteCount, null);
+                               }
+                               else
+                               {
+                                       bytes[byteIndex++] = b1;
+                                       bytes[byteIndex++] = b2;
+                               }
+                       }
+                       return byteIndex - origIndex;
+               }
+#endif
+               // Get the characters that result from decoding a byte buffer.
         public override int GetCharCount (byte[] bytes, int index, int count)
         {
             return GetDecoder ().GetCharCount (bytes, index, count);
@@ -188,9 +275,7 @@ namespace I18N.CJK
                 return GetCharCount (bytes, index, count, false);
             }
 
-#if NET_2_0
             public override
-#endif
             int GetCharCount (byte [] bytes, int index, int count, bool refresh)
             {
                 CheckRange (bytes, index, count);
@@ -280,9 +365,7 @@ namespace I18N.CJK
                 return GetChars (bytes, byteIndex, byteCount, chars, charIndex, false);
             }
 
-#if NET_2_0
             public override
-#endif
             int GetChars(byte[] bytes, int byteIndex,
                                 int byteCount, char[] chars, int charIndex, bool refresh)
             {