Author: miguel
Date: 2005-11-14 17:37:32 -0500 (Mon, 14 Nov 2005)
New Revision: 53033
Modified:
trunk/mcs/class/corlib/System.Text/ASCIIEncoding.cs
trunk/mcs/class/corlib/System.Text/ChangeLog
trunk/mcs/class/corlib/System.Text/Encoding.cs
Log:
2005-11-14 Miguel de Icaza <[EMAIL PROTECTED]>
* ASCIIEncoding.cs, Encoding: Another snack, just a few methods
missing.
Also add some checks that were missing.
Modified: trunk/mcs/class/corlib/System.Text/ASCIIEncoding.cs
===================================================================
--- trunk/mcs/class/corlib/System.Text/ASCIIEncoding.cs 2005-11-14 20:41:00 UTC
(rev 53032)
+++ trunk/mcs/class/corlib/System.Text/ASCIIEncoding.cs 2005-11-14 22:37:32 UTC
(rev 53033)
@@ -104,8 +104,7 @@
}
// Convenience wrappers for "GetBytes".
- public override int GetBytes (String s, int charIndex, int charCount,
- byte[] bytes,
int byteIndex)
+ public override int GetBytes (String s, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
if (s == null) {
throw new ArgumentNullException ("s");
@@ -154,27 +153,22 @@
}
// Get the characters that result from decoding a byte buffer.
- public override int GetChars (byte[] bytes, int byteIndex, int
byteCount,
- char[] chars,
int charIndex)
+ public override int GetChars (byte[] bytes, int byteIndex, int
byteCount, char[] chars, int charIndex)
{
- if (bytes == null) {
+ if (bytes == null)
throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
+ if (chars == null)
throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
+ if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException ("byteIndex",
_("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
+ if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
throw new ArgumentOutOfRangeException ("byteCount",
_("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
+ if (charIndex < 0 || charIndex > chars.Length)
throw new ArgumentOutOfRangeException ("charIndex",
_("ArgRange_Array"));
- }
- if ((chars.Length - charIndex) < byteCount) {
+
+ if ((chars.Length - charIndex) < byteCount)
throw new ArgumentException
(_("Arg_InsufficientSpace"));
- }
+
int count = byteCount;
while (count-- > 0) {
char c = (char)(bytes [byteIndex++]);
@@ -218,13 +212,73 @@
throw new ArgumentOutOfRangeException ("count",
_("ArgRange_Array"));
}
if (count == 0)
- return String.Empty;
+ return String.Empty;
+
unsafe {
fixed (byte *ss = &bytes [0]) {
return new String ((sbyte*)ss, index, count);
}
}
}
+
+#if NET_2_0
+ [CLSCompliantAttribute (false)]
+ public unsafe override int GetBytes (char *chars, int charCount, byte
*bytes, int byteCount)
+ {
+ if (chars == null)
+ throw new ArgumentNullException ("chars");
+ if (bytes == null)
+ throw new ArgumentNullException ("bytes");
+ if (charCount < 0)
+ throw new ArgumentOutOfRangeException ("charCount");
+ if (byteCount < 0)
+ throw new ArgumentOutOfRangeException ("byteCount");
+
+ if (byteCount < charCount)
+ throw new ArgumentException ("bytecount is less than
the number of bytes required", "byteCount");
+
+ for (int i = 0; i < charCount; i++){
+ char c = chars [i];
+ bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
+ }
+ return charCount;
+ }
+
+ [CLSCompliantAttribute(false)]
+ public unsafe override int GetChars (byte *bytes, int byteCount, char
*chars, int charCount)
+ {
+ if (bytes == null)
+ throw new ArgumentNullException ("bytes");
+ if (chars == null)
+ throw new ArgumentNullException ("chars");
+ if (charCount < 0)
+ throw new ArgumentOutOfRangeException ("charCount");
+ if (byteCount < 0)
+ throw new ArgumentOutOfRangeException ("byteCount");
+ if (charCount < byteCount)
+ throw new ArgumentException ("charcount is less than
the number of bytes required", "charCount");
+
+ for (int i = 0; i < byteCount; i++){
+ byte b = bytes [i];
+ chars [i] = b > 127 ? '?' : (char) b;
+ }
+ return byteCount;
+
+ }
+
+ [CLSCompliantAttribute(false)]
+ public unsafe override int GetCharCount (byte *bytes, int count)
+ {
+ return count;
+ }
+
+ [CLSCompliantAttribute(false)]
+ public unsafe override int GetByteCount (char *chars, int count)
+ {
+ return count;
+ }
+#else
+ // This routine is gone in 2.x
public override String GetString (byte[] bytes)
{
if (bytes == null) {
@@ -239,7 +293,8 @@
}
}
}
-
+#endif
+
}; // class ASCIIEncoding
}; // namespace System.Text
Modified: trunk/mcs/class/corlib/System.Text/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Text/ChangeLog 2005-11-14 20:41:00 UTC
(rev 53032)
+++ trunk/mcs/class/corlib/System.Text/ChangeLog 2005-11-14 22:37:32 UTC
(rev 53033)
@@ -1,3 +1,10 @@
+2005-11-14 Miguel de Icaza <[EMAIL PROTECTED]>
+
+ * ASCIIEncoding.cs, Encoding: Another snack, just a few methods
+ missing.
+
+ Also add some checks that were missing.
+
2005-11-11 Sebastien Pouliot <[EMAIL PROTECTED]>
* StringBuilder.cs: Fix ISerializable.GetObjectData (remoting tests
@@ -5,6 +12,8 @@
2005-11-11 Miguel de Icaza <[EMAIL PROTECTED]>
+ * Encoding.cs, UnicodeEncoding.cs: A few 2.x methods.
+
* StringBuilder.cs (Text): Added serialization support in 2.x.
2005-10-22 Jonathan Pryor <[EMAIL PROTECTED]>
Modified: trunk/mcs/class/corlib/System.Text/Encoding.cs
===================================================================
--- trunk/mcs/class/corlib/System.Text/Encoding.cs 2005-11-14 20:41:00 UTC
(rev 53032)
+++ trunk/mcs/class/corlib/System.Text/Encoding.cs 2005-11-14 22:37:32 UTC
(rev 53033)
@@ -799,6 +799,10 @@
[CLSCompliantAttribute(false)]
public unsafe virtual int GetByteCount (char *chars, int count)
{
+ if (chars == null)
+ throw new ArgumentNullException ("chars");
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
char [] c = new char [count];
for (int p = 0; p < count; p++)
@@ -810,6 +814,11 @@
[CLSCompliantAttribute(false)]
public unsafe virtual int GetCharCount (byte *bytes, int count)
{
+ if (bytes == null)
+ throw new ArgumentNullException ("bytes");
+ if (count < 0)
+ throw new ArgumentOutOfRangeException ("count");
+
byte [] ba = new byte [count];
for (int i = 0; i < count; i++)
ba [i] = bytes [i];
@@ -819,15 +828,56 @@
[CLSCompliantAttribute(false)]
public unsafe virtual int GetChars (byte *bytes, int byteCount, char
*chars, int charCount)
{
+ if (bytes == null)
+ throw new ArgumentNullException ("bytes");
+ if (chars == null)
+ throw new ArgumentNullException ("chars");
+ if (charCount < 0)
+ throw new ArgumentOutOfRangeException ("charCount");
+ if (byteCount < 0)
+ throw new ArgumentOutOfRangeException ("byteCount");
+
byte [] ba = new byte [byteCount];
for (int i = 0; i < byteCount; i++)
ba [i] = bytes [i];
char [] ret = GetChars (ba, 0, byteCount);
- int top = Math.Min (ret.Length, charCount);
+ int top = ret.Length;
+
+ if (top > charCount)
+ throw new ArgumentException ("charCount is less than
the number of characters produced", "charCount");
+
for (int i = 0; i < top; i++)
chars [i] = ret [i];
return top;
}
+
+ [CLSCompliantAttribute(false)]
+ public unsafe virtual int GetBytes (char *chars, int charCount, byte
*bytes, int byteCount)
+ {
+ if (bytes == null)
+ throw new ArgumentNullException ("bytes");
+ if (chars == null)
+ throw new ArgumentNullException ("chars");
+ if (charCount < 0)
+ throw new ArgumentOutOfRangeException ("charCount");
+ if (byteCount < 0)
+ throw new ArgumentOutOfRangeException ("byteCount");
+
+ char [] c = new char [charCount];
+
+ for (int i = 0; i < charCount; i++)
+ c [i] = chars [i];
+
+ byte [] b = GetBytes (c, 0, charCount);
+ int top = b.Length;
+ if (top > byteCount)
+ throw new ArgumentException ("byteCount is less that
the number of bytes produced", "byteCount");
+
+ for (int i = 0; i < top; i++)
+ bytes [i] = b [i];
+
+ return b.Length;
+ }
#endif
}; // class Encoding
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches