Fixes #683475: - Fixed ISO2022JPEncoder's GetBytesCount(..) to avoid it from returnin...
[mono.git] / mcs / class / I18N / CJK / CP949.cs
index de4628c77fe55159eb1002b767831065cedc241f..35f85dc7ccb760f88cdae7dd77080584d92c22a5 100644 (file)
@@ -117,15 +117,13 @@ namespace I18N.CJK
 
         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;
             DbcsConvert convert = GetConvert ();
-#if NET_2_0
-            EncoderFallbackBuffer buffer = null;
-#endif
 
             // 00 00 - FF FF
             while (count-- > 0) {
@@ -174,7 +172,7 @@ namespace I18N.CJK
                 if (b1 == 0 && b2 == 0) {
 #if NET_2_0
                     HandleFallback (ref buffer, chars, ref charIndex, ref charCount,
-                        bytes, ref byteIndex, ref byteCount);
+                        bytes, ref byteIndex, ref byteCount, null);
 #else
                     bytes[byteIndex++] = (byte)'?';
 #endif
@@ -185,8 +183,80 @@ namespace I18N.CJK
             }
             return byteIndex - origIndex;
         }
+#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)
+                               {
+#if NET_2_0
+                                       // FIXME: handle fallback for GetByteCountImpl().
+                                       length++;
+#else
+                    length++;
+#endif
+                               }
+                               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;
+
+                       DbcsConvert convert = GetConvert();
+#if NET_2_0
+                       EncoderFallbackBuffer buffer = null;
+#endif
 
-        // Get the characters that result from decoding a byte buffer.
+                       // 00 00 - FF FF
+                       int origIndex = byteIndex;
+                       while (charCount-- > 0)
+                       {
+                               char c = chars[charIndex++];
+                               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)
+                               {
+#if NET_2_0
+                                       HandleFallback (ref buffer, chars, ref charIndex, ref charCount,
+                                               bytes, ref byteIndex, ref byteCount, null);
+#else
+                    bytes[byteIndex++] = (byte)'?';
+#endif
+                               }
+                               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);