Ejemplo n.º 1
0
        /// <summary>
        /// Build a search and replace function
        ///
        /// The resulting function finds all patterns in the string you give it, and replaces them all with
        /// the associated replacement.
        ///
        /// Matches are found in order of their start positions.  If matches to more than one pattern occur at the same
        /// position, then the <i>longest</i> match will be used.  If there is a tie, then the first one added to this
        /// builder will be used.
        /// </summary>
        /// <returns>The search+replace function</returns>
        public Func <string, string> BuildStringReplacer()
        {
            if (dfaMemo == null)
            {
                dfaMemo = dfaBuilder.Build(AmbiguityResolver);
            }

            if (reverseFinderMemo == null)
            {
                reverseFinderMemo = dfaBuilder.BuildReverseFinder();
            }

            var searcher = new StringSearcher <int>(dfaMemo, reverseFinderMemo);
            var replacer = new StringSearcherReplacer(replacements).ReplacementSelector;

            return(str => searcher.FindAndReplace(str, replacer));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Build a search and replace function from a searcher and replacer
 /// </summary>
 /// <param name="searcher">the searcher</param>
 /// <param name="replacer">the replacer</param>
 /// <typeparam name="TResult"></typeparam>
 /// <returns>The search+replace function</returns>
 public static Func <string, string> BuildFromSearcher <TResult>(StringSearcher <TResult> searcher, ReplacementSelector <TResult> replacer)
 {
     return(str => searcher.FindAndReplace(str, replacer));
 }