SByte.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info)
  129. {
  130. Number.ParsingStatus status = Number.TryParseInt32(s, style, info, out int i);
  131. if (status != Number.ParsingStatus.OK)
  132. {
  133. Number.ThrowOverflowOrFormatException(status, TypeCode.SByte);
  134. }
  135. // For hex number styles AllowHexSpecifier >> 2 == 0x80 and cancels out MinValue so the check is effectively: (uint)i > byte.MaxValue
  136. // For integer styles it's zero and the effective check is (uint)(i - MinValue) > byte.MaxValue
  137. if ((uint)(i - MinValue - ((int)(style & NumberStyles.AllowHexSpecifier) >> 2)) > byte.MaxValue)
  138. {
  139. Number.ThrowOverflowException(TypeCode.SByte);
  140. }
  141. return (sbyte)i;
  142. }
  143. [CLSCompliant(false)]
  144. public static bool TryParse(string? s, out sbyte result)
  145. {
  146. if (s == null)
  147. {
  148. result = 0;
  149. return false;
  150. }
  151. return TryParse((ReadOnlySpan<char>)s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
  152. }
  153. [CLSCompliant(false)]
  154. public static bool TryParse(ReadOnlySpan<char> s, out sbyte result)
  155. {
  156. return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
  157. }
  158. [CLSCompliant(false)]
  159. public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out sbyte result)
  160. {
  161. NumberFormatInfo.ValidateParseStyleInteger(style);
  162. if (s == null)
  163. {
  164. result = 0;
  165. return false;
  166. }
  167. return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
  168. }
  169. [CLSCompliant(false)]
  170. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out sbyte result)
  171. {
  172. NumberFormatInfo.ValidateParseStyleInteger(style);
  173. return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
  174. }
  175. private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info, out sbyte result)
  176. {
  177. // For hex number styles AllowHexSpecifier >> 2 == 0x80 and cancels out MinValue so the check is effectively: (uint)i > byte.MaxValue
  178. // For integer styles it's zero and the effective check is (uint)(i - MinValue) > byte.MaxValue
  179. if (Number.TryParseInt32(s, style, info, out int i) != Number.ParsingStatus.OK
  180. || (uint)(i - MinValue - ((int)(style & NumberStyles.AllowHexSpecifier) >> 2)) > byte.MaxValue)
  181. {
  182. result = 0;
  183. return false;
  184. }
  185. result = (sbyte)i;
  186. return true;
  187. }
  188. //
  189. // IConvertible implementation
  190. //
  191. public TypeCode GetTypeCode()
  192. {
  193. return TypeCode.SByte;
  194. }
  195. bool IConvertible.ToBoolean(IFormatProvider? provider)
  196. {
  197. return Convert.ToBoolean(m_value);
  198. }
  199. char IConvertible.ToChar(IFormatProvider? provider)
  200. {
  201. return Convert.ToChar(m_value);
  202. }
  203. sbyte IConvertible.ToSByte(IFormatProvider? provider)
  204. {
  205. return m_value;
  206. }
  207. byte IConvertible.ToByte(IFormatProvider? provider)
  208. {
  209. return Convert.ToByte(m_value);
  210. }
  211. short IConvertible.ToInt16(IFormatProvider? provider)
  212. {
  213. return Convert.ToInt16(m_value);
  214. }
  215. ushort IConvertible.ToUInt16(IFormatProvider? provider)
  216. {
  217. return Convert.ToUInt16(m_value);
  218. }
  219. int IConvertible.ToInt32(IFormatProvider? provider)
  220. {
  221. return m_value;
  222. }
  223. uint IConvertible.ToUInt32(IFormatProvider? provider)
  224. {
  225. return Convert.ToUInt32(m_value);
  226. }
  227. long IConvertible.ToInt64(IFormatProvider? provider)
  228. {
  229. return Convert.ToInt64(m_value);
  230. }
  231. ulong IConvertible.ToUInt64(IFormatProvider? provider)
  232. {
  233. return Convert.ToUInt64(m_value);
  234. }
  235. float IConvertible.ToSingle(IFormatProvider? provider)
  236. {
  237. return Convert.ToSingle(m_value);
  238. }
  239. double IConvertible.ToDouble(IFormatProvider? provider)
  240. {
  241. return Convert.ToDouble(m_value);
  242. }
  243. decimal IConvertible.ToDecimal(IFormatProvider? provider)
  244. {
  245. return Convert.ToDecimal(m_value);
  246. }
  247. DateTime IConvertible.ToDateTime(IFormatProvider? provider)
  248. {
  249. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "SByte", "DateTime"));
  250. }
  251. object IConvertible.ToType(Type type, IFormatProvider? provider)
  252. {
  253. return Convert.DefaultToType((IConvertible)this, type, provider);
  254. }
  255. }
  256. }