public void Append_ThrowsIfNotEnoughSpace() { var formatter = new InplaceStringBuilder(1); var exception = Assert.Throws <InvalidOperationException>(() => formatter.Append("123")); Assert.Equal("Not enough capacity to write '3' characters, only '1' left.", exception.Message); }
public void Build_ThrowsIfNotEnoughWritten() { var formatter = new InplaceStringBuilder(5); formatter.Append("123"); var exception = Assert.Throws <InvalidOperationException>(() => formatter.ToString()); Assert.Equal("Entire reserved capacity was not used. Capacity: '5', written '3'.", exception.Message); }
public void Capacity_ThrowsIfAppendWasCalled() { var formatter = new InplaceStringBuilder(3); formatter.Append("123"); var exception = Assert.Throws <InvalidOperationException>(() => formatter.Capacity = 5); Assert.Equal("Cannot change capacity after write started.", exception.Message); }
public void ToString_ThrowsIfCapacityNotUsed() { var formatter = new InplaceStringBuilder(10); formatter.Append("abc"); var exception = Assert.Throws <InvalidOperationException>(() => formatter.ToString()); Assert.Equal("Entire reserved capacity was not used. Capacity: '10', written '3'.", exception.Message); }
public void Capacity_GetReturnsCorrectValue() { var formatter = new InplaceStringBuilder(3); Assert.Equal(3, formatter.Capacity); formatter.Capacity = 10; Assert.Equal(10, formatter.Capacity); formatter.Append("abc"); Assert.Equal(10, formatter.Capacity); }
public void ToString_ReturnsStringWithAllAppendedValues() { var s1 = "123"; var c1 = '4'; var s2 = "56789"; var seg = new StringSegment("890123", 2, 2); var formatter = new InplaceStringBuilder(); formatter.Capacity += s1.Length + 1 + s2.Length + seg.Length; formatter.Append(s1); formatter.Append(c1); formatter.Append(s2, 0, 2); formatter.Append(s2, 2, 2); formatter.Append(s2, 4, 1); formatter.Append(seg); Assert.Equal("12345678901", formatter.ToString()); }
static string GetJoinedStringValueFromArray(string[] values) { // Calculate final length int length = 0; for (int i = 0; i < values.Length; i++) { string value = values[i]; // Skip null and empty values if (value != null && value.Length > 0) { if (length > 0) { // Add seperator length++; } length += value.Length; } } #if NETCOREAPP || NETSTANDARD2_1 // Create the new string return(string.Create(length, values, (span, strings) => { int offset = 0; // Skip null and empty values for (int i = 0; i < strings.Length; i++) { string value = strings[i]; if (value != null && value.Length > 0) { if (offset > 0) { // Add seperator span[offset] = ','; offset++; } value.AsSpan().CopyTo(span.Slice(offset)); offset += value.Length; } } })); #else #pragma warning disable CS0618 var sb = new InplaceStringBuilder(length); #pragma warning restore CS0618 bool hasAdded = false; // Skip null and empty values for (int i = 0; i < values.Length; i++) { string value = values[i]; if (value != null && value.Length > 0) { if (hasAdded) { // Add seperator sb.Append(','); } sb.Append(value); hasAdded = true; } } return(sb.ToString()); #endif }
public void Append_ThrowsIfValueIsNull() { var formatter = new InplaceStringBuilder(3); Assert.Throws <ArgumentNullException>(() => formatter.Append(null as string)); }
public void Capacity_ThrowsIfNegativeValueSet() { var formatter = new InplaceStringBuilder(3); Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Capacity = -1); }
public void Append_ThrowIfValueLenghtMinusOffsetSmallerThanCount() { var formatter = new InplaceStringBuilder(3); Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Append("abc", 1, 3)); }
public void Append_ThrowsIfOffsetIsNegative() { var formatter = new InplaceStringBuilder(3); Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Append("abc", -1, 3)); }