SByte.cs 11 KB

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