Esempio n. 1
0
        private static IList <IOccurrence> StringOccurrences(string text, string pattern)
        {
            // Loop through all instances of the string 'text'.
            IList <IOccurrence> occurrences = new Occurrences();

            int i = 0;

            while ((i = text.IndexOf(pattern, i)) != -1)
            {
                occurrences.Add(new Occurrence(i, i + pattern.Length));
                i += pattern.Length;
            }
            return(occurrences);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        private static Occurrences ExceptionMatches(string input, IList <Expression> exceptions)
        {
            Occurrences exceptionMatches = new Occurrences();

            if (exceptions == null || (exceptions.Count == 0))
            {
                return(exceptionMatches);
            }
            foreach (Expression exception in exceptions)
            {
                foreach (Occurrence pointer in exception.Occurrences(input))
                {
                    exceptionMatches.Add(pointer);
                }
            }
            return(exceptionMatches);
        }
Esempio n. 4
0
        /// <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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        /// <summary>
        /// Pointers to start indexes in the input, where patterns starts.
        /// </summary>
        /// <param name="input">Input text in which the pattern is searched.</param>
        /// <returns>List of integers. If there is no pattern match, empty list is returned.</returns>
        // při porovnánání výjimky se pak zjišťuje, jestli výskyt je mezi těmito krajními body
        public IList <IOccurrence> Occurrences(string input)
        {
            IList <IOccurrence> occurences = new Occurrences();

            switch (Format)
            {
            case ChangeFormat.String:
                occurences = StringOccurrences(input, Pattern);
                break;

            case ChangeFormat.RegularExpression:
                MatchCollection matches = _regex.Matches(input);
                foreach (Match match in matches)
                {
                    occurences.Add(new Occurrence(match.Index, match.Index + match.Length));
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(occurences);
        }