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

            _rootSnippet.Append(snippet);
            ExtractTokens(snippet);
        }
 public void InsertAfter(int end, FastReplacerSnippet snippet)
 {
     InnerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = end,
         End     = end,
         Order1  = 1,
         Order2  = InnerSnippets.Count
     });
 }
 public void InsertBefore(int start, FastReplacerSnippet snippet)
 {
     InnerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = start,
         End     = start,
         Order1  = 2,
         Order2  = InnerSnippets.Count
     });
 }
 public void Replace(int start, int end, FastReplacerSnippet snippet)
 {
     InnerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = start,
         End     = end,
         Order1  = 0,
         Order2  = 0
     });
 }
 public void Append(FastReplacerSnippet snippet)
 {
     InnerSnippets.Add(new InnerSnippet
     {
         Snippet = snippet,
         Start   = this.Text.Length,
         End     = this.Text.Length,
         Order1  = 1,
         Order2  = InnerSnippets.Count
     });
 }
Ejemplo 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.Ordinal);
                if (start == -1)
                {
                    return;
                }
                int end = snippet.Text.IndexOf(_tokenClose, start + _tokenOpen.Length, StringComparison.Ordinal);
                if (end == -1) // closingTokenNotFound
                {
                    return;
                    //throw new ArgumentException(string.Format("Token is opened but not closed in text \"{0}\".", 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;

                // 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;
            }
        }
Ejemplo n.º 7
0
        /// <returns>Returns true if the token was found, false if nothing was replaced.</returns>
        public bool Replace(string token, string text)
        {
            List <TokenOccurrence> occurrences;

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