Int16.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Globalization;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace System
  9. {
  10. [Serializable]
  11. [StructLayout(LayoutKind.Sequential)]
  12. [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  13. public readonly struct Int16 : IComparable, IConvertible, IFormattable, IComparable<short>, IEquatable<short>, ISpanFormattable
  14. {
  15. private readonly short m_value; // Do not rename (binary serialization)
  16. public const short MaxValue = (short)0x7FFF;
  17. public const short MinValue = unchecked((short)0x8000);
  18. // Compares this object to another object, returning an integer that
  19. // indicates the relationship.
  20. // Returns a value less than zero if this object
  21. // null is considered to be less than any instance.
  22. // If object is not of type Int16, this method throws an ArgumentException.
  23. //
  24. public int CompareTo(object value)
  25. {
  26. if (value == null)
  27. {
  28. return 1;
  29. }
  30. if (value is short)
  31. {
  32. return m_value - ((short)value).m_value;
  33. }
  34. throw new ArgumentException(SR.Arg_MustBeInt16);
  35. }
  36. public int CompareTo(short value)
  37. {
  38. return m_value - value;
  39. }
  40. public override bool Equals(object obj)
  41. {
  42. if (!(obj is short))
  43. {
  44. return false;
  45. }
  46. return m_value == ((short)obj).m_value;
  47. }
  48. [NonVersionable]
  49. public bool Equals(short obj)
  50. {
  51. return m_value == obj;
  52. }
  53. // Returns a HashCode for the Int16
  54. public override int GetHashCode()
  55. {
  56. return ((int)((ushort)m_value) | (((int)m_value) << 16));
  57. }
  58. public override string ToString()
  59. {
  60. return Number.FormatInt32(m_value, null, null);
  61. }
  62. public string ToString(IFormatProvider provider)
  63. {
  64. return Number.FormatInt32(m_value, null, provider);
  65. }
  66. public string ToString(string format)
  67. {
  68. return ToString(format, null);
  69. }
  70. public string ToString(string format, IFormatProvider provider)
  71. {
  72. if (m_value < 0 && format != null && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
  73. {
  74. uint temp = (uint)(m_value & 0x0000FFFF);
  75. return Number.FormatUInt32(temp, format, provider);
  76. }
  77. return Number.FormatInt32(m_value, format, provider);
  78. }
  79. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
  80. {
  81. if (m_value < 0 && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
  82. {
  83. uint temp = (uint)(m_value & 0x0000FFFF);
  84. return Number.TryFormatUInt32(temp, format, provider, destination, out charsWritten);
  85. }
  86. return Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten);
  87. }
  88. public static short Parse(string s)
  89. {
  90. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  91. return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  92. }
  93. public static short Parse(string s, NumberStyles style)
  94. {
  95. NumberFormatInfo.ValidateParseStyleInteger(style);
  96. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  97. return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.CurrentInfo);
  98. }
  99. public static short Parse(string s, IFormatProvider provider)
  100. {
  101. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  102. return Parse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  103. }
  104. public static short Parse(string s, NumberStyles style, IFormatProvider provider)
  105. {
  106. NumberFormatInfo.ValidateParseStyleInteger(style);
  107. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  108. return Parse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider));
  109. }
  110. public static short Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
  111. {
  112. NumberFormatInfo.ValidateParseStyleInteger(style);
  113. return Parse(s, style, NumberFormatInfo.GetInstance(provider));
  114. }
  115. private static short Parse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info)
  116. {
  117. Number.ParsingStatus status = Number.TryParseInt32(s, style, info, out int i);
  118. if (status != Number.ParsingStatus.OK)
  119. {
  120. Number.ThrowOverflowOrFormatException(status, TypeCode.Int16);
  121. }
  122. // For hex number styles AllowHexSpecifier << 6 == 0x8000 and cancels out MinValue so the check is effectively: (uint)i > ushort.MaxValue
  123. // For integer styles it's zero and the effective check is (uint)(i - MinValue) > ushort.MaxValue
  124. if ((uint)(i - MinValue - ((int)(style & NumberStyles.AllowHexSpecifier) << 6)) > ushort.MaxValue)
  125. {
  126. Number.ThrowOverflowException(TypeCode.Int16);
  127. }
  128. return (short)i;
  129. }
  130. public static bool TryParse(string s, out short result)
  131. {
  132. if (s == null)
  133. {
  134. result = 0;
  135. return false;
  136. }
  137. return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
  138. }
  139. public static bool TryParse(ReadOnlySpan<char> s, out short result)
  140. {
  141. return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
  142. }
  143. public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out short result)
  144. {
  145. NumberFormatInfo.ValidateParseStyleInteger(style);
  146. if (s == null)
  147. {
  148. result = 0;
  149. return false;
  150. }
  151. return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
  152. }
  153. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out short result)
  154. {
  155. NumberFormatInfo.ValidateParseStyleInteger(style);
  156. return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
  157. }
  158. private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info, out short result)
  159. {
  160. // For hex number styles AllowHexSpecifier << 6 == 0x8000 and cancels out MinValue so the check is effectively: (uint)i > ushort.MaxValue
  161. // For integer styles it's zero and the effective check is (uint)(i - MinValue) > ushort.MaxValue
  162. if (Number.TryParseInt32(s, style, info, out int i) != Number.ParsingStatus.OK
  163. || (uint)(i - MinValue - ((int)(style & NumberStyles.AllowHexSpecifier) << 6)) > ushort.MaxValue)
  164. {
  165. result = 0;
  166. return false;
  167. }
  168. result = (short)i;
  169. return true;
  170. }
  171. //
  172. // IConvertible implementation
  173. //
  174. public TypeCode GetTypeCode()
  175. {
  176. return TypeCode.Int16;
  177. }
  178. bool IConvertible.ToBoolean(IFormatProvider provider)
  179. {
  180. return Convert.ToBoolean(m_value);
  181. }
  182. char IConvertible.ToChar(IFormatProvider provider)
  183. {
  184. return Convert.ToChar(m_value);
  185. }
  186. sbyte IConvertible.ToSByte(IFormatProvider provider)
  187. {
  188. return Convert.ToSByte(m_value);
  189. }
  190. byte IConvertible.ToByte(IFormatProvider provider)
  191. {
  192. return Convert.ToByte(m_value);
  193. }
  194. short IConvertible.ToInt16(IFormatProvider provider)
  195. {
  196. return m_value;
  197. }
  198. ushort IConvertible.ToUInt16(IFormatProvider provider)
  199. {
  200. return Convert.ToUInt16(m_value);
  201. }
  202. int IConvertible.ToInt32(IFormatProvider provider)
  203. {
  204. return Convert.ToInt32(m_value);
  205. }
  206. uint IConvertible.ToUInt32(IFormatProvider provider)
  207. {
  208. return Convert.ToUInt32(m_value);
  209. }
  210. long IConvertible.ToInt64(IFormatProvider provider)
  211. {
  212. return Convert.ToInt64(m_value);
  213. }
  214. ulong IConvertible.ToUInt64(IFormatProvider provider)
  215. {
  216. return Convert.ToUInt64(m_value);
  217. }
  218. float IConvertible.ToSingle(IFormatProvider provider)
  219. {
  220. return Convert.ToSingle(m_value);
  221. }
  222. double IConvertible.ToDouble(IFormatProvider provider)
  223. {
  224. return Convert.ToDouble(m_value);
  225. }
  226. decimal IConvertible.ToDecimal(IFormatProvider provider)
  227. {
  228. return Convert.ToDecimal(m_value);
  229. }
  230. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  231. {
  232. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int16", "DateTime"));
  233. }
  234. object IConvertible.ToType(Type type, IFormatProvider provider)
  235. {
  236. return Convert.DefaultToType((IConvertible)this, type, provider);
  237. }
  238. }
  239. }