// (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;
{
[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;
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;
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;
}
if (i == len)
- throw new FormatException ();
+ if (tryParse)
+ return false;
+ else
+ throw new FormatException ();
c = s [i];
if (c == '+')
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)]
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);