Esempio n. 1
0
        public static TextFragments HighlightPrimary(this TextFragments textFragments, string search)
        {
            var fragments = new TextFragments();

            foreach (var original in textFragments.Fragments)
            {
                if (original.FragmentType == TextFragmentType.Primary)
                {
                    fragments.Add(CreateHighlightSearch(original.Value, search));
                }
                else
                {
                    fragments.Add(original);
                }
            }

            return(fragments);
        }
Esempio n. 2
0
        public static TextFragments CreateHighlightSearch(string text, string search)
        {
            var fragments = new TextFragments();

            if (string.IsNullOrEmpty(text))
            {
                return(fragments);
            }

            if (string.IsNullOrEmpty(search))
            {
                fragments.AddPrimary(text);
                return(fragments);
            }

            var index      = -1;
            var startIndex = 0;

            do
            {
                index = text.IndexOf(search, startIndex, StringComparison.InvariantCultureIgnoreCase);
                if (index == -1)
                {
                    var sub = text.Substring(startIndex);
                    fragments.AddPrimary(sub);
                }
                else
                {
                    var sub = text.Substring(startIndex, index - startIndex);
                    fragments.AddPrimary(sub);

                    var match = text.Substring(index, search.Length);
                    fragments.AddHighlight(match);

                    startIndex = index + search.Length;
                }
            }while (index != -1);

            return(fragments);
        }
Esempio n. 3
0
 public static bool PrimaryContainsText(this TextFragments textFragments, string search)
 {
     return(textFragments.Fragments.Where(p => p.FragmentType == TextFragmentType.Primary).Any(p => p.Value.ContainsText(search) == true));
 }
Esempio n. 4
0
 public void Add(TextFragments fragments)
 {
     Add(fragments.Fragments);
 }