SByte.cs 10 KB

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