public ChangeBase(string pattern, string replace, ChangeFormat format, ChangeOptions options) { Pattern = pattern; Replace = replace; Format = format; Options = options; }
private static int GetReplacementCount(ChangeOptions options) { int replacementCount = -1; if (options.ReplacementCount.HasValue) { replacementCount = options.ReplacementCount.Value; } return(replacementCount); }
private string ApplyString(string input, ChangeOptions options, IList <Change> exceptions) { string output = input; if (!IsReplacementApplicable()) { return(output); } Occurrences exceptionMatches = ExceptionMatches(input, exceptions); if (options.Exceptions != null && options.Exceptions.Count > 0) { Occurrences innerExceptionMatches = ExceptionMatches(input, options.Exceptions); if (innerExceptionMatches.Count > 0) { exceptionMatches.AddRange(innerExceptionMatches); } } int replacementCount = GetReplacementCount(options); if (replacementCount == 0) { return(output); } if (exceptionMatches.Count == 0) { if (replacementCount == -1) { output = input.Replace(Pattern, Replace); } else { int count = replacementCount; output = Output(options, count, exceptionMatches, output); } } else { int count = (replacementCount == -1) ? input.Length : replacementCount; output = Output(options, count, exceptionMatches, output); } return(output); }
/// <summary> /// Applies change in regular expression format to input string. /// </summary> /// <param name="input"></param> /// <param name="options"></param> /// <param name="exceptions"></param> /// <returns></returns> private string ApplyRegex(string input, ChangeOptions options, IList <Change> exceptions) { if (!IsReplacementApplicable()) { return(input); //nedochází k žádné změně } //pravidla počtu nahrazení pro Regex: -1 = všechny výskyty; 0 = žádný výskyt; 1 a více = zadaný počet výskytů //pravdila Cahnge: null, 0 = žádný výskyt string output = input; int replacementCount = GetReplacementCount(options); Occurrences exceptionMatches = ExceptionMatches(input, exceptions); if (exceptionMatches.Count == 0) { return(base.Regex.Replace(input, Replace, replacementCount)); } MatchCollection patternMatches = base.Regex.Matches(input); int count = 0; output = input; foreach (Match match in patternMatches) { Occurrence occurrence = new Occurrence(match.Index, match.Index + match.Length); if (!exceptionMatches.IsOccurrenceWithin(occurrence)) { output = base.Regex.Replace(output, Replace, 1, match.Index); count++; } if (count == options.ReplacementCount) { return(output); } } return(output); }
public static string ReplaceFirstOccurrence(string original, string oldValue, string newValue, ChangeOptions options, int startIndex) { if (String.IsNullOrEmpty(original)) { return(String.Empty); } if (String.IsNullOrEmpty(oldValue)) { return(original); } if (String.IsNullOrEmpty(newValue)) { newValue = String.Empty; } int loc = original.IndexOf(oldValue, startIndex, StringComparison.Ordinal); if (loc == -1) { return(original); } return(original.Remove(loc, oldValue.Length).Insert(loc, newValue)); }
private string Output(ChangeOptions options, int count, Occurrences exceptionMatches, string output) { int start = 0; for (int i = 0; i < count; i++) { int loc = output.IndexOf(Pattern, start, StringComparison.Ordinal); Occurrence occurrence = new Occurrence(loc, loc + Pattern.Length); while (exceptionMatches.IsOccurrenceWithin(occurrence)) { loc = output.IndexOf(Pattern, loc + 1, StringComparison.Ordinal); occurrence = new Occurrence(loc, loc + Pattern.Length); } if (loc == -1) { return(output); } output = ReplaceFirstOccurrence(output, Pattern, Replace, options, loc); start = loc + 1; } return(output); }
public static string ReplaceFirstOccurrence(string original, string oldValue, string newValue, ChangeOptions options) { return(ReplaceFirstOccurrence(original, oldValue, newValue, options, 0)); }
/// <summary> /// Applies change to input string as simple string. /// </summary> /// <param name="input"></param> /// <param name="options"></param> /// <returns></returns> private string ApplyString(string input, ChangeOptions options) { return(ApplyString(input, options, new Changes())); }
public Change(int order, string pattern, string replace, ChangeFormat format, ChangeOptions options) : base(pattern, replace, format, options) { Order = order; }
public Change(string pattern, string replace, ChangeFormat format, ChangeOptions options) : base(pattern, replace, format, options) { }