public void Replace(string input, string expected)
        {
            var manip  = new StringReplacement(5);
            var result = manip.Manipulate(input);

            Assert.NotNull(result);
            Assert.Equal(expected, result);
        }
        /// <summary>
        /// Add a dynamic search + replacement.
        ///
        /// The provided replacement function will be called to generate the replacement value for each
        /// occurrence of the search pattern.
        ///
        /// <see cref="StringReplacements"/> contains commonly used replacement functions
        /// </summary>
        /// <param name="pat">The pattern to search for</param>
        /// <param name="replacement">A function to generate the replacement value</param>
        /// <returns>this</returns>
        public SearchAndReplaceBuilder AddReplacement(IMatchable pat, StringReplacement replacement)
        {
            ClearMemos();
            int result = replacements.Count;

            replacements.Add(replacement);
            dfaBuilder.AddPattern(pat, result);
            return(this);
        }
Esempio n. 3
0
 /// <summary>
 /// Make a replacement that surrounds matches with a given prefix and suffix, and applies the given replacer
 /// to the match itself
 /// </summary>
 /// <param name="prefix">to put before matches</param>
 /// <param name="replacement">for the match itself</param>
 /// <param name="suffix">suffix to put after matches</param>
 /// <returns>new StringReplacement</returns>
 public static StringReplacement Surround(string prefix, StringReplacement replacement, string suffix)
 {
     return((dest, src, startPos, endPos) =>
     {
         dest.Append(prefix);
         var ret = replacement(dest, src, startPos, endPos);
         dest.Append(suffix);
         return ret;
     });
 }
Esempio n. 4
0
        public void StringReplacement_2()
        {
            const string referenceText = "من به همه گفتم من هستم اما او من نیست";
            const string finalText = "مهرداد به همه گفتم مهرداد هستم اما او مهرداد نیست";

            var stringRplc = new StringReplacement(referenceText);

            stringRplc.Replace("من", "مهرداد");

            Assert.AreEqual(finalText, stringRplc.Text);
        }
Esempio n. 5
0
        public void StringReplacement_3()
        {
            const string referenceText = "سلوم به تو ای همیشگی ترین انسان روی زمین";
            const string finalText = "سلام به تو ای همیشگی‌ترین انسان روی زمین";

            var stringRplc = new StringReplacement(referenceText);

            stringRplc.Replace(0, "سلوم".Length, "سلام");
            stringRplc.Replace(referenceText.IndexOf("همیشگی ترین"), "همیشگی ترین".Length, "همیشگی‌ترین");

            Assert.AreEqual(finalText, stringRplc.Text);
        }
Esempio n. 6
0
        public override Verb CreateVerb(string[] tokens)
        {
            var immediate = tokens[1] == "=:";

            Color(position, length, Structures);

            var parsers = new List <Parser>
            {
                new StringParser(),
                new InterpolatedStringParser2(),
                new AssignToAnsReplacementParser(),
                new ValueReplacementParser(),
                new PrintReplacementParser(),
                new BlockReplacementParser(),
                new AtReplacementParser(),
                new PushReplacementParser(),
                new AssignReplacementParser()
            };

            var index = position + length;

            foreach (var parser in parsers.Where(parser => parser.Scan(source, index)))
            {
                if (parser is IReplacementParser replacementParser)
                {
                    Replacement           = replacementParser.Replacement;
                    Replacement.Immediate = immediate;
                    overridePosition      = parser.Result.Position;
                    return(new NullOp());
                }

                var value = parser.Result.Value;
                if (value != null)
                {
                    switch (value.Type)
                    {
                    case ValueType.String:
                        Replacement = new StringReplacement((String)value);
                        break;

                    default:
                        return(null);
                    }

                    overridePosition = parser.Result.Position;
                    return(new NullOp());
                }
            }

            return(null);
        }
Esempio n. 7
0
        public void StringReplacement_1()
        {
            const string referenceText = "سلومی چوی بوی خوشگ آشنیی";
            const string finalText = "سلام چوی بویتهای خوش آشنایی";
            var stringRplc = new StringReplacement(referenceText);

            stringRplc.Replace(0, 5, "سلام");

            stringRplc.Replace(referenceText.IndexOf("بوی"), 3, "بویتهای");

            stringRplc.Replace(referenceText.IndexOf("خوشگ"), 4, "خوش");

            stringRplc.Replace(referenceText.IndexOf("آشنیی"), 5, "آشنایی");

            Assert.AreEqual(finalText, stringRplc.Text);
        }
Esempio n. 8
0
        public void StringReplacement_4()
        {
            const string referenceText =
            @"<div dir=""rtl"">
              <pre><font face=""Tahoma"">سلوم به <strong><em>تو</em></strong> ای <strong>همیشگی ترین</strong> انسان روی زمین
            </font></pre>
            </div>";
            const string finalText =
            @"<div dir=""rtl"">
              <pre><font face=""Tahoma"">خداحافظ به <strong><em>تو</em></strong> ای <strong>همیشگی‌ترین</strong> فرد روی ماه
            </font></pre>
            </div>";

            var stringRplc = new StringReplacement(referenceText);

            stringRplc.Replace(referenceText.IndexOf("سلوم"), "سلوم".Length, "خداحافظ");
            stringRplc.Replace(referenceText.IndexOf("همیشگی ترین"), "همیشگی ترین".Length, "همیشگی‌ترین");
            stringRplc.Replace(referenceText.IndexOf("انسان"), "انسان".Length, "فرد");
            stringRplc.Replace(referenceText.IndexOf("زمین"), "زمین".Length, "ماه");

            Assert.AreEqual(finalText, stringRplc.Text);
        }