private static bool TryParseBoolean(string s, CultureInfo culture, out object value) { var segment = new StringSegmentInternal(s); segment.Trim(); if (segment.EqualsOrdinalIgnoreCase(Boolean.FalseString)) { value = false; return(true); } if (segment.EqualsOrdinalIgnoreCase(Boolean.TrueString)) { value = true; return(true); } // allowing also an integer, which will be true for nonzero value if (segment.TryParseIntQuick(true, Int64.MaxValue, out ulong result)) { value = result != 0L; return(true); } value = null; return(false); }
internal override bool Equals(StringSegmentInternal x, string y) => x.EqualsOrdinalIgnoreCase(y);
private static bool TryParseKnownValueType <T>(string s, CultureInfo culture, out T value) { Debug.Assert(typeof(T).IsValueType, "T must be a value type so the branches can be optimized away by the JIT compiler"); // Important: // - Branches will be optimized away by JIT but only if we use typeof(SomeValueType) and not Reflector.XXXType // - In release build there will be no boxing for (T)(object)value and the JITted code will be much // simpler compared to the usually more elegant pattern matching if (typeof(T) == typeof(bool)) { var segment = new StringSegmentInternal(s); segment.Trim(); if (segment.EqualsOrdinalIgnoreCase(Boolean.FalseString)) { value = (T)(object)false; return(true); } if (segment.EqualsOrdinalIgnoreCase(Boolean.TrueString)) { value = (T)(object)true; return(true); } // allowing also an integer, which will be true for nonzero value if (segment.TryParseIntQuick(true, Int64.MaxValue, out ulong result)) { value = (T)(object)(result != 0L); return(true); } value = default; return(false); } if (typeof(T) == typeof(byte)) { if (Byte.TryParse(s, NumberStyles.Integer, culture, out byte result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(sbyte)) { if (SByte.TryParse(s, NumberStyles.Integer, culture, out sbyte result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(short)) { if (Int16.TryParse(s, NumberStyles.Integer, culture, out short result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(ushort)) { if (UInt16.TryParse(s, NumberStyles.Integer, culture, out ushort result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(int)) { if (Int32.TryParse(s, NumberStyles.Integer, culture, out int result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(uint)) { if (UInt32.TryParse(s, NumberStyles.Integer, culture, out uint result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(long)) { if (Int64.TryParse(s, NumberStyles.Integer, culture, out long result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(ulong)) { if (UInt64.TryParse(s, NumberStyles.Integer, culture, out ulong result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(IntPtr)) { if (Int64.TryParse(s, NumberStyles.Integer, culture, out long result)) { value = (T)(object)new IntPtr(result); return(true); } } if (typeof(T) == typeof(UIntPtr)) { if (UInt64.TryParse(s, NumberStyles.Integer, culture, out ulong result)) { value = (T)(object)new UIntPtr(result); return(true); } } if (typeof(T) == typeof(char)) { if (Char.TryParse(s, out char result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(float)) { if (Single.TryParse(s, floatStyle, culture, out float result)) { if (result.Equals(0f) && s.Trim().StartsWith(culture.NumberFormat.NegativeSign, StringComparison.Ordinal)) { result = FloatExtensions.NegativeZero; } value = (T)(object)result; return(true); } } if (typeof(T) == typeof(double)) { if (Double.TryParse(s, floatStyle, culture, out double result)) { if (result.Equals(0d) && s.Trim().StartsWith(culture.NumberFormat.NegativeSign, StringComparison.Ordinal)) { result = DoubleExtensions.NegativeZero; } value = (T)(object)result; return(true); } } if (typeof(T) == typeof(decimal)) { if (Decimal.TryParse(s, floatStyle, culture, out decimal result)) { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(TimeSpan)) { #if NET35 if (TimeSpan.TryParse(s, out TimeSpan result)) #else if (TimeSpan.TryParse(s, culture, out TimeSpan result)) #endif { value = (T)(object)result; return(true); } } if (typeof(T) == typeof(DateTime)) { s = s.TrimEnd(); if (s.Length > 0) { DateTimeStyles style = s[s.Length - 1] == 'Z' ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None; if (DateTime.TryParse(s, culture, style, out DateTime result)) { value = (T)(object)result; return(true); } } } if (typeof(T) == typeof(DateTimeOffset)) { s = s.TrimEnd(); if (s.Length > 0) { DateTimeStyles style = s[s.Length - 1] == 'Z' ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.None; if (DateTimeOffset.TryParse(s, culture, style, out DateTimeOffset result)) { value = (T)(object)result; return(true); } } } value = default; return(false); }