Ejemplo n.º 1
0
        /// <summary>
        /// Instantiate internal regex member if not already done.
        /// </summary>
        /// <returns>True on success, false otherwise.</returns>
        private void Init()
        {
            StringComparison GetStringComparison()
            {
                StringComparison stringComparison;

                if (Options.HasFlag(WildcardOptions.IgnoreCase))
                {
                    stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
                        ? StringComparison.InvariantCultureIgnoreCase
                        : CultureInfo.CurrentCulture.Name.Equals("en-US-POSIX", StringComparison.OrdinalIgnoreCase)
                                       // The collation behavior of the POSIX locale (also known as the C locale) is case sensitive.
                                       // For this specific locale, we use 'OrdinalIgnoreCase'.
                            ? StringComparison.OrdinalIgnoreCase
                            : StringComparison.CurrentCultureIgnoreCase;
                }
                else
                {
                    stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
                        ? StringComparison.InvariantCulture
                        : StringComparison.CurrentCulture;
                }

                return(stringComparison);
            }

            if (_isMatch != null)
            {
                return;
            }

            if (Pattern.Length == 1 && Pattern[0] == '*')
            {
                _isMatch = s_matchAll;
                return;
            }

            int index = Pattern.IndexOfAny(s_specialChars);

            if (index == -1)
            {
                // No special characters present in the pattern, so we can just do a string comparison.
                _isMatch = str => string.Equals(str, Pattern, GetStringComparison());
                return;
            }

            if (index == Pattern.Length - 1 && Pattern[index] == '*')
            {
                // No special characters present in the pattern before last position and last character is asterisk.
                var patternWithoutAsterisk = Pattern.AsMemory().Slice(0, index);
                _isMatch = str => str.AsSpan().StartsWith(patternWithoutAsterisk.Span, GetStringComparison());
                return;
            }

            var matcher = new WildcardPatternMatcher(this);

            _isMatch = matcher.IsMatch;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Instantiate internal regex member if not already done.
        /// </summary>
        /// <returns>True on success, false otherwise.</returns>
        private void Init()
        {
            StringComparison GetStringComparison()
            {
                StringComparison stringComparison;

                if (Options.HasFlag(WildcardOptions.IgnoreCase))
                {
                    stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
                        ? StringComparison.InvariantCultureIgnoreCase
                        : StringComparison.CurrentCultureIgnoreCase;
                }
                else
                {
                    stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
                        ? StringComparison.InvariantCulture
                        : StringComparison.CurrentCulture;
                }

                return(stringComparison);
            }

            if (_isMatch != null)
            {
                return;
            }

            if (Pattern.Length == 1 && Pattern[0] == '*')
            {
                _isMatch = s_matchAll;
                return;
            }

            int index = Pattern.IndexOfAny(s_specialChars);

            if (index == -1)
            {
                // No special characters present in the pattern, so we can just do a string comparison.
                _isMatch = str => string.Equals(str, Pattern, GetStringComparison());
                return;
            }

            if (index == Pattern.Length - 1 && Pattern[index] == '*')
            {
                // No special characters present in the pattern before last position and last character is asterisk.
                var patternWithoutAsterisk = Pattern.AsMemory().Slice(0, index);
                _isMatch = str => str.AsSpan().StartsWith(patternWithoutAsterisk.Span, GetStringComparison());
                return;
            }

            var matcher = new WildcardPatternMatcher(this);

            _isMatch = matcher.IsMatch;
        }