Example #1
0
        public FormattedStringBuilder Append(char value, Span span)
        {
            EnsureCapacity(Length + 1);

            span           = span ?? new Span();
            values[Length] = new CharSpan(value, span);
            Length        += 1;

            return(this);
        }
Example #2
0
        private void expandCapacity(int minimumCapacity)
        {
            int newCapacity = (Capacity + 1) * 2;

            if (minimumCapacity > newCapacity)
            {
                newCapacity = minimumCapacity;
            }

            var newArray = new CharSpan[newCapacity];

            Array.Copy(values, newArray, Length);
            values = newArray;
        }
        public FormattedString Replace(char oldChar, char newChar, Span newSpan)
        {
            CharSpan[] sAr = ToCharSpanArray();

            for (int i = 0; i < sAr.Length; i++)
            {
                if (sAr[i].Character == oldChar)
                {
                    sAr[i] = new CharSpan(newChar, newSpan ?? sAr[i].Span);
                }
            }

            return(MergeCharSpan(sAr));
        }
Example #4
0
        public FormattedStringBuilder Insert(int index, char value, Span span)
        {
            if (index < 0 || Length < index)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            EnsureCapacity(Length + 1);

            Array.Copy(values, index, values, index + 1, Length - index);

            span          = span ?? new Span();
            values[index] = new CharSpan(value, span);
            Length       += 1;

            return(this);
        }
        internal CharSpan[] ToCharSpanArray()
        {
            var ar    = new CharSpan[TextLength];
            int index = 0;

            for (int i = 0; i < spans.Length; i++)
            {
                Span span = spans[i];
                foreach (var c in span.Text)
                {
                    ar[index] = new CharSpan(c, span);
                    index++;
                }
            }
            return(ar);
        }
        public FormattedString Substring(int startIndex, int length)
        {
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex));
            }
            if (startIndex + length > TextLength)
            {
                throw new ArgumentOutOfRangeException($"{nameof(startIndex)} + {nameof(length)}");
            }

            CharSpan[] sAr = ToCharSpanArray();
            CharSpan[] nAr = new CharSpan[length];

            Array.Copy(sAr, startIndex, nAr, 0, length);

            return(MergeCharSpan(nAr));
        }
        private FormattedString trim(string trimedText)
        {
            if (trimedText.Length == 0)
            {
                return(Empty);
            }

            char startChar = trimedText[0];
            char endChar   = trimedText[trimedText.Length - 1];

            int startIndex = IndexOf(startChar);
            int endIndex   = LastIndexOf(endChar);

            CharSpan[] sAr = ToCharSpanArray();
            CharSpan[] nAr = new CharSpan[endIndex - startIndex + 1];

            Array.Copy(sAr, startIndex, nAr, 0, endIndex - startIndex + 1);

            return(MergeCharSpan(nAr));
        }
        internal FormattedString Insert(int startIndex, string value, Span span, SpanOperand operand)
        {
            if (startIndex < 0 || startIndex >= TextLength)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (value.Length == 0)
            {
                return(this);
            }

            CharSpan[] sAr = ToCharSpanArray();
            CharSpan[] nAr = new CharSpan[TextLength + value.Length];

            Span newSpan;

            if (operand == SpanOperand.Left && startIndex - 1 >= 0)
            {
                newSpan = sAr[startIndex - 1].Span;
            }
            else
            {
                newSpan = sAr[startIndex].Span;
            }

            CharSpan[] iAr = new CharSpan[value.Length];
            for (int i = 0; i < value.Length; i++)
            {
                iAr[i] = new CharSpan(value[i], newSpan);
            }

            Array.Copy(sAr, nAr, startIndex);
            Array.Copy(sAr, startIndex, nAr, startIndex + iAr.Length, sAr.Length - startIndex);
            Array.Copy(iAr, 0, nAr, startIndex, iAr.Length);

            return(MergeCharSpan(nAr));
        }
        public FormattedString Remove(int startIndex, int count)
        {
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (startIndex + count > TextLength)
            {
                throw new ArgumentOutOfRangeException($"{nameof(startIndex)} + {nameof(count)}");
            }

            CharSpan[] sAr = ToCharSpanArray();
            CharSpan[] nAr = new CharSpan[TextLength - count];

            Array.Copy(sAr, nAr, startIndex);
            Array.Copy(sAr, startIndex + count, nAr, startIndex, TextLength - startIndex - count);

            return(MergeCharSpan(nAr));
        }
        internal FormattedString Replace(string oldValue, string newValue, Span newSpan, SpanOperand operand)
        {
            if (oldValue == null)
            {
                throw new ArgumentNullException(nameof(oldValue));
            }
            if (oldValue.Length == 0)
            {
                throw new ArgumentException(nameof(oldValue));
            }
            if (newValue == null)
            {
                newValue = string.Empty;
            }

            int replaceCount = 0;
            int offset       = 0;

            while ((offset = Text.IndexOf(oldValue, offset)) != -1)
            {
                replaceCount++;
                offset += oldValue.Length;
            }

            CharSpan[] sAr = ToCharSpanArray();
            CharSpan[] nAr = new CharSpan[sAr.Length + (newValue.Length - oldValue.Length) * replaceCount];

            int oldIndex = 0;
            int newIndex = 0;

            for (int i = 0; i < replaceCount; i++)
            {
                int index    = Text.IndexOf(oldValue, oldIndex);
                int copySize = index - oldIndex;

                Array.Copy(sAr, oldIndex, nAr, newIndex, copySize);

                oldIndex += copySize + oldValue.Length;
                newIndex += copySize;

                if (newSpan == null && operand == SpanOperand.Left)
                {
                    newSpan = sAr[index].Span;
                }
                else if (newSpan == null)
                {
                    newSpan = sAr[index + oldValue.Length - 1].Span;
                }

                newSpan      = newSpan.Clone();
                newSpan.Text = newValue;
                CharSpan[] rAr = newSpan.ToArray();

                Array.Copy(rAr, 0, nAr, newIndex, rAr.Length);
                newIndex += rAr.Length;
            }

            Array.Copy(sAr, oldIndex, nAr, newIndex, TextLength - oldIndex);

            return(MergeCharSpan(nAr));
        }