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);
private void WriteMatches( string input, IEnumerable <Capture> captures, ReplaceOptions replaceOptions, TextWriter textWriter) { int index = 0; foreach (Capture capture in captures) { textWriter.Write(input.AsSpan(index, capture.Index - index)); string result = replaceOptions.Replace((Match)capture); textWriter.Write(result); index = capture.Index + capture.Length; } textWriter.Write(input.AsSpan(index, input.Length - index)); }