Exemple #1
0
        public static List <ReplaceItem> GetReplaceItems(
            Match match,
            ReplaceOptions replaceOptions,
            CancellationToken cancellationToken = default)
        {
            List <Match> matches = GetMatches(match);

            int offset = 0;
            List <ReplaceItem> replaceItems = ListCache <ReplaceItem> .GetInstance();

            if (replaceItems.Capacity < matches.Count)
            {
                replaceItems.Capacity = matches.Count;
            }

            foreach (Match match2 in matches)
            {
                string value = replaceOptions.Replace(match2);

                replaceItems.Add(new ReplaceItem(match2, value, match2.Index + offset));

                offset += value.Length - match2.Length;

                cancellationToken.ThrowIfCancellationRequested();
            }

            ListCache <Match> .Free(matches);

            return(replaceItems);
Exemple #2
0
        protected override void ExecuteMatch(
            FileMatch fileMatch,
            string directoryPath)
        {
            TextWriter?    textWriter = null;
            List <Capture>?captures   = null;

            try
            {
                captures = ListCache <Capture> .GetInstance();

                GetCaptures(
                    fileMatch.ContentMatch !,
                    ContentFilter !.GroupNumber,
                    predicate: ContentFilter.Predicate,
                    captures: captures);

                if (!DryRun)
                {
                    textWriter = new StreamWriter(fileMatch.Path, false, fileMatch.Encoding);

                    WriteMatches(fileMatch.ContentText, captures, ReplaceOptions, textWriter);
                }

                int fileMatchCount       = captures.Count;
                int fileReplacementCount = fileMatchCount;
                Telemetry.MatchCount          += fileMatchCount;
                Telemetry.ProcessedMatchCount += fileReplacementCount;

                if (fileReplacementCount > 0)
                {
                    Telemetry.ProcessedFileCount++;
                }

                Report(fileMatch);
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                Report(fileMatch, ex);
            }
            finally
            {
                textWriter?.Dispose();

                if (captures != null)
                {
                    ListCache <Capture> .Free(captures);
                }
            }
        }
Exemple #3
0
            static List <Match> GetMatches(Match match)
            {
                List <Match> matches = ListCache <Match> .GetInstance();

                do
                {
                    matches.Add(match);

                    match = match.NextMatch();
                } while (match.Success);

                if (matches.Count > 1 &&
                    matches[0].Index > matches[1].Index)
                {
                    matches.Reverse();
                }

                return(matches);
            }