2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / SByte.cs
index 077f6576cc3089b629553c9f3156f39a5a45cfaf..03e38e8ef7eebc8a6a30862bd0fbdbb48fce6f90 100644 (file)
@@ -7,6 +7,25 @@
 // (C) Ximian, Inc. http://www.ximian.com
 // Copyright (C) 2004 Novell (http://www.novell.com)
 //
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 
 using System.Globalization;
 
@@ -14,7 +33,12 @@ namespace System
 {
        [CLSCompliant(false)]
        [Serializable]
-       public struct SByte : IComparable, IFormattable, IConvertible
+       public struct SByte : IFormattable, IConvertible,
+#if NET_2_0
+               IComparable, IComparable<SByte>
+#else
+               IComparable
+#endif
        {
                public const sbyte MinValue = -128;
                public const sbyte MaxValue = 127;
@@ -51,8 +75,24 @@ namespace System
                        return m_value;
                }
 
-               [CLSCompliant(false)]
-               public static sbyte Parse (string s)
+#if NET_2_0
+               public int CompareTo (sbyte value)
+               {
+                       if (m_value == value)
+                               return 0;
+                       if (m_value > value)
+                               return 1;
+                       else
+                               return -1;
+               }
+
+               public bool Equals (sbyte value)
+               {
+                       return value == m_value;
+               }
+#endif
+
+               internal static bool Parse (string s, bool tryParse, out sbyte result)
                {
                        int ival = 0;
                        int len;
@@ -60,8 +100,13 @@ namespace System
                        bool neg = false;
                        bool digits_seen = false;
 
+                       result = 0;
+
                        if (s == null)
-                               throw new ArgumentNullException ("s");
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new ArgumentNullException ("s");
 
                        len = s.Length;
 
@@ -73,7 +118,10 @@ namespace System
                        }
 
                        if (i == len)
-                               throw new FormatException ();
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new FormatException ();
 
                        c = s [i];
                        if (c == '+')
@@ -93,21 +141,34 @@ namespace System
                                        if (Char.IsWhiteSpace (c)) {
                                                for (i++; i < len; i++) {
                                                        if (!Char.IsWhiteSpace (s [i]))
-                                                               throw new FormatException ();
+                                                               if (tryParse)
+                                                                       return false;
+                                                               else
+                                                                       throw new FormatException ();
                                                }
                                                break;
                                        } else
-                                               throw new FormatException ();
+                                               if (tryParse)
+                                                       return false;
+                                               else
+                                                       throw new FormatException ();
                                }
                        }
                        if (!digits_seen)
-                               throw new FormatException ();
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new FormatException ();
 
                        ival = neg ? ival : -ival;
                        if (ival < SByte.MinValue || ival > SByte.MaxValue)
-                               throw new OverflowException ();
+                               if (tryParse)
+                                       return false;
+                               else
+                                       throw new OverflowException ();
 
-                       return (sbyte) ival;
+                       result = (sbyte)ival;
+                       return true;
                }
 
                [CLSCompliant(false)]
@@ -132,6 +193,47 @@ namespace System
                        return (sbyte) tmpResult;
                }
 
+               [CLSCompliant(false)]
+               public static sbyte Parse (string s) {
+                       sbyte res;
+
+                       Parse (s, false, out res);
+
+                       return res;
+               }
+
+#if NET_2_0
+               [CLSCompliant(false)]
+               public static bool TryParse (string s, out sbyte result) {
+                       try {
+                               return Parse (s, true, out result);
+                       }
+                       catch (Exception) {
+                               result = 0;
+                               return false;
+                       }
+               }
+
+               [CLSCompliant(false)]
+               public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out sbyte result) {
+                       try {
+                               int tmpResult;
+
+                               result = 0;
+                               if (!Int32.TryParse (s, style, provider, out tmpResult))
+                                       return false;
+                               if (tmpResult > SByte.MaxValue || tmpResult < SByte.MinValue)
+                                       return false;
+                               result = (sbyte)tmpResult;
+                               return true;
+                       }
+                       catch (Exception) {
+                               result = 0;
+                               return false;
+                       }
+               }
+#endif
+
                public override string ToString ()
                {
                        return ToString (null, null);