Merge pull request #571 from igotti-google/jt2
[mono.git] / mcs / class / I18N / West / CP865.cs
index 404c5d27d5e52a76285d54b85f0b2528dedff570..e56b6472ec501c53b0acf9f0ed3a4413122b5180 100644 (file)
@@ -24,6 +24,9 @@
 
 // Generated from "ibm-865.ucm".
 
+// WARNING: Modifying this file directly might be a bad idea.
+// You should edit the code generator tools/ucm2cp.c instead for your changes
+// to appear in all relevant classes.
 namespace I18N.West
 {
 
@@ -31,6 +34,7 @@ using System;
 using System.Text;
 using I18N.Common;
 
+[Serializable]
 public class CP865 : ByteEncoding
 {
        public CP865()
@@ -85,8 +89,53 @@ public class CP865 : ByteEncoding
                '\u207F', '\u00B2', '\u25A0', '\u00A0', 
        };
 
+       // Get the number of bytes needed to encode a character buffer.
+       public unsafe override int GetByteCountImpl (char* chars, int count)
+       {
+               if (this.EncoderFallback != null)               {
+                       //Calculate byte count by actually doing encoding and discarding the data.
+                       return GetBytesImpl(chars, count, null, 0);
+               }
+               else
+               
+               {
+                       return count;
+               }
+       }
+       
+       // Get the number of bytes needed to encode a character buffer.
+       public override int GetByteCount (String s)
+       {
+               if (this.EncoderFallback != null)
+               {
+                       //Calculate byte count by actually doing encoding and discarding the data.
+                       unsafe
+                       {
+                               fixed (char *s_ptr = s)
+                               {
+                                       return GetBytesImpl(s_ptr, s.Length, null, 0);
+                               }
+                       }
+               }
+               else
+               {
+                       //byte count equals character count because no EncoderFallback set
+                       return s.Length;
+               }
+       }
+       
+       //ToBytes is just an alias for GetBytesImpl, but doesn't return byte count
        protected unsafe override void ToBytes(char* chars, int charCount,
                                        byte* bytes, int byteCount)
+       {
+               //Calling ToBytes with null destination buffer doesn't make any sense
+               if (bytes == null)
+                       throw new ArgumentNullException("bytes");
+               GetBytesImpl(chars, charCount, bytes, byteCount);
+       }
+       
+       public unsafe override int GetBytesImpl (char* chars, int charCount,
+                                                byte* bytes, int byteCount)
        {
                int ch;
                int charIndex = 0;
@@ -94,9 +143,11 @@ public class CP865 : ByteEncoding
 #if NET_2_0
                EncoderFallbackBuffer buffer = null;
 #endif
-               while(charCount > 0)
+               while (charCount > 0)
                {
-                       ch = (int)(chars[charIndex++]);
+                       ch = (int)(chars[charIndex]);
+                       charIndex++;
+                       charCount--;
                        if(ch >= 26) switch(ch)
                        {
                                case 0x001B:
@@ -372,20 +423,24 @@ public class CP865 : ByteEncoding
                                default:
                                {
                                        if(ch >= 0xFF01 && ch <= 0xFF5E)
+                                       {
                                                ch -= 0xFEE0;
+                                       }
                                        else
-#if NET_2_0
+                                       {
                                                HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount);
-#else
-                                               ch = 0x3F;
-#endif
+                                               continue;
+                                       }
                                }
                                break;
                        }
-                       bytes[byteIndex++] = (byte)ch;
-                       --charCount;
-                       --byteCount;
+                       //Write encoded byte to buffer, if buffer is defined and fallback was not used
+                       if (bytes != null)
+                               bytes[byteIndex] = (byte)ch;
+                       byteIndex++;
+                       byteCount--;
                }
+               return byteIndex;
        }
 
        /*
@@ -671,9 +726,13 @@ public class CP865 : ByteEncoding
                                default:
                                {
                                        if(ch >= 0xFF01 && ch <= 0xFF5E)
+                                       {
                                                ch -= 0xFEE0;
+                                       }
                                        else
+                                       {
                                                ch = 0x3F;
+                                       }
                                }
                                break;
                        }
@@ -685,6 +744,7 @@ public class CP865 : ByteEncoding
 
 }; // class CP865
 
+[Serializable]
 public class ENCibm865 : CP865
 {
        public ENCibm865() : base() {}