Stop() public méthode

public Stop ( ) : void
Résultat void
Exemple #1
0
        private void Replace(string attach, RegexOptions? option)
        {
            ExecutionStopwatch executionSW = new ExecutionStopwatch();
            executionSW.Start();
            Regex r;
            try
            {
                if (option == null)
                    r = new Regex(data.Pattern);
                else
                    r = new Regex(data.Pattern, (RegexOptions)option);
            }
            catch (Exception e)
            {
                Result = e.Message;
                return;
            }
            if (string.IsNullOrEmpty(data.Text))
            {
                Result = attach + "Pattern ok.";
                return;
            }
            MatchCollection coll = r.Matches(data.Text);
            int matchesNr = coll.Count;

            string attachStats = "";
            if (matchesNr == 0)
            {
                executionSW.Stop();
                attachStats = string.Format(" ({0} ms)", executionSW.Elapsed.TotalMilliseconds);
                Result = attach + "No matches found." + attachStats;
                return;
            }

            StringBuilder sb = new StringBuilder(attach);
            StringBuilder tb = new StringBuilder();

            var utility = new HtmlHelper(new ViewContext(), new ViewPage());
            List<int> matchesCoords = new List<int>();
            List<string> replacements = new List<string>();
            int count = 0;
            int maxCount = 100;
            string matchedBefore = "<span class=\"magenta\">";
            string matchedAfter = "</span>";
            foreach (Match match in coll)
            {
                int index = match.Index;
                string value = match.Value;
                string replacement = match.Result(data.Substitution);
                matchesCoords.Add(index);
                matchesCoords.Add(value.Length);
                replacements.Add(replacement);
                if (count < maxCount)
                    sb.Append(string.Format("'"+matchedBefore+"<b>{0}</b>"+matchedAfter+"' replaced by '"+matchedBefore+"<b>{1}</b>"+matchedAfter+"' at {2} <br/>", utility.Encode(value), utility.Encode(replacement), index));
                count++;
            }
            executionSW.Stop();
            attachStats = string.Format(" ({0} ms)", executionSW.Elapsed.TotalMilliseconds);
            if(count <= maxCount)
                sb.Append(string.Format("Total replacements: {0}.{1}", matchesNr, attachStats));
            else
                sb.Append(string.Format("other replacements ({0}) not included. Total matches: {1}.{2}", matchesNr - maxCount, matchesNr, attachStats));

            bool IsReverse = (option & RegexOptions.RightToLeft) != null && (option & RegexOptions.RightToLeft) != RegexOptions.None;
            if (IsReverse)
            {
                List<int> tmpMatchesCoords = new List<int>();
                List<string> tmpReplacements = new List<string>();
                for (int i = matchesCoords.Count - 1; i > 0; i -= 2)
                {
                    tmpMatchesCoords.Add(matchesCoords[i - 1]);
                    tmpMatchesCoords.Add(matchesCoords[i]);
                    tmpReplacements.Add(replacements[i / 2]);
                }
                matchesCoords = tmpMatchesCoords;
                replacements = tmpReplacements;
            }

            if (matchesNr != 0)
            {
                string before0 = "<span class=\"yellow\">";
                string after = "</span>";
                string before1 = "<span class=\"blue\">";
                bool flipper = true;
                int lastIndex = 0;
                for (int i = 0; i < matchesCoords.Count; i += 2)
                {
                    string before = flipper ? before0 : before1;
                    flipper = !flipper;
                    if (lastIndex != matchesCoords[i])
                        tb.Append(utility.Encode(data.Text.Substring(lastIndex, matchesCoords[i] - lastIndex)));
                    tb.Append(before + utility.Encode(replacements[i / 2]) + after);
                    lastIndex = matchesCoords[i] + matchesCoords[i + 1];
                }
                tb.Append(utility.Encode(data.Text.Substring(lastIndex)));
            }
            else
                tb.Append(utility.Encode(data.Text));

            Result = sb.ToString() + "<br/><br/><br/><span id=\"ResultText\">" + tb.ToString().Replace("\r", "").Replace("\n", "<br/>") + "</span>";
        }
Exemple #2
0
        private void Match(string attach, RegexOptions? option)
        {
            ExecutionStopwatch executionSW = new ExecutionStopwatch();
            executionSW.Start();
            Regex r;
            try
            {
                if (option == null)
                    r = new Regex(data.Pattern);
                else
                    r = new Regex(data.Pattern, (RegexOptions)option);
            }
            catch (Exception e)
            {
                Result = e.Message;
                return;
            }
            if (string.IsNullOrEmpty(data.Text))
            {
                Result = attach + "Pattern ok.";
                return;
            }
            MatchCollection coll = r.Matches(data.Text);
            int matchesNr = coll.Count;
            executionSW.Stop();
            string attachStats = string.Format(" ({0} ms)", executionSW.Elapsed.TotalMilliseconds);
            if (matchesNr == 0)
            {
                Result = attach + "No matches found." + attachStats;
                return;
            }

            StringBuilder sb = new StringBuilder(attach);
            StringBuilder tb = new StringBuilder();

            var utility = new HtmlHelper(new ViewContext(), new ViewPage());

            int count = 0;
            int maxCount = 100;
            bool addMoreMatches = true;
            List<int> matchIndexes = new List<int>();

            List<Match> collsTmp = new List<Match>();
            List<Match> colls = collsTmp;
            foreach (Match m in coll)
                collsTmp.Add(m);
            bool IsReverse = (option & RegexOptions.RightToLeft) != null && (option & RegexOptions.RightToLeft) != RegexOptions.None;
            if (IsReverse)
            {
                colls = new List<Match>();
                for (int i = collsTmp.Count - 1; i >= 0; i--)
                    colls.Add(collsTmp[i]);
            }
            List<string> reverseMatches = new List<string>();

            foreach (Match match in colls)
            {
                string value = match.Value;
                int index = match.Index;

                matchIndexes.Add(index);
                matchIndexes.Add(value.Length);

                string matchedBefore = "<span class=\"magenta\">";
                string matchedAfter = "</span>";
                count++;
                if (!IsReverse)
                {
                    if (count == maxCount + 1)
                    {
                        sb.Append(string.Format("other matches ({0}) not included. Total matches: {1}.{2}", matchesNr - maxCount, matchesNr, attachStats));
                        addMoreMatches = false;
                    }
                    if (addMoreMatches)
                        sb.Append(string.Format("'"+matchedBefore+"<b>{0}</b>"+matchedAfter+"' found at {1} <br/>", utility.Encode(value), index));
                }
                else
                {
                    if (matchesNr - count < maxCount)
                        reverseMatches.Add(string.Format("'"+matchedBefore+"<b>{0}</b>"+matchedAfter+"' found at {1} <br/>", utility.Encode(value), index));
                }
            }
            if (IsReverse)
            {
                for (int i = reverseMatches.Count - 1; i >= 0; i--)
                    sb.Append(reverseMatches[i]);
                if (matchesNr > maxCount)
                    sb.Append(string.Format("other matches ({0}) not included. Total matches: {1}.{2}", matchesNr - maxCount, matchesNr, attachStats));
                else
                    sb.Append(string.Format("Total matches: {0}.{1}", matchesNr, attachStats));
            }
            else if (addMoreMatches)
                sb.Append(string.Format("Total matches: {0}.{1}", matchesNr, attachStats));

            if (matchesNr != 0)
            {
                string before0 = "<span class=\"yellow\">";
                string after = "</span>";
                string before1 = "<span class=\"blue\">";
                bool flipper = true;
                int lastIndex = 0;
                for (int i = 0; i < matchIndexes.Count; i += 2)
                {
                    string before = flipper ? before0 : before1;
                    flipper = !flipper;
                    if (lastIndex != matchIndexes[i])
                        tb.Append(utility.Encode(data.Text.Substring(lastIndex, matchIndexes[i] - lastIndex)));
                    tb.Append(before + utility.Encode(data.Text.Substring(matchIndexes[i], matchIndexes[i + 1])) + after);
                    lastIndex = matchIndexes[i] + matchIndexes[i + 1];
                }
                tb.Append(utility.Encode(data.Text.Substring(lastIndex)));
            }
            else
                tb.Append(utility.Encode(data.Text));

            Result = sb.ToString() + "<br/><br/><br/>" + tb.ToString();//.Replace("\n", "<br/>");
        }