diff options
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/Boolean.cs')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Boolean.cs | 108 |
1 files changed, 71 insertions, 37 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Boolean.cs b/src/System.Private.CoreLib/shared/System/Boolean.cs index fd56082f9..4daf6f3f9 100644 --- a/src/System.Private.CoreLib/shared/System/Boolean.cs +++ b/src/System.Private.CoreLib/shared/System/Boolean.cs @@ -12,7 +12,6 @@ ** ===========================================================*/ -using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.Versioning; @@ -20,12 +19,12 @@ namespace System { [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct Boolean : IComparable, IConvertible, IComparable<Boolean>, IEquatable<Boolean> + public readonly struct Boolean : IComparable, IConvertible, IComparable<bool>, IEquatable<bool> { // // Member Variables // - private bool m_value; // Do not rename (binary serialization) + private readonly bool m_value; // Do not rename (binary serialization) // The true value. // @@ -42,11 +41,11 @@ namespace System // The internal string representation of true. // - internal const String TrueLiteral = "True"; + internal const string TrueLiteral = "True"; // The internal string representation of false. // - internal const String FalseLiteral = "False"; + internal const string FalseLiteral = "False"; // @@ -55,11 +54,11 @@ namespace System // The public string representation of true. // - public static readonly String TrueString = TrueLiteral; + public static readonly string TrueString = TrueLiteral; // The public string representation of false. // - public static readonly String FalseString = FalseLiteral; + public static readonly string FalseString = FalseLiteral; // // Overriden Instance Methods @@ -82,7 +81,7 @@ namespace System **Exceptions: None. ==============================================================================*/ // Converts the boolean value of this instance to a String. - public override String ToString() + public override string ToString() { if (false == m_value) { @@ -91,41 +90,57 @@ namespace System return TrueLiteral; } - public String ToString(IFormatProvider provider) + public string ToString(IFormatProvider provider) { return ToString(); } public bool TryFormat(Span<char> destination, out int charsWritten) { - string s = m_value ? TrueLiteral : FalseLiteral; - - if (s.AsSpan().TryCopyTo(destination)) + if (m_value) { - charsWritten = s.Length; - return true; + if ((uint)destination.Length > 3) // uint cast, per https://github.com/dotnet/coreclr/issues/18688 + { + destination[0] = 'T'; + destination[1] = 'r'; + destination[2] = 'u'; + destination[3] = 'e'; + charsWritten = 4; + return true; + } } else { - charsWritten = 0; - return false; + if ((uint)destination.Length > 4) + { + destination[0] = 'F'; + destination[1] = 'a'; + destination[2] = 'l'; + destination[3] = 's'; + destination[4] = 'e'; + charsWritten = 5; + return true; + } } + + charsWritten = 0; + return false; } // Determines whether two Boolean objects are equal. - public override bool Equals(Object obj) + public override bool Equals(object obj) { //If it's not a boolean, we're definitely not equal - if (!(obj is Boolean)) + if (!(obj is bool)) { return false; } - return (m_value == ((Boolean)obj).m_value); + return (m_value == ((bool)obj).m_value); } [NonVersionable] - public bool Equals(Boolean obj) + public bool Equals(bool obj) { return m_value == obj; } @@ -137,18 +152,18 @@ namespace System // // Returns a value less than zero if this object // - public int CompareTo(Object obj) + public int CompareTo(object obj) { if (obj == null) { return 1; } - if (!(obj is Boolean)) + if (!(obj is bool)) { throw new ArgumentException(SR.Arg_MustBeBoolean); } - if (m_value == ((Boolean)obj).m_value) + if (m_value == ((bool)obj).m_value) { return 0; } @@ -159,7 +174,7 @@ namespace System return 1; } - public int CompareTo(Boolean value) + public int CompareTo(bool value) { if (m_value == value) { @@ -176,20 +191,41 @@ namespace System // Static Methods // + // Custom string compares for early application use by config switches, etc + // + internal static bool IsTrueStringIgnoreCase(ReadOnlySpan<char> value) + { + return (value.Length == 4 && + (value[0] == 't' || value[0] == 'T') && + (value[1] == 'r' || value[1] == 'R') && + (value[2] == 'u' || value[2] == 'U') && + (value[3] == 'e' || value[3] == 'E')); + } + + internal static bool IsFalseStringIgnoreCase(ReadOnlySpan<char> value) + { + return (value.Length == 5 && + (value[0] == 'f' || value[0] == 'F') && + (value[1] == 'a' || value[1] == 'A') && + (value[2] == 'l' || value[2] == 'L') && + (value[3] == 's' || value[3] == 'S') && + (value[4] == 'e' || value[4] == 'E')); + } + // Determines whether a String represents true or false. // - public static Boolean Parse(String value) + public static bool Parse(string value) { if (value == null) throw new ArgumentNullException(nameof(value)); return Parse(value.AsSpan()); } public static bool Parse(ReadOnlySpan<char> value) => - TryParse(value, out bool result) ? result : throw new FormatException(SR.Format_BadBoolean); + TryParse(value, out bool result) ? result : throw new FormatException(SR.Format(SR.Format_BadBoolean, new string(value))); // Determines whether a String represents true or false. // - public static Boolean TryParse(String value, out Boolean result) + public static bool TryParse(string value, out bool result) { if (value == null) { @@ -202,15 +238,13 @@ namespace System public static bool TryParse(ReadOnlySpan<char> value, out bool result) { - ReadOnlySpan<char> trueSpan = TrueLiteral.AsSpan(); - if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase)) + if (IsTrueStringIgnoreCase(value)) { result = true; return true; } - ReadOnlySpan<char> falseSpan = FalseLiteral.AsSpan(); - if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase)) + if (IsFalseStringIgnoreCase(value)) { result = false; return true; @@ -219,13 +253,13 @@ namespace System // Special case: Trim whitespace as well as null characters. value = TrimWhiteSpaceAndNull(value); - if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase)) + if (IsTrueStringIgnoreCase(value)) { result = true; return true; } - if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase)) + if (IsFalseStringIgnoreCase(value)) { result = false; return true; @@ -242,7 +276,7 @@ namespace System int start = 0; while (start < value.Length) { - if (!Char.IsWhiteSpace(value[start]) && value[start] != nullChar) + if (!char.IsWhiteSpace(value[start]) && value[start] != nullChar) { break; } @@ -252,7 +286,7 @@ namespace System int end = value.Length - 1; while (end >= start) { - if (!Char.IsWhiteSpace(value[end]) && value[end] != nullChar) + if (!char.IsWhiteSpace(value[end]) && value[end] != nullChar) { break; } @@ -332,7 +366,7 @@ namespace System return Convert.ToDouble(m_value); } - Decimal IConvertible.ToDecimal(IFormatProvider provider) + decimal IConvertible.ToDecimal(IFormatProvider provider) { return Convert.ToDecimal(m_value); } @@ -342,7 +376,7 @@ namespace System throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Boolean", "DateTime")); } - Object IConvertible.ToType(Type type, IFormatProvider provider) + object IConvertible.ToType(Type type, IFormatProvider provider) { return Convert.DefaultToType((IConvertible)this, type, provider); } |