Example #1
0
        public void RepositionTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("[a-z0-9]+ +[a-z0-9]+"), (dest, src, s, e) =>
            {
                for (e = s; src[e] != ' '; ++e)
                {
                }

                dest.Append(src, s, e).Append(", ");
                for (; src[e] == ' '; ++e)
                {
                }

                return(e);
            });
            var replacer = builder.BuildStringReplacer();

            var instr = " one two  three   four five ";
            var want  = " one, two, three, four, five ";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #2
0
        private static Func <string, string> CreateStringReplacer()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(DfaLex.Pattern.Regex(Pattern), (dest, srcStr, s, e) => 0);
            return(builder.BuildStringReplacer());
        }
Example #3
0
        private int TimeSearchAndReplaceBuilder(string src, string patString)
        {
            Func <string, string> replacer;
            {
                var builder = new SearchAndReplaceBuilder();
                builder.AddReplacement(Pattern.Regex(patString), (dest, srcStr, s, e) => 0);
                replacer = builder.BuildStringReplacer();
            }

            var count     = 0;
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            var str = src;

            for (var t = stopWatch.ElapsedMilliseconds; t < SpinUp + 1000; t = stopWatch.ElapsedMilliseconds)
            {
                str = replacer(str);
                if (t >= SpinUp)
                {
                    ++count;
                }
            }

            Assert.Equal(src, str);
            return(count);
        }
Example #4
0
        public void CrazyWontonTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("(<name>)"), StringReplacements.Delete);
            var replacer = builder.BuildStringReplacer();
            var instr    = ReadResource("SearcherTestInput2.txt");
            var have     = replacer(instr);
            var want     = instr.Replace("<name>", string.Empty).Replace("<NAME>", string.Empty);

            Assert.Equal(want, have);
        }
Example #5
0
        public void ReplacementSurroundTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("[a-zA-z]*"), StringReplacements.Surround("(", StringReplacements.ToUpper, ")"));
            var replacer = builder.BuildStringReplacer();

            var instr = " one two  three   four five ";
            var want  = " (ONE) (TWO)  (THREE)   (FOUR) (FIVE) ";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #6
0
        public void ReplacementStringTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("[a-zA-z]*"), StringReplacements.String("x"));
            var replacer = builder.BuildStringReplacer();

            var instr = " one two  three   four five ";
            var want  = " x x  x   x x ";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #7
0
        public void ReplacementSpaceOrNewlineTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("[\0- ]+"), StringReplacements.SpaceOrNewline);
            var replacer = builder.BuildStringReplacer();

            var instr = "    one \n two\r\n\r\nthree  \t four\n\n\nfive ";
            var want  = " one\ntwo\nthree four\nfive ";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #8
0
        public void ReplacementCaseTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("u[a-zA-z]*"), StringReplacements.ToUpper);
            builder.AddReplacement(Pattern.RegexI("l[a-zA-z]*"), StringReplacements.ToLower);
            var replacer = builder.BuildStringReplacer();

            var instr = "lAbCd uAbCd";
            var want  = "labcd UABCD";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #9
0
        public void ReplacementDeleteIgnoreTest()
        {
            var builder = new SearchAndReplaceBuilder();

            builder.AddReplacement(Pattern.RegexI("three"), StringReplacements.Ignore);
            builder.AddReplacement(Pattern.RegexI("[a-z0-9]+"), StringReplacements.Delete);
            var replacer = builder.BuildStringReplacer();

            var instr = " one two  three   four five ";
            var want  = "    three     ";
            var have  = replacer(instr);

            Assert.Equal(want, have);
        }
Example #10
0
        public void TestReplaceFunc()
        {
            var builder = new SearchAndReplaceBuilder();

            foreach (JavaToken tok in Enum.GetValues(typeof(JavaToken)))
            {
                builder.AddReplacement(tok.Pattern(), (dest, src, s, e) => TokenReplace(dest, tok, src, s, e));
            }

            var replacer = builder.BuildStringReplacer();
            var instr    = ReadResource("SearcherTestInput.txt");
            var want     = ReadResource("SearcherTestOutput.txt");
            var have     = replacer(instr);

            Assert.Equal(want, have);
        }