[Mono-list] System.Text.ASCIIEncoding patch

Mike Kestner mkestner@speakeasy.net
14 Mar 2002 23:17:09 -0600


--=-ApvMd+qvthj5s/SV9ok/
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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.

Mike



--=-ApvMd+qvthj5s/SV9ok/
Content-Disposition: attachment; filename=ascii.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1

Index: ASCIIEncoding.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D true;
 		}
=20
-		[MonoTODO]
+		public override int GetByteCount (string chars)
+		{
+			if (chars =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			return chars.Length;
+		}
+
+		public override int GetByteCount (char[] chars)
+		{
+			if (chars =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			return chars.Length;
+		}
+
+		public override int GetByteCount (char[] chars, int index, int count)
+		{
+			if (chars =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			if ((index < 0) || (count <=3D 0) || ((index + count) >=3D chars.Length=
))
+				throw new ArgumentOutOfRangeException ();
+
+			return count;
+		}
+
+		public override int GetBytes (char[] chars, int charIndex, int charCount=
,
+					      byte[] bytes, int byteIndex)
+		{
+			if ((bytes =3D=3D null) || (chars =3D=3D 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 =3D 0; i < charCount; i++)
+				if (chars[charIndex+i] > 0x7f)
+					bytes[byteIndex+i] =3D (byte) '?';
+				else
+					bytes[byteIndex+i] =3D (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 =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			return bytes.Length;
+		}
+
+		public override int GetCharCount (byte[] bytes, int index, int count)
+		{
+			if (bytes =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			if ((index < 0) || (count <=3D 0) || ((index + count) >=3D bytes.Length=
))
+				throw new ArgumentOutOfRangeException ();
+
+			return count;
+		}
+
+		public override int GetChars (byte[] bytes, int byteIndex, int byteCount=
,
+					      char[] chars, int charIndex)
+		{
+			if ((bytes =3D=3D null) || (chars =3D=3D 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 =3D 0; i < byteCount; i++)
+				if (bytes[byteIndex+i] > 0x7f)
+					chars[charIndex+i] =3D '?';
+				else
+					chars[charIndex+i] =3D (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)=20
+				throw new ArgumentOutOfRangeException ();
+
+			return charCount;
 		}
=20
 		public override int GetMaxCharCount (int byteCount)
 		{
+			if (byteCount < 0)=20
+				throw new ArgumentOutOfRangeException ();
+
 			return byteCount;
 		}
+
+		public override string GetString (byte[] bytes)
+		{
+			if (bytes =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			return new String (GetChars (bytes, 0, bytes.Length));
+		}
+
+		public override string GetString (byte[] bytes, int byteIndex, int byteC=
ount)
+		{
+			if (bytes =3D=3D null)=20
+				throw new ArgumentNullException ();
+
+			if ((byteIndex < 0) || (byteCount <=3D 0) ||=20
+			    ((byteIndex + byteCount) >=3D bytes.Length))
+				throw new ArgumentOutOfRangeException ();
+
+			return new String (GetChars (bytes, byteIndex, byteCount));
+		}
+
 	}
 }
=20
Index: Encoding.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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;
                 }
=20
-                public int GetBytes (char[] chars, int charIndex, int char=
Count,
-				     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, byte=
Index, true);
 		}

--=-ApvMd+qvthj5s/SV9ok/--