namespace System {
[Serializable]
- public struct Int16 : IComparable, IFormattable, IConvertible {
+ [System.Runtime.InteropServices.ComVisible (true)]
+ public struct Int16 : IFormattable, IConvertible, IComparable, IComparable<Int16>, IEquatable <Int16>
+ {
public const short MaxValue = 32767;
public const short MinValue = -32768;
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;
}
- public static short Parse (string s)
+ public int CompareTo (short value)
+ {
+ return m_value - value;
+ }
+
+ public bool Equals (short obj)
+ {
+ return obj == m_value;
+ }
+
+ internal static bool Parse (string s, bool tryParse, out short result, out Exception exc)
{
short val = 0;
int len;
int i, sign = 1;
bool digits_seen = false;
- if (s == null)
- throw new ArgumentNullException ("s");
+ result = 0;
+ exc = null;
+
+ if (s == null) {
+ if (!tryParse)
+ exc = new ArgumentNullException ("s");
+ return false;
+ }
len = s.Length;
break;
}
- if (i == len)
- 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]))
- throw new FormatException ();
+ 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
+ val = (short) ((val * 10) + d);
+
+ if (Int32.ProcessTrailingWhitespace (tryParse, s, i + 1, ref exc)){
+ result = val;
+ return true;
}
- break;
- } else
- throw new FormatException ();
- }
+ goto overflow;
+ } else
+ val = (short) (val * 10 + d);
+
+
+ digits_seen = true;
+ } else if (!Int32.ProcessTrailingWhitespace (tryParse, s, i, ref exc))
+ return false;
+
+ }
+ if (!digits_seen) {
+ if (!tryParse)
+ exc = Int32.GetFormatException ();
+ return false;
}
- if (!digits_seen)
- throw new FormatException ();
- return 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)
+ {
+ Exception exc;
+ short res;
+
+ if (!Parse (s, false, out res, out exc))
+ throw exc;
+
+ return res;
+ }
+
+ public static bool TryParse (string s, out short result)
+ {
+ Exception exc;
+ if (!Parse (s, true, out result, out exc)) {
+ result = 0;
+ return false;
+ }
+
+ return true;
+ }
+
+ 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;
}
public override string ToString ()
{
- return ToString (null, null);
+ return NumberFormatter.NumberToString (m_value, null);
}
- public string ToString (IFormatProvider fp)
+ public string ToString (IFormatProvider provider)
{
- return ToString (null, 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 );
-
- // use "G" when format is null or String.Empty
- if ((format == null) || (format.Length == 0))
- format = "G";
-
- return IntegerFormatter.NumberToString(format, nfi, m_value);
+ 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)