regenerated encoder classes with a patched ucm2cp.c with proper EncoderFallback support
[mono.git] / mcs / class / I18N / Other / CP28594.cs
index c8ca59fdfad31db3f6370dcd14afbf1688dc4721..86f015d419859891ecbbc544359a05b640e79418 100644 (file)
 
 // Generated from "ibm-914.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.Other
 {
 
 using System;
+using System.Text;
 using I18N.Common;
 
+[Serializable]
 public class CP28594 : ByteEncoding
 {
        public CP28594()
@@ -84,13 +89,64 @@ public class CP28594 : ByteEncoding
                '\u00FC', '\u0169', '\u016B', '\u02D9', 
        };
 
-       protected override void ToBytes(char[] chars, int charIndex, int charCount,
-                                       byte[] bytes, int byteIndex)
+       // 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;
-               while(charCount > 0)
+               int charIndex;
+               int byteIndex = 0;
+#if NET_2_0
+               EncoderFallbackBuffer buffer = null;
+#endif
+               for (charIndex=0; charCount > 0; charIndex++, charCount--)
                {
-                       ch = (int)(chars[charIndex++]);
+                       ch = (int)(chars[charIndex]);
+                       bool fallback = false;
                        if(ch >= 161) switch(ch)
                        {
                                case 0x00A4:
@@ -192,17 +248,39 @@ public class CP28594 : ByteEncoding
                                default:
                                {
                                        if(ch >= 0xFF01 && ch <= 0xFF5E)
+                                       {
                                                ch -= 0xFEE0;
+                                       }
                                        else
+                                       {
+#if NET_2_0
+                                               //Execute fallback routine and set fallback flag on
+                                               HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount);
+                                               fallback = true;
+#else
                                                ch = 0x3F;
+#endif
+                                       }
                                }
                                break;
                        }
-                       bytes[byteIndex++] = (byte)ch;
-                       --charCount;
+                       //Write encoded byte to buffer, if buffer is defined and fallback was not used
+                       if (bytes != null && !fallback)
+                       {
+                               bytes[byteIndex] = (byte)ch;
+                       }
+                       
+                       //Bump counters if fallback was not used
+                       if (!fallback)
+                       {
+                               byteIndex++;
+                               byteCount--;
+                       }
                }
+               return byteIndex;
        }
 
+       /*
        protected override void ToBytes(String s, int charIndex, int charCount,
                                        byte[] bytes, int byteIndex)
        {
@@ -311,9 +389,13 @@ public class CP28594 : ByteEncoding
                                default:
                                {
                                        if(ch >= 0xFF01 && ch <= 0xFF5E)
+                                       {
                                                ch -= 0xFEE0;
+                                       }
                                        else
+                                       {
                                                ch = 0x3F;
+                                       }
                                }
                                break;
                        }
@@ -321,9 +403,11 @@ public class CP28594 : ByteEncoding
                        --charCount;
                }
        }
+       */
 
 }; // class CP28594
 
+[Serializable]
 public class ENCiso_8859_4 : CP28594
 {
        public ENCiso_8859_4() : base() {}