Exemplo n.º 1
0
 /// <summary>
 /// Validates the <paramref name="selection"/> against what is <see cref="Supported"/> to ensure validity.
 /// </summary>
 /// <param name="selection">The <see cref="WildcardSelection"/> to validate.</param>
 /// <returns><c>true</c> indicates that the selection is valid; otherwise, <c>false</c> for invalid.</returns>
 public bool Validate(WildcardSelection selection)
 {
     if ((selection.HasFlag(WildcardSelection.None) && !Supported.HasFlag(WildcardSelection.None)) ||
         (selection.HasFlag(WildcardSelection.Equal) && !Supported.HasFlag(WildcardSelection.Equal)) ||
         (selection.HasFlag(WildcardSelection.Single) && !Supported.HasFlag(WildcardSelection.Single)) ||
         (selection.HasFlag(WildcardSelection.StartsWith) && !Supported.HasFlag(WildcardSelection.StartsWith)) ||
         (selection.HasFlag(WildcardSelection.EndsWith) && !Supported.HasFlag(WildcardSelection.EndsWith)) ||
         (selection.HasFlag(WildcardSelection.Contains) && !Supported.HasFlag(WildcardSelection.Contains)) ||
         (selection.HasFlag(WildcardSelection.Embedded) && !Supported.HasFlag(WildcardSelection.Embedded)) ||
         (selection.HasFlag(WildcardSelection.MultiWildcard) && !Supported.HasFlag(WildcardSelection.MultiWildcard)) ||
         (selection.HasFlag(WildcardSelection.SingleWildcard) && !Supported.HasFlag(WildcardSelection.SingleWildcard)) ||
         (selection.HasFlag(WildcardSelection.AdjacentWildcards) && !Supported.HasFlag(WildcardSelection.AdjacentWildcards)) ||
         (selection.HasFlag(WildcardSelection.InvalidCharacter)))
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Parses the wildcard text to ensure validitity returning a <see cref="WildcardResult"/>.
        /// </summary>
        /// <param name="text">The wildcard text.</param>
        /// <returns>The corresponding <see cref="WildcardResult"/>.</returns>
        public WildcardResult Parse(string?text)
        {
            text = Cleaner.Clean(text, StringTrim.Both, Transform);
            if (string.IsNullOrEmpty(text))
            {
                return new WildcardResult(this)
                       {
                           Selection = WildcardSelection.None, Text = text
                       }
            }
            ;

            var sb = new StringBuilder();
            var wr = new WildcardResult(this)
            {
                Selection = WildcardSelection.Undetermined
            };

            if (CharactersNotAllowed != null && CharactersNotAllowed.Count > 0 && text.IndexOfAny(CharactersNotAllowed.ToArray()) >= 0)
            {
                wr.Selection |= WildcardSelection.InvalidCharacter;
            }

            var hasMulti = SpaceTreatment == WildcardSpaceTreatment.MultiWildcardWhenOthers && Supported.HasFlag(WildcardSelection.MultiWildcard) && text.IndexOf(MultiWildcardCharacter, StringComparison.InvariantCulture) >= 0;
            var hasTxt   = false;

            for (int i = 0; i < text.Length; i++)
            {
                var c        = text[i];
                var isMulti  = c == MultiWildcard;
                var isSingle = c == SingleWildcard;

                if (isMulti)
                {
                    wr.Selection |= WildcardSelection.MultiWildcard;

                    // Skip adjacent multi's as they are redundant.
                    for (int j = i + 1; j < text.Length; j++)
                    {
                        if (text[j] == MultiWildcard)
                        {
                            i = j;
                            continue;
                        }

                        break;
                    }
                }

                if (isSingle)
                {
                    wr.Selection |= WildcardSelection.SingleWildcard;
                }

                if (isMulti || isSingle)
                {
                    if (text.Length == 1)
                    {
                        wr.Selection |= WildcardSelection.Single;
                    }
                    else if (i == 0)
                    {
                        wr.Selection |= WildcardSelection.EndsWith;
                    }
                    else if (i == text.Length - 1)
                    {
                        wr.Selection |= WildcardSelection.StartsWith;
                    }
                    else
                    {
                        if (hasTxt || isSingle)
                        {
                            wr.Selection |= WildcardSelection.Embedded;
                        }
                        else
                        {
                            wr.Selection |= WildcardSelection.EndsWith;
                        }
                    }

                    if (i < text.Length - 1 && (text[i + 1] == MultiWildcard || text[i + 1] == SingleWildcard))
                    {
                        wr.Selection |= WildcardSelection.AdjacentWildcards;
                    }
                }
                else
                {
                    hasTxt = true;
                    if (c == SpaceCharacter && (SpaceTreatment == WildcardSpaceTreatment.Compress || SpaceTreatment == WildcardSpaceTreatment.MultiWildcardAlways || SpaceTreatment == WildcardSpaceTreatment.MultiWildcardWhenOthers))
                    {
                        // Compress adjacent spaces.
                        bool skipChar = SpaceTreatment != WildcardSpaceTreatment.Compress && text[i - 1] == MultiWildcardCharacter;
                        for (int j = i + 1; j < text.Length; j++)
                        {
                            if (text[j] == SpaceCharacter)
                            {
                                i = j;
                                continue;
                            }

                            break;
                        }

                        if (skipChar || (SpaceTreatment != WildcardSpaceTreatment.Compress && text[i + 1] == MultiWildcardCharacter))
                        {
                            continue;
                        }

                        if (SpaceTreatment == WildcardSpaceTreatment.MultiWildcardAlways || (SpaceTreatment == WildcardSpaceTreatment.MultiWildcardWhenOthers && hasMulti))
                        {
                            c             = MultiWildcardCharacter;
                            wr.Selection |= WildcardSelection.MultiWildcard;
                            wr.Selection |= WildcardSelection.Embedded;
                        }
                    }
                }

                sb.Append(c);
            }

            if (!hasTxt && wr.Selection == (WildcardSelection.StartsWith | WildcardSelection.MultiWildcard))
            {
                wr.Selection |= WildcardSelection.Single;
                wr.Selection ^= WildcardSelection.StartsWith;
            }

            if (hasTxt && wr.Selection.HasFlag(WildcardSelection.StartsWith) && wr.Selection.HasFlag(WildcardSelection.EndsWith) && !wr.Selection.HasFlag(WildcardSelection.Embedded))
            {
                wr.Selection |= WildcardSelection.Contains;
                wr.Selection ^= WildcardSelection.StartsWith;
                wr.Selection ^= WildcardSelection.EndsWith;
            }

            if (wr.Selection == WildcardSelection.Undetermined)
            {
                wr.Selection |= WildcardSelection.Equal;
            }

            wr.Text = sb.Length == 0 ? null : sb.ToString();
            return(wr);
        }
    }