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