Пример #1
0
        public void Replace(RelatedLocation location, LazyString replacement)
        {
            // only tail replacing
            if (location.StartIndex < TailStartIndex)
            {
                return;
            }

            var tail = _workingText.Last.Value;

            var beforeLength = location.StartIndex - TailStartIndex;
            var afterLength  = tail.Length - beforeLength - location.Length;

            _workingText.RemoveLast();

            // tail_1_2_3 -> tail_1 + replacement + tail_3
            if (beforeLength > 0)
            {
                _workingText.Append(tail.Substring(0, beforeLength));
            }
            _workingText.Append(replacement);
            if (afterLength > 0)
            {
                _workingText.Append(tail.Substring(beforeLength + location.Length, afterLength));
            }

            TotalLegthCorrection.Value += replacement.Length - location.Length;
        }
Пример #2
0
        public void ShouldNotCallToStringBeforeToStrring()
        {
            var mock = new Mock <Func <string> >();

            var lazy = new LazyString(mock.Object);

            mock.Verify(f => f(), Times.Never());
        }
Пример #3
0
 public ActionOption(string prototype, LazyString description, int count, Action <OptionValueCollection> action)
     : base(prototype, description, count)
 {
     if (action == null)
     {
         throw new ArgumentNullException("action");
     }
     this.action = action;
 }
Пример #4
0
        public OptionSet Add(string prototype, LazyString description, OptionAction <string, string> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            Option p = new ActionOption(prototype, description, 2,
                                        delegate(OptionValueCollection v) { action(v [0], v [1]); });

            base.Add(p);
            return(this);
        }
Пример #5
0
        private string ParseLazyStringValue(LazyString lazyString)
        {
            if (lazyString.Value != null)
            {
                return(lazyString.Value);
            }

            int start = lazyString._indexStart;

            lazyString.Value = _protoAsText.Substring(start, lazyString._indexEnd - start);
            return(lazyString.Value);
        }
Пример #6
0
        protected Option(string prototype, LazyString description, int maxValueCount)
        {
            if (prototype == null)
            {
                throw new ArgumentNullException("prototype");
            }
            if (prototype.Length == 0)
            {
                throw new ArgumentException("Cannot be the empty string.", "prototype");
            }
            if (maxValueCount < 0)
            {
                throw new ArgumentOutOfRangeException("maxValueCount");
            }

            this.prototype   = prototype;
            this.names       = prototype.Split('|');
            this.description = description;
            this.count       = maxValueCount;
            this.type        = ParsePrototype();

            if (this.count == 0 && type != OptionValueType.None)
            {
                throw new ArgumentException(
                          "Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
                          "OptionValueType.Optional.",
                          "maxValueCount");
            }
            if (this.type == OptionValueType.None && maxValueCount > 1)
            {
                throw new ArgumentException(
                          string.Format("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
                          "maxValueCount");
            }
            if (Array.IndexOf(names, "<>") >= 0 &&
                ((names.Length == 1 && this.type != OptionValueType.None) ||
                 (names.Length > 1 && this.MaxValueCount > 1)))
            {
                throw new ArgumentException(
                          "The default option handler '<>' cannot require values.",
                          "prototype");
            }
        }
Пример #7
0
        public void Replace(RelatedLocation location, LazyString replacement)
        {
            // only tail replacing
            if (location.StartIndex < TailStartIndex) return;

            var tail = _workingText.Last.Value;

            var beforeLength = location.StartIndex - TailStartIndex;
            var afterLength = tail.Length - beforeLength - location.Length;

            _workingText.RemoveLast();

            // tail_1_2_3 -> tail_1 + replacement + tail_3
            if (beforeLength > 0) _workingText.Append(tail.Substring(0, beforeLength));
            _workingText.Append(replacement);
            if (afterLength > 0) _workingText.Append(tail.Substring(beforeLength + location.Length, afterLength));

            TotalLegthCorrection.Value += replacement.Length - location.Length;
        }
Пример #8
0
        private string ParseAttributeValue(LazyString lazyString)
        {
            if (lazyString.Value != null)
            {
                return(lazyString.Value);
            }

            int start = lazyString._indexStart + 1; // skip whitespace

            string value;

            if (_protoAsText[start] == '"')
            {
                value = _protoAsText.Substring(start + 1, lazyString._indexEnd - start - 2);
            }
            else
            {
                value = _protoAsText.Substring(start, lazyString._indexEnd - start);
            }

            lazyString.Value = value;
            return(value);
        }
Пример #9
0
        private bool AreEqual(LazyString lazyString, string anotherString)
        {
            int i      = 0;
            int j      = lazyString._indexStart;
            int length = anotherString.Length;

            if (lazyString._indexEnd - j != length)
            {
                return(false);
            }

            while (i < length)
            {
                if (anotherString[i] != _protoAsText[j])
                {
                    return(false);
                }

                i++;
                j++;
            }

            return(true);
        }
Пример #10
0
 protected Option(string prototype, LazyString description)
     : this(prototype, description, 1)
 {
 }
Пример #11
0
        public void Parse(string protoAsText)
        {
            _protoAsText = protoAsText;
            var lengthToProcess = protoAsText.Length;

            Stack <ProtoMessage4> messagesStack = new Stack <ProtoMessage4>();
            var currentProtoMessage             = this;

            List <LazyStringTuple> currentMessageAttributes = currentProtoMessage._attributes;
            List <LazyStringProtoMessage4Tuple> currentMessagSubMessages = currentProtoMessage._subMessages;

            var entryNameStartPosition = -1;

            LazyString?attributeName = null;

            var attributeValueStartPosition = 0;

            int char1 = 0;

            int isLineStart    = 0;
            int attributeValue = 1;

            var spaceMask = ~' ';

            for (int i = 0; i < lengthToProcess; i++)
            {
                char1 = protoAsText[i];

                switch ((attributeValue << 17) | char1)
                {
                // ' '				(attributeValue << 17) |' '	131104	int
                case 131104:
                    if (isLineStart == 0)
                    {
                        entryNameStartPosition = i;
                    }

                    break;

                // ':'		(attributeValue << 17) |':'	131130	int
                case 131130:
                    attributeName = new LazyString(entryNameStartPosition + 1, i);

                    attributeValueStartPosition = i + 1;
                    attributeValue = 0;

                    isLineStart = char1 & spaceMask;
                    break;

                // '{'		(attributeValue << 17) | '{'	131195	int
                case 131195:
                    messagesStack.Push(currentProtoMessage);
                    currentProtoMessage = new ProtoMessage4(protoAsText);
                    currentMessagSubMessages.Add(new LazyStringProtoMessage4Tuple(
                                                     new LazyString(entryNameStartPosition + 1, i - 1),
                                                     currentProtoMessage));

                    currentMessageAttributes = currentProtoMessage._attributes;
                    currentMessagSubMessages = currentProtoMessage._subMessages;

                    isLineStart = char1 & spaceMask;
                    break;

                // '}'      (attributeValue << 17) | '}'	131197	int
                case 131197:
                    currentProtoMessage      = messagesStack.Pop();
                    currentMessageAttributes = currentProtoMessage._attributes;
                    currentMessagSubMessages = currentProtoMessage._subMessages;

                    isLineStart = char1 & spaceMask;
                    break;

                // '\n'         10
                // '\n'         (attributeValue << 17) | '\n'	131082	int
                case 10:
                case 131082:
                    if (attributeName != null)
                    {
                        currentMessageAttributes.Add(new LazyStringTuple(attributeName.Value, new LazyString(attributeValueStartPosition, i)));
                        attributeName = null;
                    }

                    entryNameStartPosition = i;

                    isLineStart    = 0;
                    attributeValue = 1;

                    break;

                default:

                    isLineStart = char1 & spaceMask;
                    break;
                }
            }
        }
Пример #12
0
 public bool isEqual(LazyString lazyStringd)
 {
     return(lazyStringd._indexStart == _indexStart && lazyStringd._indexEnd == _indexEnd);
 }
Пример #13
0
 public TextReplacer(string text)
 {
     _workingText = new LazyString(text);
     TotalLegthCorrection = new Boxed<int>();
 }
Пример #14
0
 public LazyStringTuple(LazyString item1, LazyString item2)
 {
     Item1 = item1;
     Item2 = item2;
 }
Пример #15
0
 public TextReplacer(string text)
 {
     _workingText         = new LazyString(text);
     TotalLegthCorrection = new Boxed <int>();
 }
Пример #16
0
 public OptionSet Add <T> (string prototype, LazyString description, Action <T> action)
 {
     return(Add(new ActionOption <T> (prototype, description, action)));
 }
Пример #17
0
 public OptionSet Add <TKey, TValue> (string prototype, LazyString description, OptionAction <TKey, TValue> action)
 {
     return(Add(new ActionOption <TKey, TValue> (prototype, description, action)));
 }
Пример #18
0
 public LazyStringProtoMessage4Tuple(LazyString item1, ProtoMessage4 item2)
 {
     Item1 = item1;
     Item2 = item2;
 }