コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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());
        }
コード例 #7
0
            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
            }
コード例 #8
0
        public void Append_ThrowsIfValueIsNull()
        {
            var formatter = new InplaceStringBuilder(3);

            Assert.Throws <ArgumentNullException>(() => formatter.Append(null as string));
        }
コード例 #9
0
        public void Capacity_ThrowsIfNegativeValueSet()
        {
            var formatter = new InplaceStringBuilder(3);

            Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Capacity = -1);
        }
コード例 #10
0
        public void Append_ThrowIfValueLenghtMinusOffsetSmallerThanCount()
        {
            var formatter = new InplaceStringBuilder(3);

            Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Append("abc", 1, 3));
        }
コード例 #11
0
        public void Append_ThrowsIfOffsetIsNegative()
        {
            var formatter = new InplaceStringBuilder(3);

            Assert.Throws <ArgumentOutOfRangeException>(() => formatter.Append("abc", -1, 3));
        }