示例#1
0
        /// <summary>
        /// Accumulates a replacement operation for a specified string span.
        /// </summary>
        /// <param name="span">The string span to replace.</param>
        /// <param name="newValue">The string to use as a replacement.</param>
        public void Replace(StringSpan span, string newValue)
        {
            if (newValue == null)
            {
                throw new ArgumentNullException(nameof(newValue));
            }

            ValidateSpan(span, _Value.Length);

            _ReplaceCore(span, newValue);
        }
示例#2
0
 /// <summary>
 /// Validates a string span.
 /// </summary>
 /// <param name="span">The span.</param>
 /// <param name="size">The string size.</param>
 static void ValidateSpan(StringSpan span, int size)
 {
     if (span.StartIndex < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(span), "Span start index cannot be less than zero.");
     }
     if (span.Length < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(span), "Span length cannot be less than zero.");
     }
     if (span.Length > size - span.StartIndex)
     {
         throw new ArgumentOutOfRangeException(nameof(span), "Span start index and length must refer to a location within the string.");
     }
 }
示例#3
0
        /// <summary>
        /// Replaces a specified string span with a new value.
        /// </summary>
        /// <param name="s">The string to edit.</param>
        /// <param name="span">The string span to replace.</param>
        /// <param name="newValue">The string to use as a replacement.</param>
        public static string Replace(string s, StringSpan span, string newValue)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }
            if (newValue == null)
            {
                throw new ArgumentNullException(nameof(newValue));
            }

            ValidateSpan(span, s.Length);

            return(_ReplaceCore(s, span, newValue));
        }
示例#4
0
        static string _ReplaceCore(string s, StringSpan span, string newValue)
        {
            int index  = span.StartIndex;
            int length = span.Length;

            if (length != 0)
            {
                s = s.Remove(index, length);
            }

            if (newValue.Length != 0)
            {
                s = s.Insert(index, newValue);
            }

            return(s);
        }
示例#5
0
        /// <summary>
        /// Returns a new string in which a specified span has been deleted.
        /// </summary>
        /// <param name="s">The string to edit.</param>
        /// <param name="span">The string span to remove.</param>
        /// <returns>A new string that is equivalent to <paramref name="s"/> except for the removed characters.</returns>
        public static string Remove(string s, StringSpan span)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            ValidateSpan(span, s.Length);

            if (span.Length == 0)
            {
                return(s);
            }
            else
            {
                return(s.Remove(span.StartIndex, span.Length));
            }
        }
示例#6
0
        void _ReplaceCore(StringSpan span, string newValue)
        {
            Operation compatibleOperation = null;

            foreach (var i in _Operations)
            {
                if (i.Span == span)
                {
                    compatibleOperation = i;
                    break;
                }
            }

            if (compatibleOperation != null)
            {
                compatibleOperation.NewValue = newValue;
            }
            else
            {
                _Operations.Add(new Operation(span, newValue));
            }
        }
示例#7
0
 public Operation(StringSpan span, string newValue)
 {
     Span     = span;
     NewValue = newValue;
 }
示例#8
0
 /// <summary>
 /// Accumulates a removal operation for a specified string span.
 /// </summary>
 /// <param name="span">The string span to remove.</param>
 public void Remove(StringSpan span) => Replace(span, string.Empty);