Exemplo n.º 1
0
        public void Append(string text)
        {
            var snippet = new FastReplacerSnippet(text);

            _rootSnippet.Append(snippet);
            ExtractTokens(snippet);
        }
Exemplo n.º 2
0
 public void Replace(int start, int end, FastReplacerSnippet snippet)
 {
     _innerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = start,
         End     = end,
         Order1  = 0,
         Order2  = 0
     });
 }
Exemplo n.º 3
0
 public void InsertBefore(int start, FastReplacerSnippet snippet)
 {
     _innerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = start,
         End     = start,
         Order1  = 2,
         Order2  = _innerSnippets.Count
     });
 }
Exemplo n.º 4
0
 public void InsertAfter(int end, FastReplacerSnippet snippet)
 {
     _innerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = end,
         End     = end,
         Order1  = 1,
         Order2  = _innerSnippets.Count
     });
 }
Exemplo n.º 5
0
 public void Append(FastReplacerSnippet snippet)
 {
     _innerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = Text.Length,
         End     = Text.Length,
         Order1  = 1,
         Order2  = _innerSnippets.Count
     });
 }
Exemplo n.º 6
0
        private void ExtractTokens(FastReplacerSnippet snippet)
        {
            int last = 0;

            while (last < snippet.Text.Length)
            {
                // Find next token position in snippet.Text:
                int start = snippet.Text.IndexOf(_tokenOpen, last, StringComparison.InvariantCultureIgnoreCase);
                if (start == -1)
                {
                    return;
                }
                int end = snippet.Text.IndexOf(_tokenClose, start + _tokenOpen.Length, StringComparison.InvariantCultureIgnoreCase);
                if (end == -1)
                {
                    throw new ArgumentException($"Token is opened but not closed in text \"{snippet.Text}\".");
                }
                int eol = snippet.Text.IndexOf('\n', start + _tokenOpen.Length);
                if (eol != -1 && eol < end)
                {
                    last = eol + 1;
                    continue;
                }

                // Take the token from snippet.Text:
                end += _tokenClose.Length;
                string token   = snippet.Text.Substring(start, end - start);
                string context = snippet.Text;
                ValidateToken(token, context, true);

                // Add the token to the dictionary:
                var tokenOccurrence = new TokenOccurrence {
                    Snippet = snippet, Start = start, End = end
                };
                List <TokenOccurrence> occurrences;
                if (_occurrencesOfToken.TryGetValue(token, out occurrences))
                {
                    occurrences.Add(tokenOccurrence);
                }
                else
                {
                    _occurrencesOfToken.Add(token, new List <TokenOccurrence> {
                        tokenOccurrence
                    });
                }

                last = end;
            }
        }
Exemplo n.º 7
0
        /// <returns>Returns true if the token was found, false if nothing was replaced.</returns>
        public bool InsertBefore(string token, string text)
        {
            ValidateToken(token, text, false);
            List <TokenOccurrence> occurrences;

            if (_occurrencesOfToken.TryGetValue(token, out occurrences) && occurrences.Count > 0)
            {
                var snippet = new FastReplacerSnippet(text);
                foreach (var occurrence in occurrences)
                {
                    occurrence.Snippet.InsertBefore(occurrence.Start, snippet);
                }
                ExtractTokens(snippet);
                return(true);
            }
            return(false);
        }