Пример #1
0
        private List <KeyValuePair <string, Color> > GenerateStyleMap(
            IEnumerable <KeyValuePair <StyleClass <TextPattern>, MatchLocation> > targets, string input)
        {
            var styleMap = new List <KeyValuePair <string, Color> >();

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

            foreach (var styledLocation in targets)
            {
                var currentLocation = styledLocation.Value;

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

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

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

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

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

                previousLocation = currentLocation.Prototype();
            }

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

            return(styleMap);
        }
Пример #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)
        {
            var matches = regexPattern.Matches(input);

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

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

                var location = new MatchLocation(beginning, end);

                yield return(location);
            }
        }