Author: atsushi Date: 2005-12-01 10:17:39 -0500 (Thu, 01 Dec 2005) New Revision: 53768
Added: trunk/mcs/class/I18N/Common/MonoEncoding.cs Modified: trunk/mcs/class/I18N/Common/ByteEncoding.cs trunk/mcs/class/I18N/Common/ChangeLog trunk/mcs/class/I18N/Common/I18N.dll.sources Log: 2005-12-01 Atsushi Enomoto <[EMAIL PROTECTED]> * MonoEncoding.cs : new file, which is going to be used as the abstract class for all Encoding classes in I18N.*.dll. Only GetBytesImpl() is required in derived classes. * I18N.dll.sources : added above. * ByteEncoding : HandleFallback() is moved to MonoEncoding. Now that only ToBytes() with pointers is required, commented out other ToBytes() overloads. GetBytesImpl() is implemented to call ToBytes() and thus all of the derived classes work as they used to be. Modified: trunk/mcs/class/I18N/Common/ByteEncoding.cs =================================================================== --- trunk/mcs/class/I18N/Common/ByteEncoding.cs 2005-12-01 15:09:55 UTC (rev 53767) +++ trunk/mcs/class/I18N/Common/ByteEncoding.cs 2005-12-01 15:17:39 UTC (rev 53768) @@ -34,7 +34,7 @@ // subclasses providing implementations of the "ToBytes" methods to perform // the char->byte conversion. -public abstract class ByteEncoding : Encoding +public abstract class ByteEncoding : MonoEncoding { // Internal state. protected char[] toChars; @@ -105,33 +105,11 @@ return s.Length; } -#if NET_2_0 - protected unsafe void HandleFallback (ref EncoderFallbackBuffer buffer, - char* chars, ref int charIndex, ref int charCount, - byte* bytes, ref int byteIndex, ref int byteCount) - { - if (buffer == null) - buffer = EncoderFallback.CreateFallbackBuffer (); - if (Char.IsSurrogate (chars [charIndex]) && charCount > 0 && - Char.IsSurrogate (chars [charIndex + 1])) { - buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex); - charIndex++; - charCount--; - } - else - buffer.Fallback (chars [charIndex], charIndex); - char [] tmp = new char [buffer.Remaining]; - int idx = 0; - while (buffer.Remaining > 0) - tmp [idx++] = buffer.GetNextChar (); - fixed (char* tmparr = tmp) { - byteIndex += GetBytes (tmparr, tmp.Length, bytes + byteIndex, byteCount); - } - } -#endif - // Convert an array of characters into a byte buffer, // once the parameters have been validated. + protected unsafe abstract void ToBytes ( + char* chars, int charCount, byte* bytes, int byteCount); + /* protected unsafe virtual void ToBytes ( char* chars, int charCount, byte* bytes, int byteCount) { @@ -142,6 +120,7 @@ Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount); ToBytes (carr, 0, charCount, barr, 0); } + */ // Convert an array of characters into a byte buffer, // once the parameters have been validated. @@ -160,6 +139,7 @@ } } +/* // Convert a string into a byte buffer, once the parameters // have been validated. protected unsafe virtual void ToBytes(String s, int charIndex, int charCount, @@ -176,7 +156,15 @@ } } } +*/ + [CLSCompliant (false)] + public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount) + { + ToBytes (chars, charCount, bytes, byteCount); + return charCount; + } +/* // Get the bytes that result from encoding a character buffer. public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) @@ -250,6 +238,7 @@ ToBytes(s, charIndex, charCount, bytes, byteIndex); return charCount; } +*/ // Get the number of characters needed to decode a byte buffer. public override int GetCharCount(byte[] bytes, int index, int count) Modified: trunk/mcs/class/I18N/Common/ChangeLog =================================================================== --- trunk/mcs/class/I18N/Common/ChangeLog 2005-12-01 15:09:55 UTC (rev 53767) +++ trunk/mcs/class/I18N/Common/ChangeLog 2005-12-01 15:17:39 UTC (rev 53768) @@ -1,3 +1,15 @@ +2005-12-01 Atsushi Enomoto <[EMAIL PROTECTED]> + + * MonoEncoding.cs : new file, which is going to be used as the + abstract class for all Encoding classes in I18N.*.dll. Only + GetBytesImpl() is required in derived classes. + * I18N.dll.sources : added above. + * ByteEncoding : HandleFallback() is moved to MonoEncoding. + Now that only ToBytes() with pointers is required, commented out + other ToBytes() overloads. + GetBytesImpl() is implemented to call ToBytes() and thus all of + the derived classes work as they used to be. + 2005-11-30 Atsushi Enomoto <[EMAIL PROTECTED]> * Makefile : now it uses unsafe pointers. Modified: trunk/mcs/class/I18N/Common/I18N.dll.sources =================================================================== --- trunk/mcs/class/I18N/Common/I18N.dll.sources 2005-12-01 15:09:55 UTC (rev 53767) +++ trunk/mcs/class/I18N/Common/I18N.dll.sources 2005-12-01 15:17:39 UTC (rev 53768) @@ -3,4 +3,5 @@ ByteEncoding.cs Handlers.cs Manager.cs +MonoEncoding.cs Strings.cs Added: trunk/mcs/class/I18N/Common/MonoEncoding.cs =================================================================== --- trunk/mcs/class/I18N/Common/MonoEncoding.cs 2005-12-01 15:09:55 UTC (rev 53767) +++ trunk/mcs/class/I18N/Common/MonoEncoding.cs 2005-12-01 15:17:39 UTC (rev 53768) @@ -0,0 +1,186 @@ +// +// MonoEncoding.cs +// +// Author: +// Atsushi Enomoto <[EMAIL PROTECTED]> +// +// Copyright (C) 2005 Novell, Inc. http://www.novell.com +// +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace I18N.Common +{ + + public abstract class MonoEncoding : Encoding + { + public MonoEncoding (int codePage) + : base (codePage) + { + } + +#if NET_2_0 + [CLSCompliant (false)] + public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer, + char* chars, ref int charIndex, ref int charCount, + byte* bytes, ref int byteIndex, ref int byteCount) + { + if (buffer == null) + buffer = EncoderFallback.CreateFallbackBuffer (); + if (Char.IsSurrogate (chars [charIndex]) && charCount > 0 && + Char.IsSurrogate (chars [charIndex + 1])) { + buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex); + charIndex++; + charCount--; + } + else + buffer.Fallback (chars [charIndex], charIndex); + char [] tmp = new char [buffer.Remaining]; + int idx = 0; + while (buffer.Remaining > 0) + tmp [idx++] = buffer.GetNextChar (); + fixed (char* tmparr = tmp) { + byteIndex += GetBytes (tmparr, tmp.Length, bytes + byteIndex, byteCount); + } + } +#endif + + // Get the bytes that result from encoding a character buffer. + public override int GetBytes ( + char [] chars, int charIndex, int charCount, + byte [] bytes, int byteIndex) + { + if (chars == null) + throw new ArgumentNullException ("chars"); + if (bytes == null) + throw new ArgumentNullException ("bytes"); + if (charIndex < 0 || charIndex > chars.Length) + throw new ArgumentOutOfRangeException + ("charIndex", Strings.GetString ("ArgRange_Array")); + if (charCount < 0 || charCount > (chars.Length - charIndex)) + throw new ArgumentOutOfRangeException + ("charCount", Strings.GetString ("ArgRange_Array")); + if (byteIndex < 0 || byteIndex > bytes.Length) + throw new ArgumentOutOfRangeException + ("byteIndex", Strings.GetString ("ArgRange_Array")); + if (bytes.Length - byteIndex < charCount) + throw new ArgumentException (Strings.GetString ("Arg_InsufficientSpace"), "bytes"); + + if (charCount == 0) + return 0; + + unsafe { + fixed (char* cptr = chars) { + fixed (byte* bptr = bytes) { + return GetBytesImpl ( + cptr + charIndex, + charCount, + bptr + byteIndex, + bytes.Length - byteIndex); + } + } + } + } + + // Convenience wrappers for "GetBytes". + public override int GetBytes (string s, int charIndex, int charCount, + byte [] bytes, int byteIndex) + { + // Validate the parameters. + if(s == null) + throw new ArgumentNullException("s"); + if(bytes == null) + throw new ArgumentNullException("bytes"); + if(charIndex < 0 || charIndex > s.Length) + throw new ArgumentOutOfRangeException + ("charIndex", + Strings.GetString("ArgRange_StringIndex")); + if(charCount < 0 || charCount > (s.Length - charIndex)) + throw new ArgumentOutOfRangeException + ("charCount", + Strings.GetString("ArgRange_StringRange")); + if(byteIndex < 0 || byteIndex > bytes.Length) + throw new ArgumentOutOfRangeException + ("byteIndex", + Strings.GetString("ArgRange_Array")); + if((bytes.Length - byteIndex) < charCount) + throw new ArgumentException + (Strings.GetString("Arg_InsufficientSpace"), "bytes"); + + if (charCount == 0 || bytes.Length == byteIndex) + return 0; + unsafe { + fixed (char* cptr = s) { + fixed (byte* bptr = bytes) { + return GetBytesImpl ( + cptr + charIndex, + charCount, + bptr + byteIndex, + bytes.Length - byteIndex); + } + } + } + } + +#if NET_2_0 + public unsafe override int GetBytes (char* chars, int charCount, + byte* bytes, int byteCount) + + { + return GetBytesImpl (chars, charCount, bytes, byteCount); + } +#endif + + [CLSCompliant (false)] + public unsafe abstract int GetBytesImpl (char* chars, int charCount, + byte* bytes, int byteCount); + + public abstract class MonoEncoder : Encoder + { + MonoEncoding encoding; + + public MonoEncoder (MonoEncoding encoding) + { + this.encoding = encoding; + } + + public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex, bool flush) + { + if (charCount == 0) + return 0; + unsafe { + fixed (char* cptr = chars) { + fixed (byte* bptr = bytes) { + return GetBytesImpl (cptr + charIndex, + charCount, + bptr + byteIndex, + bytes.Length - byteIndex, + flush); + } + } + } + } + + public unsafe abstract int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount, bool flush); + + #if NET_2_0 + public unsafe override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount, bool flush) + { + return GetBytesImpl (chars, charCount, bytes, byteCount, flush); + } + + //[CLSCompliant (false)] + public unsafe void HandleFallback ( + char* chars, ref int charIndex, ref int charCount, + byte* bytes, ref int byteIndex, ref int byteCount) + { + EncoderFallbackBuffer buffer = FallbackBuffer; + encoding.HandleFallback (ref buffer, + chars, ref charIndex, ref charCount, + bytes, ref byteIndex, ref byteCount); + } + #endif + } + } +} Property changes on: trunk/mcs/class/I18N/Common/MonoEncoding.cs ___________________________________________________________________ Name: svn:eol-style + native _______________________________________________ Mono-patches maillist - Mono-patches@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-patches