namespace System {
[Serializable]
- public struct Int16 : IFormattable, IConvertible,
-#if NET_2_0
- IComparable, IComparable<Int16>
-#else
- IComparable
-#endif
+ [System.Runtime.InteropServices.ComVisible (true)]
+ public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16>
{
public const short MaxValue = 32767;
internal short m_value;
- public int CompareTo (object v)
+ public int CompareTo (object value)
{
- if (v == null)
+ if (value == null)
return 1;
- if (!(v is System.Int16))
+ if (!(value is System.Int16))
throw new ArgumentException (Locale.GetText ("Value is not a System.Int16"));
- short xv = (short) v;
- if (m_value == xv)
- return 0;
- if (m_value > xv)
- return 1;
- else
- return -1;
+ return CompareTo ((short) value);
}
- public override bool Equals (object o)
+ public override bool Equals (object obj)
{
- if (!(o is System.Int16))
+ if (!(obj is System.Int16))
return false;
- return ((short) o) == m_value;
+ return ((short) obj) == m_value;
}
public override int GetHashCode ()
return m_value;
}
-#if NET_2_0
public int CompareTo (short value)
{
- if (m_value == value)
- return 0;
- if (m_value > value)
- return 1;
- else
- return -1;
+ return m_value - value;
}
- public bool Equals (short value)
+ public bool Equals (short obj)
{
- return value == m_value;
+ return obj == m_value;
}
-#endif
- internal static bool Parse (string s, bool tryParse, out short result)
+ internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
{
short val = 0;
int len;
bool digits_seen = false;
result = 0;
+ exc = null;
- if (s == null)
- if (tryParse)
- return false;
- else
- throw new ArgumentNullException ("s");
+ if (s == null) {
+ if (!tryParse)
+ exc = new ArgumentNullException ("s");
+ return false;
+ }
len = s.Length;
break;
}
- if (i == len)
- if (tryParse)
- return false;
- else
- throw new FormatException ();
+ if (i == len) {
+ if (!tryParse)
+ exc = Int32.GetFormatException ();
+ return false;
+ }
c = s [i];
if (c == '+')
for (; i < len; i++){
c = s [i];
-
if (c >= '0' && c <= '9'){
- val = checked ((short) (val * 10 + (c - '0') * sign));
- digits_seen = true;
- } else {
- if (Char.IsWhiteSpace (c)){
- for (i++; i < len; i++){
- if (!Char.IsWhiteSpace (s [i]))
- if (tryParse)
- return false;
- else
- throw new FormatException ();
- }
- break;
- } else
- if (tryParse)
- return false;
+ byte d = (byte) (c - '0');
+
+ if (val > (MaxValue/10))
+ goto overflow;
+
+ if (val == (MaxValue/10)){
+ if ((d > (MaxValue % 10)) && (sign == 1 || (d > ((MaxValue % 10) + 1))))
+ goto overflow;
+ if (sign == -1)
+ val = (short) ((val * sign * 10) - d);
else
- throw new FormatException ();
- }
- }
- if (!digits_seen)
- if (tryParse)
+ val = (short) ((val * 10) + d);
+
+ if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
+ result = val;
+ return true;
+ }
+ goto overflow;
+ } else
+ val = (short) (val * 10 + d);
+
+
+ digits_seen = true;
+ } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
return false;
- else
- throw new FormatException ();
+
+ }
+ if (!digits_seen) {
+ if (!tryParse)
+ exc = Int32.GetFormatException ();
+ return false;
+ }
- result = val;
+ if (sign == -1)
+ result = (short) (val * sign);
+ else
+ result = val;
+
return true;
+
+ overflow:
+ if (!tryParse)
+ exc = new OverflowException ("Value is too large");
+ return false;
}
- public static short Parse (string s, IFormatProvider fp)
+ public static short Parse (string s, IFormatProvider provider)
{
- return Parse (s, NumberStyles.Integer, fp);
+ return Parse (s, NumberStyles.Integer, provider);
}
public static short Parse (string s, NumberStyles style)
return Parse (s, style, null);
}
- public static short Parse (string s, NumberStyles style, IFormatProvider fp)
+ public static short Parse (string s, NumberStyles style, IFormatProvider provider)
{
- int tmpResult = Int32.Parse (s, style, fp);
- if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
- throw new OverflowException ("Value too large or too small.");
+ int tmpResult = Int32.Parse (s, style, provider);
+ if ((style & NumberStyles.AllowHexSpecifier) != 0) {
+ if (tmpResult >= 0 && tmpResult <= ushort.MaxValue)
+ return (short) tmpResult;
+ } else if (tmpResult <= MaxValue && tmpResult >= MinValue) {
+ return (short) tmpResult;
+ }
- return (short) tmpResult;
+ throw new OverflowException ("Value too large or too small.");
}
- public static short Parse (string s) {
+ public static short Parse (string s)
+ {
+ Exception exc;
short res;
- Parse (s, false, out res);
+ if (!Parse (s, false, out res, out exc))
+ throw exc;
return res;
}
-#if NET_2_0
- public static bool TryParse (string s, out short result) {
- try {
- return Parse (s, true, out result);
- }
- catch (Exception) {
+ public static bool TryParse (string s, out short result)
+ {
+ Exception exc;
+ if (!Parse (s, true, out result, out exc)) {
result = 0;
return false;
}
- }
- public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result) {
- try {
- int tmpResult;
+ return true;
+ }
- result = 0;
- if (!Int32.TryParse (s, style, provider, out tmpResult))
- return false;
- if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
- return false;
- result = (short)tmpResult;
- return true;
- }
- catch (Exception) {
- result = 0;
+ public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out short result)
+ {
+ int tmpResult;
+ result = 0;
+
+ if (!Int32.TryParse (s, style, provider, out tmpResult))
return false;
- }
+
+ if (tmpResult > Int16.MaxValue || tmpResult < Int16.MinValue)
+ return false;
+
+ result = (short)tmpResult;
+ return true;
}
-#endif
public override string ToString ()
{
- return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
+ return NumberFormatter.NumberToString (m_value, null);
}
- public string ToString (IFormatProvider fp)
+ public string ToString (IFormatProvider provider)
{
- return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), fp);
+ return NumberFormatter.NumberToString (m_value, provider);
}
public string ToString (string format)
return ToString (format, null);
}
- public string ToString (string format, IFormatProvider fp)
+ public string ToString (string format, IFormatProvider provider)
{
- NumberFormatInfo nfi = NumberFormatInfo.GetInstance( fp );
- return NumberFormatter.NumberToString(format, m_value, nfi);
+ return NumberFormatter.NumberToString(format, m_value, provider);
}
// =========== IConvertible Methods =========== //
{
return System.Convert.ToSByte (m_value);
}
-
+
float IConvertible.ToSingle (IFormatProvider provider)
{
return System.Convert.ToSingle (m_value);
}
- object IConvertible.ToType (Type conversionType, IFormatProvider provider)
+ object IConvertible.ToType (Type targetType, IFormatProvider provider)
{
- return System.Convert.ToType (m_value, conversionType, provider);
+ if (targetType == null)
+ throw new ArgumentNullException ("targetType");
+ return System.Convert.ToType (m_value, targetType, provider, false);
}
ushort IConvertible.ToUInt16 (IFormatProvider provider)