Exemple #1
0
        private List <KeyValuePair <string, Color> > GenerateStyleMap(IEnumerable <KeyValuePair <StyleClass <TextPattern>, MatchLocation> > targets, string input)
        {
            List <KeyValuePair <string, Color> > styleMap = new List <KeyValuePair <string, Color> >();

            MatchLocation previousLocation = new MatchLocation(0, 0);
            int           chocolateEnd     = 0;

            foreach (KeyValuePair <StyleClass <TextPattern>, MatchLocation> styledLocation in targets)
            {
                MatchLocation currentLocation = styledLocation.Value;

                if (previousLocation.End > currentLocation.Beginning)
                {
                    previousLocation = new MatchLocation(0, 0);
                }

                int vanillaStart   = previousLocation.End;
                int vanillaEnd     = currentLocation.Beginning;
                int chocolateStart = vanillaEnd;
                chocolateEnd = currentLocation.End;

                string vanilla   = input.Substring(vanillaStart, vanillaEnd - vanillaStart);
                string chocolate = input.Substring(chocolateStart, chocolateEnd - chocolateStart);

                chocolate = this.matchFoundHandlers[styledLocation.Key].Invoke(input, styledLocation.Value, input.Substring(chocolateStart, chocolateEnd - chocolateStart));

                if (vanilla != "")
                {
                    styleMap.Add(new KeyValuePair <string, Color>(vanilla, this.styleSheet.UnstyledColor));
                }

                if (chocolate != "")
                {
                    styleMap.Add(new KeyValuePair <string, Color>(chocolate, styledLocation.Key.Color));
                }

                previousLocation = currentLocation.Prototype();
            }

            if (chocolateEnd < input.Length)
            {
                string vanilla = input.Substring(chocolateEnd, input.Length - chocolateEnd);
                styleMap.Add(new KeyValuePair <string, Color>(vanilla, this.styleSheet.UnstyledColor));
            }

            return(styleMap);
        }
Exemple #2
0
        /// <summary>
        ///     Finds matches between the TextPattern instance and a given object.
        /// </summary>
        /// <param name="input">The string to which the TextPattern instance should be compared.</param>
        /// <returns>
        ///     Returns a collection of the locations in the string under comparison that
        ///     match the TextPattern instance.
        /// </returns>
        public override IEnumerable <MatchLocation> GetMatchLocations(string input)
        {
            MatchCollection matches = this.regexPattern.Matches(input);

            if (matches.Count == 0)
            {
                yield break;
            }

            foreach (Match match in matches)
            {
                int beginning = match.Index;
                int end       = beginning + match.Length;

                MatchLocation location = new MatchLocation(beginning, end);

                yield return(location);
            }
        }