[Mono-list] System.Text.ASCIIEncoding patch
Dietmar Maurer
dietmar@maurer-it.com
15 Mar 2002 10:15:55 +0100
On Fri, 2002-03-15 at 06:17, Mike Kestner wrote:
> All,
>
> Just so that nobody gets the urge to work on this while I'm writing test
> cases, here's a patch to implement the missing methods of ASCIIEncoding.
> Duplication of effort on this pig could push a hacker over the edge.
>
> I'm writing tests prior to committing, but if anyone has any feedback,
> I'm all ears.
It is a long time ago when I wrote those classes, but I thought
ASCIEncoding is already working. Why do you overwrite GetBytes, GetChar,
... - most of them should be handled by the base class (Encoder).
But maybe i miss something?
- Dietmar
>
> Mike
>
>
> ----
>
> Index: ASCIIEncoding.cs
> ===================================================================
> RCS file: /cvs/public/mcs/class/corlib/System.Text/ASCIIEncoding.cs,v
> retrieving revision 1.5
> diff -u -r1.5 ASCIIEncoding.cs
> --- ASCIIEncoding.cs 2002/01/05 06:11:42 1.5
> +++ ASCIIEncoding.cs 2002/03/15 01:07:11
> @@ -25,17 +25,141 @@
> is_mail_news_save = true;
> }
>
> - [MonoTODO]
> + public override int GetByteCount (string chars)
> + {
> + if (chars == null)
> + throw new ArgumentNullException ();
> +
> + return chars.Length;
> + }
> +
> + public override int GetByteCount (char[] chars)
> + {
> + if (chars == null)
> + throw new ArgumentNullException ();
> +
> + return chars.Length;
> + }
> +
> + public override int GetByteCount (char[] chars, int index, int count)
> + {
> + if (chars == null)
> + throw new ArgumentNullException ();
> +
> + if ((index < 0) || (count <= 0) || ((index + count) >= chars.Length))
> + throw new ArgumentOutOfRangeException ();
> +
> + return count;
> + }
> +
> + public override int GetBytes (char[] chars, int charIndex, int charCount,
> + byte[] bytes, int byteIndex)
> + {
> + if ((bytes == null) || (chars == null))
> + throw new ArgumentNullException ();
> +
> + if ((byteIndex < 0) || (charIndex < 0) || (charCount < 0) ||
> + ((charIndex + charCount) > chars.Length) ||
> + (byteIndex < bytes.Length))
> + throw new ArgumentOutOfRangeException ();
> +
> + if ((bytes.Length - byteIndex) < charCount)
> + throw new ArgumentException ();
> +
> + for (int i = 0; i < charCount; i++)
> + if (chars[charIndex+i] > 0x7f)
> + bytes[byteIndex+i] = (byte) '?';
> + else
> + bytes[byteIndex+i] = (byte) chars[charIndex+i];
> +
> + return charCount;
> + }
> +
> + public override int GetBytes (string chars, int charIndex, int charCount,
> + byte[] bytes, int byteIndex)
> + {
> + return GetBytes (chars.ToCharArray (), charIndex, charCount,
> + bytes, byteIndex);
> + }
> +
> + public override int GetCharCount (byte[] bytes)
> + {
> + if (bytes == null)
> + throw new ArgumentNullException ();
> +
> + return bytes.Length;
> + }
> +
> + public override int GetCharCount (byte[] bytes, int index, int count)
> + {
> + if (bytes == null)
> + throw new ArgumentNullException ();
> +
> + if ((index < 0) || (count <= 0) || ((index + count) >= bytes.Length))
> + throw new ArgumentOutOfRangeException ();
> +
> + return count;
> + }
> +
> + public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
> + char[] chars, int charIndex)
> + {
> + if ((bytes == null) || (chars == null))
> + throw new ArgumentNullException ();
> +
> + if ((byteIndex < 0) || (charIndex < 0) || (byteCount < 0) ||
> + ((byteIndex + byteCount) > bytes.Length) ||
> + (charIndex < chars.Length))
> + throw new ArgumentOutOfRangeException ();
> +
> + if ((chars.Length - charIndex) < byteCount)
> + throw new ArgumentException ();
> +
> + for (int i = 0; i < byteCount; i++)
> + if (bytes[byteIndex+i] > 0x7f)
> + chars[charIndex+i] = '?';
> + else
> + chars[charIndex+i] = (char) bytes[byteIndex+i];
> +
> + return byteCount;
> + }
> +
> public override int GetMaxByteCount (int charCount)
> {
> - // FIXME: this is wrong, dont know the right value
> - return charCount*6;
> + if (charCount < 0)
> + throw new ArgumentOutOfRangeException ();
> +
> + return charCount;
> }
>
> public override int GetMaxCharCount (int byteCount)
> {
> + if (byteCount < 0)
> + throw new ArgumentOutOfRangeException ();
> +
> return byteCount;
> }
> +
> + public override string GetString (byte[] bytes)
> + {
> + if (bytes == null)
> + throw new ArgumentNullException ();
> +
> + return new String (GetChars (bytes, 0, bytes.Length));
> + }
> +
> + public override string GetString (byte[] bytes, int byteIndex, int byteCount)
> + {
> + if (bytes == null)
> + throw new ArgumentNullException ();
> +
> + if ((byteIndex < 0) || (byteCount <= 0) ||
> + ((byteIndex + byteCount) >= bytes.Length))
> + throw new ArgumentOutOfRangeException ();
> +
> + return new String (GetChars (bytes, byteIndex, byteCount));
> + }
> +
> }
> }
>
> Index: Encoding.cs
> ===================================================================
> RCS file: /cvs/public/mcs/class/corlib/System.Text/Encoding.cs,v
> retrieving revision 1.9
> diff -u -r1.9 Encoding.cs
> --- Encoding.cs 2002/01/05 06:11:42 1.9
> +++ Encoding.cs 2002/03/15 01:07:11
> @@ -244,8 +244,8 @@
> return res;
> }
>
> - public int GetBytes (char[] chars, int charIndex, int charCount,
> - byte[] bytes, int byteIndex)
> + public virtual int GetBytes (char[] chars, int charIndex, int charCount,
> + byte[] bytes, int byteIndex)
> {
> return iconv_encoder.GetBytes (chars, charIndex, charCount, bytes, byteIndex, true);
> }
--
--------------------------------------------------
Dietmar Maurer Maurer IT Systemlösungen KEG
Technischer Leiter
Kohlgasse 51/9 Tel: +43 1 545 449 711
A - 1050 WIEN Fax: +43 1 545 449 722
Mobil: +43 699 105 880 32
dietmar@maurer-it.com
http://www.maurer-it.com
---------------------------------------------------