示例#1
0
        static void Main()
        {
            var sw = new Stopwatch();

            sw.Restart();
            var txt = new TextFile(@"C:\Temp\B1.html");
            var str = txt.ReadText();

            Console.WriteLine("new txt, read" + sw.ElapsedTicks);

            sw.Restart();
            var s = new TextReplacer(str);

            Console.WriteLine("new TextReplacer" + sw.ElapsedTicks);

            sw.Restart();
            var pat   = "<span(.+?)</span>";    //"#bookmark(\\d+?\")(.+?)bookmark\\1";//"<span(.+?)</span>";
            var regex = new RegexProcessor(pat);
            var rep   = new Replacement(@"\1"); //(@"#b\1\2b\1");//(@"12345\1=234567");

            Console.WriteLine("new regex/repl" + sw.ElapsedTicks);

            sw.Restart();
            var matches = regex.RelatedMatches(str, s);

            Console.WriteLine("matches" + sw.ElapsedTicks + " in ms:" + sw.Elapsed);

            sw.Reset();
            var sw1 = new Stopwatch();

            foreach (var match in matches)
            {
                sw.Start();
                var repls = rep.CreateCopyWithGroups(match);
                sw.Stop();

                sw1.Start();
                s.Replace(match, repls);
                sw1.Stop();
            }
            Console.WriteLine("replacement build " + sw.ElapsedTicks);
            Console.WriteLine("text replace" + sw1.ElapsedTicks);

            sw.Restart();
            var res = s.BuildResult();

            sw.Stop();
            Console.WriteLine("сборка" + sw.ElapsedTicks);
            Console.ReadLine();
        }
示例#2
0
        public void Run(CancellationToken cancellationToken)
        {
            lock (_busy)
            {
                if (Immutable)
                {
                    return;
                }

                if (_state != MicroTaskStates.None)
                {
                    _log.Error("_state != MicroTaskStates.None");
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Reading;
                var text     = _file.ReadText();
                var replacer = new TextReplacer(text);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Searching;
                var matches = new List <RelatedMatch>();
                var m       = _regex.RelatedMatch(text, 0, replacer);
                while (m.Success)
                {
                    CurrentStagePercentage = (double)(m.StartIndex + m.Length) / text.Length;
                    matches.Add(m);
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }
                    m = _regex.RelatedMatch(text, m.StartIndex + m.Length, replacer);
                }
                if (matches.Count == 0)
                {
                    matches.Add(m);
                }

                if (!matches[0].Success)
                {
                    Immutable = true;
                    State     = MicroTaskStates.Complete;
                    return;
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.Replacing;
                foreach (var relatedMatch in matches)
                {
                    replacer.Replace(relatedMatch, _replacement.CreateCopyWithGroups(relatedMatch));
                }

                State = MicroTaskStates.BuildingResult;
                var result = replacer.BuildResult();
                ReplacesCount += matches.Count;

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }
                State = MicroTaskStates.SavingResult;
                _file.WriteText(result);

                State = MicroTaskStates.Complete;
            }
        }