From af937392f745ad7d776e4f709dbb6b8ca1ccb3d4 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 1 Mar 2018 20:43:07 +0000 Subject: [PATCH 1/6] StringValues Add IsNull, always set single item to _value --- .../StringValues.cs | 245 +++++------------- 1 file changed, 70 insertions(+), 175 deletions(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index d8da66d895a..f9360e21746 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -27,8 +27,23 @@ public StringValues(string value) public StringValues(string[] values) { - _value = null; - _values = values; + if (values == null || values.Length == 0) + { + // Set null and empty arrays to null + _value = null; + _values = null; + } + else if (values.Length == 1) + { + // Set single item arrays to the string value + _values = null; + _value = values[0]; + } + else + { + _value = null; + _values = values; + } } public static implicit operator StringValues(string value) @@ -53,6 +68,8 @@ public static implicit operator string[] (StringValues value) public int Count => _value != null ? 1 : (_values?.Length ?? 0); + public bool IsNull => _value == null && _values == null; + bool ICollection.IsReadOnly { get { return true; } @@ -80,29 +97,11 @@ public string this[int index] } } - public override string ToString() - { - return GetStringValue() ?? string.Empty; - } + public override string ToString() => GetStringValue() ?? string.Empty; - private string GetStringValue() - { - if (_values == null) - { - return _value; - } - switch (_values.Length) - { - case 0: return null; - case 1: return _values[0]; - default: return string.Join(",", _values); - } - } + private string GetStringValue() => (_values == null) ? _value : string.Join(",", _values); - public string[] ToArray() - { - return GetArrayValue() ?? EmptyArray; - } + public string[] ToArray() => GetArrayValue() ?? EmptyArray; private string[] GetArrayValue() { @@ -110,13 +109,11 @@ private string[] GetArrayValue() { return new[] { _value }; } + return _values; } - int IList.IndexOf(string item) - { - return IndexOf(item); - } + int IList.IndexOf(string item) => IndexOf(item); private int IndexOf(string item) { @@ -141,15 +138,9 @@ private int IndexOf(string item) return -1; } - bool ICollection.Contains(string item) - { - return IndexOf(item) >= 0; - } + bool ICollection.Contains(string item) => IndexOf(item) >= 0; - void ICollection.CopyTo(string[] array, int arrayIndex) - { - CopyTo(array, arrayIndex); - } + void ICollection.CopyTo(string[] array, int arrayIndex) => CopyTo(array, arrayIndex); private void CopyTo(string[] array, int arrayIndex) { @@ -179,45 +170,21 @@ private void CopyTo(string[] array, int arrayIndex) } } - void ICollection.Add(string item) - { - throw new NotSupportedException(); - } + void ICollection.Add(string item) => throw new NotSupportedException(); - void IList.Insert(int index, string item) - { - throw new NotSupportedException(); - } + void IList.Insert(int index, string item) => throw new NotSupportedException(); - bool ICollection.Remove(string item) - { - throw new NotSupportedException(); - } + bool ICollection.Remove(string item) => throw new NotSupportedException(); - void IList.RemoveAt(int index) - { - throw new NotSupportedException(); - } + void IList.RemoveAt(int index) => throw new NotSupportedException(); - void ICollection.Clear() - { - throw new NotSupportedException(); - } + void ICollection.Clear() => throw new NotSupportedException(); - public Enumerator GetEnumerator() - { - return new Enumerator(_values, _value); - } + public Enumerator GetEnumerator() => new Enumerator(_values, _value); - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public static bool IsNullOrEmpty(StringValues value) { @@ -225,29 +192,25 @@ public static bool IsNullOrEmpty(StringValues value) { return string.IsNullOrEmpty(value._value); } - switch (value._values.Length) - { - case 0: return true; - case 1: return string.IsNullOrEmpty(value._values[0]); - default: return false; - } + + return false; } public static StringValues Concat(StringValues values1, StringValues values2) { - var count1 = values1.Count; - var count2 = values2.Count; - if (count1 == 0) + if (values1.IsNull) { return values2; } - if (count2 == 0) + if (values2.IsNull) { return values1; } + var count1 = values1.Count; + var count2 = values2.Count; var combined = new string[count1 + count2]; values1.CopyTo(combined, 0); values2.CopyTo(combined, count1); @@ -261,12 +224,12 @@ public static StringValues Concat(in StringValues values, string value) return values; } - var count = values.Count; - if (count == 0) + if (values.IsNull) { return new StringValues(value); } + var count = values.Count; var combined = new string[count + 1]; values.CopyTo(combined, 0); combined[count] = value; @@ -280,12 +243,12 @@ public static StringValues Concat(string value, in StringValues values) return values; } - var count = values.Count; - if (count == 0) + if (values.IsNull) { return new StringValues(value); } + var count = values.Count; var combined = new string[count + 1]; combined[0] = value; values.CopyTo(combined, 1); @@ -295,7 +258,6 @@ public static StringValues Concat(string value, in StringValues values) public static bool Equals(StringValues left, StringValues right) { var count = left.Count; - if (count != right.Count) { return false; @@ -312,120 +274,58 @@ public static bool Equals(StringValues left, StringValues right) return true; } - public static bool operator ==(StringValues left, StringValues right) - { - return Equals(left, right); - } + public static bool operator ==(StringValues left, StringValues right) => Equals(left, right); - public static bool operator !=(StringValues left, StringValues right) - { - return !Equals(left, right); - } + public static bool operator !=(StringValues left, StringValues right) => !Equals(left, right); - public bool Equals(StringValues other) - { - return Equals(this, other); - } + public bool Equals(StringValues other) => Equals(this, other); - public static bool Equals(string left, StringValues right) - { - return Equals(new StringValues(left), right); - } + public static bool Equals(string left, StringValues right) => Equals(new StringValues(left), right); - public static bool Equals(StringValues left, string right) - { - return Equals(left, new StringValues(right)); - } + public static bool Equals(StringValues left, string right) => Equals(left, new StringValues(right)); - public bool Equals(string other) - { - return Equals(this, new StringValues(other)); - } + public bool Equals(string other) => Equals(this, new StringValues(other)); - public static bool Equals(string[] left, StringValues right) - { - return Equals(new StringValues(left), right); - } + public static bool Equals(string[] left, StringValues right) => Equals(new StringValues(left), right); - public static bool Equals(StringValues left, string[] right) - { - return Equals(left, new StringValues(right)); - } + public static bool Equals(StringValues left, string[] right) => Equals(left, new StringValues(right)); - public bool Equals(string[] other) - { - return Equals(this, new StringValues(other)); - } + public bool Equals(string[] other) => Equals(this, new StringValues(other)); - public static bool operator ==(StringValues left, string right) - { - return Equals(left, new StringValues(right)); - } + public static bool operator ==(StringValues left, string right) => Equals(left, new StringValues(right)); - public static bool operator !=(StringValues left, string right) - { - return !Equals(left, new StringValues(right)); - } + public static bool operator !=(StringValues left, string right) => !Equals(left, new StringValues(right)); - public static bool operator ==(string left, StringValues right) - { - return Equals(new StringValues(left), right); - } + public static bool operator ==(string left, StringValues right) => Equals(new StringValues(left), right); - public static bool operator !=(string left, StringValues right) - { - return !Equals(new StringValues(left), right); - } + public static bool operator !=(string left, StringValues right) => !Equals(new StringValues(left), right); - public static bool operator ==(StringValues left, string[] right) - { - return Equals(left, new StringValues(right)); - } + public static bool operator ==(StringValues left, string[] right) => Equals(left, new StringValues(right)); - public static bool operator !=(StringValues left, string[] right) - { - return !Equals(left, new StringValues(right)); - } + public static bool operator !=(StringValues left, string[] right) => !Equals(left, new StringValues(right)); - public static bool operator ==(string[] left, StringValues right) - { - return Equals(new StringValues(left), right); - } + public static bool operator ==(string[] left, StringValues right) => Equals(new StringValues(left), right); - public static bool operator !=(string[] left, StringValues right) - { - return !Equals(new StringValues(left), right); - } + public static bool operator !=(string[] left, StringValues right) => !Equals(new StringValues(left), right); - public static bool operator ==(StringValues left, object right) - { - return left.Equals(right); - } + public static bool operator ==(StringValues left, object right) => left.Equals(right); - public static bool operator !=(StringValues left, object right) - { - return !left.Equals(right); - } - public static bool operator ==(object left, StringValues right) - { - return right.Equals(left); - } + public static bool operator !=(StringValues left, object right) => !left.Equals(right); - public static bool operator !=(object left, StringValues right) - { - return !right.Equals(left); - } + public static bool operator ==(object left, StringValues right) => right.Equals(left); + + public static bool operator !=(object left, StringValues right) => !right.Equals(left); public override bool Equals(object obj) { if (obj == null) { - return Equals(this, StringValues.Empty); + return this.IsNull; } if (obj is string) { - return Equals(this, (string)obj); + return string.Equals(this._value, (string)obj, StringComparison.Ordinal); } if (obj is string[]) @@ -504,14 +404,9 @@ public bool MoveNext() object IEnumerator.Current => _current; - void IEnumerator.Reset() - { - throw new NotSupportedException(); - } + void IEnumerator.Reset() => throw new NotSupportedException(); - public void Dispose() - { - } + public void Dispose() { } } } } From e9d85673ca45768ec29435d87b5bff3784648997 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 15 Mar 2018 12:25:44 -0400 Subject: [PATCH 2/6] Revert formatting --- .../StringValues.cs | 188 ++++++++++++++---- 1 file changed, 147 insertions(+), 41 deletions(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index f9360e21746..dd0bfdef465 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -97,11 +97,17 @@ public string this[int index] } } - public override string ToString() => GetStringValue() ?? string.Empty; + public override string ToString() + { + return GetStringValue() ?? string.Empty; + } private string GetStringValue() => (_values == null) ? _value : string.Join(",", _values); - public string[] ToArray() => GetArrayValue() ?? EmptyArray; + public string[] ToArray() + { + return GetArrayValue() ?? EmptyArray; + } private string[] GetArrayValue() { @@ -109,11 +115,13 @@ private string[] GetArrayValue() { return new[] { _value }; } - return _values; } - int IList.IndexOf(string item) => IndexOf(item); + int IList.IndexOf(string item) + { + return IndexOf(item); + } private int IndexOf(string item) { @@ -138,9 +146,15 @@ private int IndexOf(string item) return -1; } - bool ICollection.Contains(string item) => IndexOf(item) >= 0; + bool ICollection.Contains(string item) + { + return IndexOf(item) >= 0; + } - void ICollection.CopyTo(string[] array, int arrayIndex) => CopyTo(array, arrayIndex); + void ICollection.CopyTo(string[] array, int arrayIndex) + { + CopyTo(array, arrayIndex); + } private void CopyTo(string[] array, int arrayIndex) { @@ -170,21 +184,45 @@ private void CopyTo(string[] array, int arrayIndex) } } - void ICollection.Add(string item) => throw new NotSupportedException(); + void ICollection.Add(string item) + { + throw new NotSupportedException(); + } - void IList.Insert(int index, string item) => throw new NotSupportedException(); + void IList.Insert(int index, string item) + { + throw new NotSupportedException(); + } - bool ICollection.Remove(string item) => throw new NotSupportedException(); + bool ICollection.Remove(string item) + { + throw new NotSupportedException(); + } - void IList.RemoveAt(int index) => throw new NotSupportedException(); + void IList.RemoveAt(int index) + { + throw new NotSupportedException(); + } - void ICollection.Clear() => throw new NotSupportedException(); + void ICollection.Clear() + { + throw new NotSupportedException(); + } - public Enumerator GetEnumerator() => new Enumerator(_values, _value); + public Enumerator GetEnumerator() + { + return new Enumerator(_values, _value); + } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } public static bool IsNullOrEmpty(StringValues value) { @@ -258,6 +296,7 @@ public static StringValues Concat(string value, in StringValues values) public static bool Equals(StringValues left, StringValues right) { var count = left.Count; + if (count != right.Count) { return false; @@ -274,47 +313,109 @@ public static bool Equals(StringValues left, StringValues right) return true; } - public static bool operator ==(StringValues left, StringValues right) => Equals(left, right); - - public static bool operator !=(StringValues left, StringValues right) => !Equals(left, right); + public static bool operator ==(StringValues left, StringValues right) + { + return Equals(left, right); + } - public bool Equals(StringValues other) => Equals(this, other); + public static bool operator !=(StringValues left, StringValues right) + { + return !Equals(left, right); + } - public static bool Equals(string left, StringValues right) => Equals(new StringValues(left), right); + public bool Equals(StringValues other) + { + return Equals(this, other); + } - public static bool Equals(StringValues left, string right) => Equals(left, new StringValues(right)); + public static bool Equals(string left, StringValues right) + { + return Equals(new StringValues(left), right); + } - public bool Equals(string other) => Equals(this, new StringValues(other)); + public static bool Equals(StringValues left, string right) + { + return Equals(left, new StringValues(right)); + } - public static bool Equals(string[] left, StringValues right) => Equals(new StringValues(left), right); + public bool Equals(string other) + { + return Equals(this, new StringValues(other)); + } - public static bool Equals(StringValues left, string[] right) => Equals(left, new StringValues(right)); + public static bool Equals(string[] left, StringValues right) + { + return Equals(new StringValues(left), right); + } - public bool Equals(string[] other) => Equals(this, new StringValues(other)); + public static bool Equals(StringValues left, string[] right) + { + return Equals(left, new StringValues(right)); + } - public static bool operator ==(StringValues left, string right) => Equals(left, new StringValues(right)); + public bool Equals(string[] other) + { + return Equals(this, new StringValues(other)); + } - public static bool operator !=(StringValues left, string right) => !Equals(left, new StringValues(right)); + public static bool operator ==(StringValues left, string right) + { + return Equals(left, new StringValues(right)); + } - public static bool operator ==(string left, StringValues right) => Equals(new StringValues(left), right); + public static bool operator !=(StringValues left, string right) + { + return !Equals(left, new StringValues(right)); + } - public static bool operator !=(string left, StringValues right) => !Equals(new StringValues(left), right); + public static bool operator ==(string left, StringValues right) + { + return Equals(new StringValues(left), right); + } - public static bool operator ==(StringValues left, string[] right) => Equals(left, new StringValues(right)); + public static bool operator !=(string left, StringValues right) + { + return !Equals(new StringValues(left), right); + } - public static bool operator !=(StringValues left, string[] right) => !Equals(left, new StringValues(right)); + public static bool operator ==(StringValues left, string[] right) + { + return Equals(left, new StringValues(right)); + } - public static bool operator ==(string[] left, StringValues right) => Equals(new StringValues(left), right); + public static bool operator !=(StringValues left, string[] right) + { + return !Equals(left, new StringValues(right)); + } - public static bool operator !=(string[] left, StringValues right) => !Equals(new StringValues(left), right); + public static bool operator ==(string[] left, StringValues right) + { + return Equals(new StringValues(left), right); + } - public static bool operator ==(StringValues left, object right) => left.Equals(right); + public static bool operator !=(string[] left, StringValues right) + { + return !Equals(new StringValues(left), right); + } - public static bool operator !=(StringValues left, object right) => !left.Equals(right); + public static bool operator ==(StringValues left, object right) + { + return left.Equals(right); + } - public static bool operator ==(object left, StringValues right) => right.Equals(left); + public static bool operator !=(StringValues left, object right) + { + return !left.Equals(right); + } + public static bool operator ==(object left, StringValues right) + { + return right.Equals(left); + } - public static bool operator !=(object left, StringValues right) => !right.Equals(left); + public static bool operator !=(object left, StringValues right) + { + return !right.Equals(left); + } public override bool Equals(object obj) { @@ -323,9 +424,9 @@ public override bool Equals(object obj) return this.IsNull; } - if (obj is string) + if (obj is string st) { - return string.Equals(this._value, (string)obj, StringComparison.Ordinal); + return this._value == st; } if (obj is string[]) @@ -404,9 +505,14 @@ public bool MoveNext() object IEnumerator.Current => _current; - void IEnumerator.Reset() => throw new NotSupportedException(); + void IEnumerator.Reset() + { + throw new NotSupportedException(); + } - public void Dispose() { } + public void Dispose() + { + } } } -} +} \ No newline at end of file From 74e788818522c5b977c22e6afcf15e8d86972dec Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 16 Mar 2018 17:35:25 -0400 Subject: [PATCH 3/6] Add comment --- src/Microsoft.Extensions.Primitives/StringValues.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index dd0bfdef465..855d818fcf1 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -30,8 +30,8 @@ public StringValues(string[] values) if (values == null || values.Length == 0) { // Set null and empty arrays to null - _value = null; - _values = null; + _value = null; // Both null is required for + _values = null; // IsNull property to work correctly } else if (values.Length == 1) { From 6a5e5f15b3409d78a0f23fdd14b7875e5392dd39 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sun, 18 Mar 2018 16:13:18 -0400 Subject: [PATCH 4/6] Remove nulls from arrays --- .../StringValues.cs | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index 855d818fcf1..81288318f71 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -41,8 +41,56 @@ public StringValues(string[] values) } else { + // Remove any nulls from the array else counts will be off + var nullCount = 0; + foreach (var value in values) + { + if (value == null) + { + nullCount++; + } + } + _value = null; - _values = values; + if (nullCount == 0) + { + // No nulls, just assign given array + _values = values; + } + else if (values.Length - nullCount == 0) + { + // No values, set to null so IsNull works + _values = null; + } + else if (values.Length - nullCount == 1) + { + // Only one value when nulls are removed + _values = null; // Set array to null + foreach (var value in values) + { + if (value != null) + { + // Set single value to _value + _value = value; + break; + } + } + } + else + { + // Are nulls, remove nulls from the array else counts will be off + var newValues = new string[values.Length - nullCount]; + var i = 0; + foreach (var value in values) + { + if (value != null) + { + newValues[i] = value; + i++; + } + } + _values = newValues; + } } } From fb8e5fedbd05b7586b06176fbf975c586e2bdff7 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 20 Mar 2018 00:55:53 +0000 Subject: [PATCH 5/6] Revert "Remove nulls from arrays" This reverts commit 6a5e5f15b3409d78a0f23fdd14b7875e5392dd39. --- .../StringValues.cs | 50 +------------------ 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index 81288318f71..855d818fcf1 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -41,56 +41,8 @@ public StringValues(string[] values) } else { - // Remove any nulls from the array else counts will be off - var nullCount = 0; - foreach (var value in values) - { - if (value == null) - { - nullCount++; - } - } - _value = null; - if (nullCount == 0) - { - // No nulls, just assign given array - _values = values; - } - else if (values.Length - nullCount == 0) - { - // No values, set to null so IsNull works - _values = null; - } - else if (values.Length - nullCount == 1) - { - // Only one value when nulls are removed - _values = null; // Set array to null - foreach (var value in values) - { - if (value != null) - { - // Set single value to _value - _value = value; - break; - } - } - } - else - { - // Are nulls, remove nulls from the array else counts will be off - var newValues = new string[values.Length - nullCount]; - var i = 0; - foreach (var value in values) - { - if (value != null) - { - newValues[i] = value; - i++; - } - } - _values = newValues; - } + _values = values; } } From 9152086d5857a8b0516e317c0e9699efe2ecac25 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 20 Mar 2018 19:11:23 +0000 Subject: [PATCH 6/6] feedback --- src/Microsoft.Extensions.Primitives/StringValues.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Extensions.Primitives/StringValues.cs b/src/Microsoft.Extensions.Primitives/StringValues.cs index 855d818fcf1..77af635b805 100644 --- a/src/Microsoft.Extensions.Primitives/StringValues.cs +++ b/src/Microsoft.Extensions.Primitives/StringValues.cs @@ -424,9 +424,9 @@ public override bool Equals(object obj) return this.IsNull; } - if (obj is string st) + if (obj is string str) { - return this._value == st; + return this._value == str; } if (obj is string[])