Commit 94d20d5e authored by Nick Craver's avatar Nick Craver

Cleanup: RedisValue

parent b854e4d7
...@@ -13,13 +13,9 @@ namespace StackExchange.Redis ...@@ -13,13 +13,9 @@ namespace StackExchange.Redis
public struct RedisValue : IEquatable<RedisValue>, IComparable<RedisValue>, IComparable, IConvertible public struct RedisValue : IEquatable<RedisValue>, IComparable<RedisValue>, IComparable, IConvertible
{ {
internal static readonly RedisValue[] EmptyArray = new RedisValue[0]; internal static readonly RedisValue[] EmptyArray = new RedisValue[0];
private static readonly byte[] EmptyByteArr = new byte[0];
static readonly byte[] EmptyByteArr = new byte[0];
private static readonly byte[] IntegerSentinel = new byte[0]; private static readonly byte[] IntegerSentinel = new byte[0];
private readonly byte[] valueBlob; private readonly byte[] valueBlob;
private readonly long valueInt64; private readonly long valueInt64;
// internal bool IsNullOrDefaultValue { get { return (valueBlob == null && valueInt64 == 0L) || ((object)valueBlob == (object)NullSentinel); } } // internal bool IsNullOrDefaultValue { get { return (valueBlob == null && valueInt64 == 0L) || ((object)valueBlob == (object)NullSentinel); } }
...@@ -57,19 +53,20 @@ private RedisValue(long valueInt64, byte[] valueBlob) ...@@ -57,19 +53,20 @@ private RedisValue(long valueInt64, byte[] valueBlob)
/// <summary> /// <summary>
/// Indicates whether the value is greater than zero-length /// Indicates whether the value is greater than zero-length
/// </summary> /// </summary>
public bool HasValue => valueBlob != null && valueBlob.Length > 0; public bool HasValue => valueBlob?.Length > 0;
/// <summary> /// <summary>
/// Indicates whether two RedisValue values are equivalent /// Indicates whether two RedisValue values are equivalent
/// </summary> /// </summary>
public static bool operator !=(RedisValue x, RedisValue y) /// <param name="x">The first <see cref="RedisValue"/> to compare.</param>
{ /// <param name="y">The second <see cref="RedisValue"/> to compare.</param>
return !(x == y); public static bool operator !=(RedisValue x, RedisValue y) => !(x == y);
}
/// <summary> /// <summary>
/// Indicates whether two RedisValue values are equivalent /// Indicates whether two RedisValue values are equivalent
/// </summary> /// </summary>
/// <param name="x">The first <see cref="RedisValue"/> to compare.</param>
/// <param name="y">The second <see cref="RedisValue"/> to compare.</param>
public static bool operator ==(RedisValue x, RedisValue y) public static bool operator ==(RedisValue x, RedisValue y)
{ {
if (x.valueBlob == null) return y.valueBlob == null; if (x.valueBlob == null) return y.valueBlob == null;
...@@ -96,6 +93,7 @@ private RedisValue(long valueInt64, byte[] valueBlob) ...@@ -96,6 +93,7 @@ private RedisValue(long valueInt64, byte[] valueBlob)
/// <summary> /// <summary>
/// See Object.Equals() /// See Object.Equals()
/// </summary> /// </summary>
/// <param name="obj">The other <see cref="RedisValue"/> to compare.</param>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj == null) return valueBlob == null; if (obj == null) return valueBlob == null;
...@@ -126,10 +124,8 @@ public override bool Equals(object obj) ...@@ -126,10 +124,8 @@ public override bool Equals(object obj)
/// <summary> /// <summary>
/// Indicates whether two RedisValue values are equivalent /// Indicates whether two RedisValue values are equivalent
/// </summary> /// </summary>
public bool Equals(RedisValue other) /// <param name="other">The <see cref="RedisValue"/> to compare to.</param>
{ public bool Equals(RedisValue other) => this == other;
return this == other;
}
/// <summary> /// <summary>
/// See Object.GetHashCode() /// See Object.GetHashCode()
...@@ -144,10 +140,7 @@ public override int GetHashCode() ...@@ -144,10 +140,7 @@ public override int GetHashCode()
/// <summary> /// <summary>
/// Returns a string representation of the value /// Returns a string representation of the value
/// </summary> /// </summary>
public override string ToString() public override string ToString() => (string)this;
{
return (string)this;
}
internal static unsafe bool Equals(byte[] x, byte[] y) internal static unsafe bool Equals(byte[] x, byte[] y)
{ {
...@@ -236,10 +229,12 @@ internal void AssertNotNull() ...@@ -236,10 +229,12 @@ internal void AssertNotNull()
if (IsNull) throw new ArgumentException("A null value is not valid in this context"); if (IsNull) throw new ArgumentException("A null value is not valid in this context");
} }
enum CompareType { private enum CompareType
{
Null, Int64, Double, Raw Null, Int64, Double, Raw
} }
CompareType ResolveType(out long i64, out double r8)
private CompareType ResolveType(out long i64, out double r8)
{ {
byte[] blob = valueBlob; byte[] blob = valueBlob;
if (blob == IntegerSentinel) if (blob == IntegerSentinel)
...@@ -248,18 +243,18 @@ CompareType ResolveType(out long i64, out double r8) ...@@ -248,18 +243,18 @@ CompareType ResolveType(out long i64, out double r8)
r8 = default(double); r8 = default(double);
return CompareType.Int64; return CompareType.Int64;
} }
if(blob == null) if (blob == null)
{ {
i64 = default(long); i64 = default(long);
r8 = default(double); r8 = default(double);
return CompareType.Null; return CompareType.Null;
} }
if(TryParseInt64(blob, 0, blob.Length, out i64)) if (TryParseInt64(blob, 0, blob.Length, out i64))
{ {
r8 = default(double); r8 = default(double);
return CompareType.Int64; return CompareType.Int64;
} }
if(TryParseDouble(blob, out r8)) if (TryParseDouble(blob, out r8))
{ {
i64 = default(long); i64 = default(long);
return CompareType.Double; return CompareType.Double;
...@@ -272,30 +267,29 @@ CompareType ResolveType(out long i64, out double r8) ...@@ -272,30 +267,29 @@ CompareType ResolveType(out long i64, out double r8)
/// <summary> /// <summary>
/// Compare against a RedisValue for relative order /// Compare against a RedisValue for relative order
/// </summary> /// </summary>
/// <param name="other">The other <see cref="RedisValue"/> to compare.</param>
public int CompareTo(RedisValue other) public int CompareTo(RedisValue other)
{ {
try try
{ {
long thisInt64, otherInt64; CompareType thisType = ResolveType(out long thisInt64, out double thisDouble),
double thisDouble, otherDouble; otherType = other.ResolveType(out long otherInt64, out double otherDouble);
CompareType thisType = this.ResolveType(out thisInt64, out thisDouble),
otherType = other.ResolveType(out otherInt64, out otherDouble);
if(thisType == CompareType.Null) if (thisType == CompareType.Null)
{ {
return otherType == CompareType.Null ? 0 : -1; return otherType == CompareType.Null ? 0 : -1;
} }
if(otherType == CompareType.Null) if (otherType == CompareType.Null)
{ {
return 1; return 1;
} }
if(thisType == CompareType.Int64) if (thisType == CompareType.Int64)
{ {
if (otherType == CompareType.Int64) return thisInt64.CompareTo(otherInt64); if (otherType == CompareType.Int64) return thisInt64.CompareTo(otherInt64);
if (otherType == CompareType.Double) return ((double)thisInt64).CompareTo(otherDouble); if (otherType == CompareType.Double) return ((double)thisInt64).CompareTo(otherDouble);
} }
else if(thisType == CompareType.Double) else if (thisType == CompareType.Double)
{ {
if (otherType == CompareType.Int64) return thisDouble.CompareTo((double)otherInt64); if (otherType == CompareType.Int64) return thisDouble.CompareTo((double)otherInt64);
if (otherType == CompareType.Double) return thisDouble.CompareTo(otherDouble); if (otherType == CompareType.Double) return thisDouble.CompareTo(otherDouble);
...@@ -327,53 +321,46 @@ int IComparable.CompareTo(object obj) ...@@ -327,53 +321,46 @@ int IComparable.CompareTo(object obj)
return -1; return -1;
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from an Int32 /// Creates a new <see cref="RedisValue"/> from an <see cref="int"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(int value) /// <param name="value">The <see cref="int"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(int value) => new RedisValue(value, IntegerSentinel);
return new RedisValue(value, IntegerSentinel);
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Int32 /// Creates a new <see cref="RedisValue"/> from an <see cref="T:Nullable{int}"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(int? value) /// <param name="value">The <see cref="T:Nullable{int}"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(int? value) => value == null ? Null : (RedisValue)value.GetValueOrDefault();
return value == null ? Null : (RedisValue)value.GetValueOrDefault();
}
/// <summary> /// <summary>
/// Creates a new RedisValue from an Int64 /// Creates a new <see cref="RedisValue"/> from an <see cref="long"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(long value) /// <param name="value">The <see cref="long"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(long value) => new RedisValue(value, IntegerSentinel);
return new RedisValue(value, IntegerSentinel);
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Int64 /// Creates a new <see cref="RedisValue"/> from an <see cref="T:Nullable{long}"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(long? value) /// <param name="value">The <see cref="T:Nullable{long}"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(long? value) => value == null ? Null : (RedisValue)value.GetValueOrDefault();
return value == null ? Null : (RedisValue)value.GetValueOrDefault();
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a Double /// Creates a new <see cref="RedisValue"/> from an <see cref="double"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(double value) /// <param name="value">The <see cref="double"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(double value) => Format.ToString(value);
return Format.ToString(value);
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Double /// Creates a new <see cref="RedisValue"/> from an <see cref="T:Nullable{double}"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(double? value) /// <param name="value">The <see cref="T:Nullable{double}"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(double? value) => value == null ? Null : (RedisValue)value.GetValueOrDefault();
return value == null ? Null : (RedisValue)value.GetValueOrDefault();
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a String /// Creates a new <see cref="RedisValue"/> from an <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="string"/> to convert to a <see cref="RedisValue"/>.</param>
public static implicit operator RedisValue(string value) public static implicit operator RedisValue(string value)
{ {
byte[] blob; byte[] blob;
...@@ -382,9 +369,11 @@ int IComparable.CompareTo(object obj) ...@@ -382,9 +369,11 @@ int IComparable.CompareTo(object obj)
else blob = Encoding.UTF8.GetBytes(value); else blob = Encoding.UTF8.GetBytes(value);
return new RedisValue(0, blob); return new RedisValue(0, blob);
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from a Byte[] /// Creates a new <see cref="RedisValue"/> from an <see cref="T:byte[]"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="T:byte[]"/> to convert to a <see cref="RedisValue"/>.</param>
public static implicit operator RedisValue(byte[] value) public static implicit operator RedisValue(byte[] value)
{ {
byte[] blob; byte[] blob;
...@@ -408,26 +397,26 @@ internal static RedisValue Parse(object obj) ...@@ -408,26 +397,26 @@ internal static RedisValue Parse(object obj)
throw new InvalidOperationException("Unable to format type for redis: " + obj.GetType().FullName); throw new InvalidOperationException("Unable to format type for redis: " + obj.GetType().FullName);
} }
/// <summary> /// <summary>
/// Creates a new RedisValue from a Boolean /// Creates a new <see cref="RedisValue"/> from an <see cref="bool"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(bool value) /// <param name="value">The <see cref="bool"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(bool value) => new RedisValue(value ? 1 : 0, IntegerSentinel);
return new RedisValue(value ? 1 : 0, IntegerSentinel);
}
/// <summary> /// <summary>
/// Creates a new RedisValue from a nullable Boolean /// Creates a new <see cref="RedisValue"/> from an <see cref="T:Nullable{bool}"/>.
/// </summary> /// </summary>
public static implicit operator RedisValue(bool? value) /// <param name="value">The <see cref="T:Nullable{bool}"/> to convert to a <see cref="RedisValue"/>.</param>
{ public static implicit operator RedisValue(bool? value) => value == null ? Null : (RedisValue)value.GetValueOrDefault();
return value == null ? Null : (RedisValue)value.GetValueOrDefault();
}
/// <summary> /// <summary>
/// Converts the value to a Boolean /// Converts a <see cref="RedisValue"/> to a <see cref="bool"/>.
/// </summary> /// </summary>
public static explicit operator bool (RedisValue value) /// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator bool(RedisValue value)
{ {
switch((long)value) switch ((long)value)
{ {
case 0: return false; case 0: return false;
case 1: return true; case 1: return true;
...@@ -436,8 +425,9 @@ internal static RedisValue Parse(object obj) ...@@ -436,8 +425,9 @@ internal static RedisValue Parse(object obj)
} }
/// <summary> /// <summary>
/// Converts the value to an Int32 /// Converts a <see cref="RedisValue"/> to a <see cref="int"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator int(RedisValue value) public static explicit operator int(RedisValue value)
{ {
checked checked
...@@ -445,34 +435,35 @@ internal static RedisValue Parse(object obj) ...@@ -445,34 +435,35 @@ internal static RedisValue Parse(object obj)
return (int)(long)value; return (int)(long)value;
} }
} }
/// <summary> /// <summary>
/// Converts the value to an Int64 /// Converts a <see cref="RedisValue"/> to a <see cref="long"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator long(RedisValue value) public static explicit operator long(RedisValue value)
{ {
var blob = value.valueBlob; var blob = value.valueBlob;
if (blob == IntegerSentinel) return value.valueInt64; if (blob == IntegerSentinel) return value.valueInt64;
if (blob == null) return 0; // in redis, an arithmetic zero is kinda the same thing as not-exists (think "incr") if (blob == null) return 0; // in redis, an arithmetic zero is kinda the same thing as not-exists (think "incr")
long i64; if (TryParseInt64(blob, 0, blob.Length, out long i64)) return i64;
if (TryParseInt64(blob, 0, blob.Length, out i64)) return i64;
throw new InvalidCastException(); throw new InvalidCastException();
} }
/// <summary> /// <summary>
/// Converts the value to a Double /// Converts a <see cref="RedisValue"/> to a <see cref="double"/>.
/// </summary> /// </summary>
public static explicit operator double (RedisValue value) /// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator double(RedisValue value)
{ {
var blob = value.valueBlob; var blob = value.valueBlob;
if (blob == IntegerSentinel) return value.valueInt64; if (blob == IntegerSentinel) return value.valueInt64;
if (blob == null) return 0; // in redis, an arithmetic zero is kinda the same thing as not-exists (think "incr") if (blob == null) return 0; // in redis, an arithmetic zero is kinda the same thing as not-exists (think "incr")
double r8; if (TryParseDouble(blob, out double r8)) return r8;
if (TryParseDouble(blob, out r8)) return r8;
throw new InvalidCastException(); throw new InvalidCastException();
} }
static bool TryParseDouble(byte[] blob, out double value) private static bool TryParseDouble(byte[] blob, out double value)
{ {
// simple integer? // simple integer?
if (blob.Length == 1 && blob[0] >= '0' && blob[0] <= '9') if (blob.Length == 1 && blob[0] >= '0' && blob[0] <= '9')
...@@ -485,32 +476,39 @@ static bool TryParseDouble(byte[] blob, out double value) ...@@ -485,32 +476,39 @@ static bool TryParseDouble(byte[] blob, out double value)
} }
/// <summary> /// <summary>
/// Converts the value to a nullable Double /// Converts the <see cref="RedisValue"/> to a <see cref="T:Nullable{double}"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator double? (RedisValue value) public static explicit operator double? (RedisValue value)
{ {
if (value.valueBlob == null) return null; if (value.valueBlob == null) return null;
return (double)value; return (double)value;
} }
/// <summary> /// <summary>
/// Converts the value to a nullable Int64 /// Converts the <see cref="RedisValue"/> to a <see cref="T:Nullable{long}"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator long? (RedisValue value) public static explicit operator long? (RedisValue value)
{ {
if (value.valueBlob == null) return null; if (value.valueBlob == null) return null;
return (long)value; return (long)value;
} }
/// <summary> /// <summary>
/// Converts the value to a nullable Int32 /// Converts the <see cref="RedisValue"/> to a <see cref="T:Nullable{int}"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator int? (RedisValue value) public static explicit operator int? (RedisValue value)
{ {
if (value.valueBlob == null) return null; if (value.valueBlob == null) return null;
return (int)value; return (int)value;
} }
/// <summary> /// <summary>
/// Converts the value to a nullable Boolean /// Converts the <see cref="RedisValue"/> to a <see cref="T:Nullable{bool}"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static explicit operator bool? (RedisValue value) public static explicit operator bool? (RedisValue value)
{ {
if (value.valueBlob == null) return null; if (value.valueBlob == null) return null;
...@@ -518,8 +516,9 @@ static bool TryParseDouble(byte[] blob, out double value) ...@@ -518,8 +516,9 @@ static bool TryParseDouble(byte[] blob, out double value)
} }
/// <summary> /// <summary>
/// Converts the value to a String /// Converts a <see cref="RedisValue"/> to a <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static implicit operator string(RedisValue value) public static implicit operator string(RedisValue value)
{ {
var valueBlob = value.valueBlob; var valueBlob = value.valueBlob;
...@@ -541,10 +540,12 @@ static bool TryParseDouble(byte[] blob, out double value) ...@@ -541,10 +540,12 @@ static bool TryParseDouble(byte[] blob, out double value)
return BitConverter.ToString(valueBlob); return BitConverter.ToString(valueBlob);
} }
} }
/// <summary> /// <summary>
/// Converts the value to a byte[] /// Converts a <see cref="RedisValue"/> to a <see cref="T:byte[]"/>.
/// </summary> /// </summary>
public static implicit operator byte[](RedisValue value) /// <param name="value">The <see cref="RedisValue"/> to convert.</param>
public static implicit operator byte[] (RedisValue value)
{ {
var valueBlob = value.valueBlob; var valueBlob = value.valueBlob;
if (valueBlob == IntegerSentinel) if (valueBlob == IntegerSentinel)
...@@ -557,35 +558,24 @@ static bool TryParseDouble(byte[] blob, out double value) ...@@ -557,35 +558,24 @@ static bool TryParseDouble(byte[] blob, out double value)
TypeCode IConvertible.GetTypeCode() => TypeCode.Object; TypeCode IConvertible.GetTypeCode() => TypeCode.Object;
bool IConvertible.ToBoolean(IFormatProvider provider) => (bool)this; bool IConvertible.ToBoolean(IFormatProvider provider) => (bool)this;
byte IConvertible.ToByte(IFormatProvider provider) => (byte)this; byte IConvertible.ToByte(IFormatProvider provider) => (byte)this;
char IConvertible.ToChar(IFormatProvider provider) => (char)this; char IConvertible.ToChar(IFormatProvider provider) => (char)this;
DateTime IConvertible.ToDateTime(IFormatProvider provider) => DateTime.Parse((string)this, provider); DateTime IConvertible.ToDateTime(IFormatProvider provider) => DateTime.Parse((string)this, provider);
decimal IConvertible.ToDecimal(IFormatProvider provider) => (decimal)this; decimal IConvertible.ToDecimal(IFormatProvider provider) => (decimal)this;
double IConvertible.ToDouble(IFormatProvider provider) => (double)this; double IConvertible.ToDouble(IFormatProvider provider) => (double)this;
short IConvertible.ToInt16(IFormatProvider provider) => (short)this; short IConvertible.ToInt16(IFormatProvider provider) => (short)this;
int IConvertible.ToInt32(IFormatProvider provider) => (int)this; int IConvertible.ToInt32(IFormatProvider provider) => (int)this;
long IConvertible.ToInt64(IFormatProvider provider) => (long)this; long IConvertible.ToInt64(IFormatProvider provider) => (long)this;
sbyte IConvertible.ToSByte(IFormatProvider provider) => (sbyte)this; sbyte IConvertible.ToSByte(IFormatProvider provider) => (sbyte)this;
float IConvertible.ToSingle(IFormatProvider provider) => (float)this; float IConvertible.ToSingle(IFormatProvider provider) => (float)this;
string IConvertible.ToString(IFormatProvider provider) => (string)this; string IConvertible.ToString(IFormatProvider provider) => (string)this;
object IConvertible.ToType(Type conversionType, IFormatProvider provider) object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{ {
if (conversionType== null) throw new ArgumentNullException(nameof(conversionType)); if (conversionType == null) throw new ArgumentNullException(nameof(conversionType));
if (conversionType== typeof(byte[])) return (byte[])this; if (conversionType == typeof(byte[])) return (byte[])this;
if (conversionType == typeof(RedisValue)) return this; if (conversionType == typeof(RedisValue)) return this;
switch(conversionType.GetTypeCode()) switch (conversionType.GetTypeCode())
{ {
case TypeCode.Boolean: return (bool)this; case TypeCode.Boolean: return (bool)this;
case TypeCode.Byte: return (byte)this; case TypeCode.Byte: return (byte)this;
...@@ -609,9 +599,7 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider) ...@@ -609,9 +599,7 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
} }
ushort IConvertible.ToUInt16(IFormatProvider provider) => (ushort)this; ushort IConvertible.ToUInt16(IFormatProvider provider) => (ushort)this;
uint IConvertible.ToUInt32(IFormatProvider provider) => (uint)this; uint IConvertible.ToUInt32(IFormatProvider provider) => (uint)this;
ulong IConvertible.ToUInt64(IFormatProvider provider) => (ulong)this; ulong IConvertible.ToUInt64(IFormatProvider provider) => (ulong)this;
/// <summary> /// <summary>
...@@ -619,6 +607,7 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider) ...@@ -619,6 +607,7 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider)
/// ///
/// Returns false otherwise. /// Returns false otherwise.
/// </summary> /// </summary>
/// <param name="val">The <see cref="long"/> value, if conversion was possible.</param>
public bool TryParse(out long val) public bool TryParse(out long val)
{ {
var blob = valueBlob; var blob = valueBlob;
...@@ -643,10 +632,10 @@ public bool TryParse(out long val) ...@@ -643,10 +632,10 @@ public bool TryParse(out long val)
/// ///
/// Returns false otherwise. /// Returns false otherwise.
/// </summary> /// </summary>
/// <param name="val">The <see cref="int"/> value, if conversion was possible.</param>
public bool TryParse(out int val) public bool TryParse(out int val)
{ {
long l; if (!TryParse(out long l) || l > int.MaxValue || l < int.MinValue)
if (!TryParse(out l) || l > int.MaxValue || l < int.MinValue)
{ {
val = 0; val = 0;
return false; return false;
...@@ -661,6 +650,7 @@ public bool TryParse(out int val) ...@@ -661,6 +650,7 @@ public bool TryParse(out int val)
/// ///
/// Returns false otherwise. /// Returns false otherwise.
/// </summary> /// </summary>
/// <param name="val">The <see cref="double"/> value, if conversion was possible.</param>
public bool TryParse(out double val) public bool TryParse(out double val)
{ {
var blob = valueBlob; var blob = valueBlob;
...@@ -686,8 +676,7 @@ internal static class ReflectionExtensions ...@@ -686,8 +676,7 @@ internal static class ReflectionExtensions
internal static TypeCode GetTypeCode(this Type type) internal static TypeCode GetTypeCode(this Type type)
{ {
if (type == null) return TypeCode.Empty; if (type == null) return TypeCode.Empty;
TypeCode result; if (typeCodeLookup.TryGetValue(type, out TypeCode result)) return result;
if (typeCodeLookup.TryGetValue(type, out result)) return result;
if (type.GetTypeInfo().IsEnum) if (type.GetTypeInfo().IsEnum)
{ {
...@@ -697,7 +686,7 @@ internal static TypeCode GetTypeCode(this Type type) ...@@ -697,7 +686,7 @@ internal static TypeCode GetTypeCode(this Type type)
return TypeCode.Object; return TypeCode.Object;
} }
static readonly Dictionary<Type, TypeCode> typeCodeLookup = new Dictionary<Type, TypeCode> private static readonly Dictionary<Type, TypeCode> typeCodeLookup = new Dictionary<Type, TypeCode>
{ {
{typeof(bool), TypeCode.Boolean }, {typeof(bool), TypeCode.Boolean },
{typeof(byte), TypeCode.Byte }, {typeof(byte), TypeCode.Byte },
...@@ -723,4 +712,4 @@ internal static TypeCode GetTypeCode(this Type type) ...@@ -723,4 +712,4 @@ internal static TypeCode GetTypeCode(this Type type)
} }
#endif #endif
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment