Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / corlib / System.Text / UTF32Encoding.cs
index 1cb055a4d47e3fdc7ed908975886c68e3e9af0bf..16413e8ad578a703c747aabed5bd206b6c0e0c2c 100644 (file)
@@ -31,8 +31,6 @@
  * OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#if NET_2_0
-
 namespace System.Text
 {
 
@@ -59,30 +57,28 @@ public sealed class UTF32Encoding : Encoding
        {
        }
 
-       public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalid)
+       public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters)
                : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
        {
                this.bigEndian = bigEndian;
                this.byteOrderMark = byteOrderMark;
 
-               if (throwOnInvalid)
+               if (throwOnInvalidCharacters)
                        SetFallbackInternal (EncoderFallback.ExceptionFallback,
                                DecoderFallback.ExceptionFallback);
                else
-                       SetFallbackInternal (EncoderFallback.ReplacementFallback,
-                               DecoderFallback.ReplacementFallback);
+                       SetFallbackInternal (new EncoderReplacementFallback ("\uFFFD"),
+                               new DecoderReplacementFallback ("\uFFFD"));
 
                if (bigEndian){
                        body_name = "utf-32BE";
                        encoding_name = "UTF-32 (Big-Endian)";
                        header_name = "utf-32BE";
-                       is_browser_save = false;
                        web_name = "utf-32BE";
                } else {
                        body_name = "utf-32";
                        encoding_name = "UTF-32";
                        header_name = "utf-32";
-                       is_browser_save = true;
                        web_name = "utf-32";
                }
                
@@ -105,7 +101,7 @@ public sealed class UTF32Encoding : Encoding
                        throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
                }
                int ret = 0;
-               for (int i = index; i < count; i++) {
+               for (int i = index; i < index + count; i++) {
                        if (Char.IsSurrogate (chars [i])) {
                                if (i + 1 < chars.Length && Char.IsSurrogate (chars [i + 1]))
                                        ret += 4;
@@ -309,18 +305,18 @@ public sealed class UTF32Encoding : Encoding
        public override byte[] GetPreamble ()
        {
                if (byteOrderMark) {
-                       byte[] preamble = new byte[2];
+                       byte[] preamble = new byte[4];
                        if (bigEndian) {
-                               preamble[0] = (byte)0xFE;
-                               preamble[1] = (byte)0xFF;
+                               preamble[2] = (byte)0xFE;
+                               preamble[3] = (byte)0xFF;
                        } else {
                                preamble[0] = (byte)0xFF;
                                preamble[1] = (byte)0xFE;
                        }
                        return preamble;
-               } else {
-                       return new byte [0];
                }
+               
+               return EmptyArray<byte>.Value;
        }
 
        // Determine if this object is equal to another.
@@ -340,7 +336,12 @@ public sealed class UTF32Encoding : Encoding
        // Get the hash code for this object.
        public override int GetHashCode ()
        {
-               return base.GetHashCode ();
+               int basis = base.GetHashCode ();
+               if (bigEndian)
+                       basis ^= 0x1F;
+               if (byteOrderMark)
+                       basis ^= 0x3F;
+               return basis;
        }
 
        // UTF32 decoder implementation.
@@ -466,22 +467,53 @@ public sealed class UTF32Encoding : Encoding
 
        } // class UTF32Decoder
        
-#if NET_2_0
        [CLSCompliantAttribute(false)]
        public unsafe override int GetByteCount (char *chars, int count)
        {
+               if (chars == null)
+                       throw new ArgumentNullException ("chars");
                return count * 4;
        }
-#else
-       public override byte [] GetBytes (String s)
+
+       // a bunch of practically missing implementations (but should just work)
+
+       public override int GetByteCount (string s)
        {
-               return base.GetBytes (s);
+               return base.GetByteCount (s);
+       }
+
+       [CLSCompliantAttribute (false)]
+       public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
+       {
+               return base.GetBytes (chars, charCount, bytes, byteCount);
+       }
+
+       public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
+       {
+               return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
        }
-#endif
-       
 
+       [CLSCompliantAttribute (false)]
+       public override unsafe int GetCharCount (byte *bytes, int count)
+       {
+               return base.GetCharCount (bytes, count);
+       }
+
+       [CLSCompliantAttribute (false)]
+       public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
+       {
+               return base.GetChars (bytes, byteCount, chars, charCount);
+       }
+
+       public override string GetString (byte [] bytes, int index, int count)
+       {
+               return base.GetString (bytes, index, count);
+       }
+
+       public override Encoder GetEncoder ()
+       {
+               return base.GetEncoder ();
+       }
 }; // class UTF32Encoding
 
 }; // namespace System.Text
-
-#endif