/// <summary> /// Replaces all occurrences of the regex in the string with the /// replacement evaluator. /// /// Note that the special case of no matches is handled on its own: /// with no matches, the input string is returned unchanged. /// The right-to-left case is split out because StringBuilder /// doesn't handle right-to-left string building directly very well. /// </summary> private static string Replace(MatchEvaluator evaluator, Regex regex, string input, int count, int startat) { if (evaluator is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.evaluator); } if (count < -1) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.CountTooSmall); } if ((uint)startat > (uint)input.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startat, ExceptionResource.BeginIndexNotNegative); } if (count == 0) { return(input); } var state = (segments : SegmentStringBuilder.Create(), evaluator, prevat : 0, input, count); if (!regex.RightToLeft) { regex.RunAllMatchesWithCallback(input, startat, ref state, static (ref (SegmentStringBuilder segments, MatchEvaluator evaluator, int prevat, string input, int count)state, Match match) => { state.segments.Add(state.input.AsMemory(state.prevat, match.Index - state.prevat)); state.prevat = match.Index + match.Length; state.segments.Add(state.evaluator(match).AsMemory()); return(--state.count != 0); }, RegexRunnerMode.FullMatchRequired, reuseMatchObject: false);
/// <summary> /// Replaces all occurrences of the regex in the string with the /// replacement pattern. /// /// Note that the special case of no matches is handled on its own: /// with no matches, the input string is returned unchanged. /// The right-to-left case is split out because StringBuilder /// doesn't handle right-to-left string building directly very well. /// </summary> public string Replace(Regex regex, string input, int count, int startat) { if (count < -1) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.CountTooSmall); } if ((uint)startat > (uint)input.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startat, ExceptionResource.BeginIndexNotNegative); } if (count == 0) { return(input); } var state = (replacement : this, segments : SegmentStringBuilder.Create(), inputMemory : input.AsMemory(), prevat : 0, count); if (!regex.RightToLeft) { regex.RunAllMatchesWithCallback(input, startat, ref state, (ref (RegexReplacement thisRef, SegmentStringBuilder segments, ReadOnlyMemory <char> inputMemory, int prevat, int count)state, Match match) => { state.segments.Add(state.inputMemory.Slice(state.prevat, match.Index - state.prevat)); state.prevat = match.Index + match.Length; state.thisRef.ReplacementImpl(ref state.segments, match); return(--state.count != 0); }, _hasBackreferences ? RegexRunnerMode.FullMatchRequired : RegexRunnerMode.BoundsRequired, reuseMatchObject: true);
/// <summary> /// Does a split. In the right-to-left case we reorder the /// array to be forwards. /// </summary> private static string[] Split(Regex regex, string input, int count, int startat) { if (count < 0) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.CountTooSmall); } if ((uint)startat > (uint)input.Length) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startat, ExceptionResource.BeginIndexNotNegative); } if (count == 1) { return(new[] { input }); } count--; var state = (results : new List <string>(), prevat : 0, input, count); if (!regex.RightToLeft) { regex.RunAllMatchesWithCallback(input, startat, ref state, static (ref (List <string> results, int prevat, string input, int count)state, Match match) => { state.results.Add(state.input.Substring(state.prevat, match.Index - state.prevat)); state.prevat = match.Index + match.Length; // add all matched capture groups to the list. for (int i = 1; i < match.Groups.Count; i++) { if (match.IsMatched(i)) { state.results.Add(match.Groups[i].Value); } } return(--state.count != 0); }, RegexRunnerMode.FullMatchRequired, reuseMatchObject: true);