2
0

Boolean.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. /*============================================================
  5. **
  6. **
  7. **
  8. ** Purpose: The boolean class serves as a wrapper for the primitive
  9. ** type boolean.
  10. **
  11. **
  12. ===========================================================*/
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.Versioning;
  15. namespace System
  16. {
  17. [Serializable]
  18. [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  19. public readonly struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool>
  20. {
  21. //
  22. // Member Variables
  23. //
  24. private readonly bool m_value; // Do not rename (binary serialization)
  25. // The true value.
  26. //
  27. internal const int True = 1;
  28. // The false value.
  29. //
  30. internal const int False = 0;
  31. //
  32. // Internal Constants are real consts for performance.
  33. //
  34. // The internal string representation of true.
  35. //
  36. internal const string TrueLiteral = "True";
  37. // The internal string representation of false.
  38. //
  39. internal const string FalseLiteral = "False";
  40. //
  41. // Public Constants
  42. //
  43. // The public string representation of true.
  44. //
  45. public static readonly string TrueString = TrueLiteral;
  46. // The public string representation of false.
  47. //
  48. public static readonly string FalseString = FalseLiteral;
  49. //
  50. // Overriden Instance Methods
  51. //
  52. /*=================================GetHashCode==================================
  53. **Args: None
  54. **Returns: 1 or 0 depending on whether this instance represents true or false.
  55. **Exceptions: None
  56. **Overriden From: Value
  57. ==============================================================================*/
  58. // Provides a hash code for this instance.
  59. public override int GetHashCode()
  60. {
  61. return (m_value) ? True : False;
  62. }
  63. /*===================================ToString===================================
  64. **Args: None
  65. **Returns: "True" or "False" depending on the state of the boolean.
  66. **Exceptions: None.
  67. ==============================================================================*/
  68. // Converts the boolean value of this instance to a String.
  69. public override string ToString()
  70. {
  71. if (false == m_value)
  72. {
  73. return FalseLiteral;
  74. }
  75. return TrueLiteral;
  76. }
  77. public string ToString(IFormatProvider provider)
  78. {
  79. return ToString();
  80. }
  81. public bool TryFormat(Span<char> destination, out int charsWritten)
  82. {
  83. if (m_value)
  84. {
  85. if ((uint)destination.Length > 3) // uint cast, per https://github.com/dotnet/coreclr/issues/18688
  86. {
  87. destination[0] = 'T';
  88. destination[1] = 'r';
  89. destination[2] = 'u';
  90. destination[3] = 'e';
  91. charsWritten = 4;
  92. return true;
  93. }
  94. }
  95. else
  96. {
  97. if ((uint)destination.Length > 4)
  98. {
  99. destination[0] = 'F';
  100. destination[1] = 'a';
  101. destination[2] = 'l';
  102. destination[3] = 's';
  103. destination[4] = 'e';
  104. charsWritten = 5;
  105. return true;
  106. }
  107. }
  108. charsWritten = 0;
  109. return false;
  110. }
  111. // Determines whether two Boolean objects are equal.
  112. public override bool Equals(object obj)
  113. {
  114. //If it's not a boolean, we're definitely not equal
  115. if (!(obj is bool))
  116. {
  117. return false;
  118. }
  119. return (m_value == ((bool)obj).m_value);
  120. }
  121. [NonVersionable]
  122. public bool Equals(bool obj)
  123. {
  124. return m_value == obj;
  125. }
  126. // Compares this object to another object, returning an integer that
  127. // indicates the relationship. For booleans, false sorts before true.
  128. // null is considered to be less than any instance.
  129. // If object is not of type boolean, this method throws an ArgumentException.
  130. //
  131. // Returns a value less than zero if this object
  132. //
  133. public int CompareTo(object obj)
  134. {
  135. if (obj == null)
  136. {
  137. return 1;
  138. }
  139. if (!(obj is bool))
  140. {
  141. throw new ArgumentException(SR.Arg_MustBeBoolean);
  142. }
  143. if (m_value == ((bool)obj).m_value)
  144. {
  145. return 0;
  146. }
  147. else if (m_value == false)
  148. {
  149. return -1;
  150. }
  151. return 1;
  152. }
  153. public int CompareTo(bool value)
  154. {
  155. if (m_value == value)
  156. {
  157. return 0;
  158. }
  159. else if (m_value == false)
  160. {
  161. return -1;
  162. }
  163. return 1;
  164. }
  165. //
  166. // Static Methods
  167. //
  168. // Custom string compares for early application use by config switches, etc
  169. //
  170. internal static bool IsTrueStringIgnoreCase(ReadOnlySpan<char> value)
  171. {
  172. return (value.Length == 4 &&
  173. (value[0] == 't' || value[0] == 'T') &&
  174. (value[1] == 'r' || value[1] == 'R') &&
  175. (value[2] == 'u' || value[2] == 'U') &&
  176. (value[3] == 'e' || value[3] == 'E'));
  177. }
  178. internal static bool IsFalseStringIgnoreCase(ReadOnlySpan<char> value)
  179. {
  180. return (value.Length == 5 &&
  181. (value[0] == 'f' || value[0] == 'F') &&
  182. (value[1] == 'a' || value[1] == 'A') &&
  183. (value[2] == 'l' || value[2] == 'L') &&
  184. (value[3] == 's' || value[3] == 'S') &&
  185. (value[4] == 'e' || value[4] == 'E'));
  186. }
  187. // Determines whether a String represents true or false.
  188. //
  189. public static bool Parse(string value)
  190. {
  191. if (value == null) throw new ArgumentNullException(nameof(value));
  192. return Parse(value.AsSpan());
  193. }
  194. public static bool Parse(ReadOnlySpan<char> value) =>
  195. TryParse(value, out bool result) ? result : throw new FormatException(SR.Format(SR.Format_BadBoolean, new string(value)));
  196. // Determines whether a String represents true or false.
  197. //
  198. public static bool TryParse(string value, out bool result)
  199. {
  200. if (value == null)
  201. {
  202. result = false;
  203. return false;
  204. }
  205. return TryParse(value.AsSpan(), out result);
  206. }
  207. public static bool TryParse(ReadOnlySpan<char> value, out bool result)
  208. {
  209. if (IsTrueStringIgnoreCase(value))
  210. {
  211. result = true;
  212. return true;
  213. }
  214. if (IsFalseStringIgnoreCase(value))
  215. {
  216. result = false;
  217. return true;
  218. }
  219. // Special case: Trim whitespace as well as null characters.
  220. value = TrimWhiteSpaceAndNull(value);
  221. if (IsTrueStringIgnoreCase(value))
  222. {
  223. result = true;
  224. return true;
  225. }
  226. if (IsFalseStringIgnoreCase(value))
  227. {
  228. result = false;
  229. return true;
  230. }
  231. result = false;
  232. return false;
  233. }
  234. private static ReadOnlySpan<char> TrimWhiteSpaceAndNull(ReadOnlySpan<char> value)
  235. {
  236. const char nullChar = (char)0x0000;
  237. int start = 0;
  238. while (start < value.Length)
  239. {
  240. if (!char.IsWhiteSpace(value[start]) && value[start] != nullChar)
  241. {
  242. break;
  243. }
  244. start++;
  245. }
  246. int end = value.Length - 1;
  247. while (end >= start)
  248. {
  249. if (!char.IsWhiteSpace(value[end]) && value[end] != nullChar)
  250. {
  251. break;
  252. }
  253. end--;
  254. }
  255. return value.Slice(start, end - start + 1);
  256. }
  257. //
  258. // IConvertible implementation
  259. //
  260. public TypeCode GetTypeCode()
  261. {
  262. return TypeCode.Boolean;
  263. }
  264. bool IConvertible.ToBoolean(IFormatProvider provider)
  265. {
  266. return m_value;
  267. }
  268. char IConvertible.ToChar(IFormatProvider provider)
  269. {
  270. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Boolean", "Char"));
  271. }
  272. sbyte IConvertible.ToSByte(IFormatProvider provider)
  273. {
  274. return Convert.ToSByte(m_value);
  275. }
  276. byte IConvertible.ToByte(IFormatProvider provider)
  277. {
  278. return Convert.ToByte(m_value);
  279. }
  280. short IConvertible.ToInt16(IFormatProvider provider)
  281. {
  282. return Convert.ToInt16(m_value);
  283. }
  284. ushort IConvertible.ToUInt16(IFormatProvider provider)
  285. {
  286. return Convert.ToUInt16(m_value);
  287. }
  288. int IConvertible.ToInt32(IFormatProvider provider)
  289. {
  290. return Convert.ToInt32(m_value);
  291. }
  292. uint IConvertible.ToUInt32(IFormatProvider provider)
  293. {
  294. return Convert.ToUInt32(m_value);
  295. }
  296. long IConvertible.ToInt64(IFormatProvider provider)
  297. {
  298. return Convert.ToInt64(m_value);
  299. }
  300. ulong IConvertible.ToUInt64(IFormatProvider provider)
  301. {
  302. return Convert.ToUInt64(m_value);
  303. }
  304. float IConvertible.ToSingle(IFormatProvider provider)
  305. {
  306. return Convert.ToSingle(m_value);
  307. }
  308. double IConvertible.ToDouble(IFormatProvider provider)
  309. {
  310. return Convert.ToDouble(m_value);
  311. }
  312. decimal IConvertible.ToDecimal(IFormatProvider provider)
  313. {
  314. return Convert.ToDecimal(m_value);
  315. }
  316. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  317. {
  318. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Boolean", "DateTime"));
  319. }
  320. object IConvertible.ToType(Type type, IFormatProvider provider)
  321. {
  322. return Convert.DefaultToType((IConvertible)this, type, provider);
  323. }
  324. }
  325. }