public static async Task <Regex> GetRegexAsync(RegexEngine engine, string pattern, RegexOptions?options = null, TimeSpan?matchTimeout = null)
        {
            if (options is null)
            {
                Assert.Null(matchTimeout);
            }

            switch (engine)
            {
            case RegexEngine.Interpreter:
                return
                    (options is null ? new Regex(pattern) :
                     matchTimeout is null ? new Regex(pattern, options.Value) :
                     new Regex(pattern, options.Value, matchTimeout.Value));

            case RegexEngine.Compiled:
                return
                    (options is null ? new Regex(pattern, RegexOptions.Compiled) :
                     matchTimeout is null ? new Regex(pattern, options.Value | RegexOptions.Compiled) :
                     new Regex(pattern, options.Value | RegexOptions.Compiled, matchTimeout.Value));

            case RegexEngine.SourceGenerated:
                return(await RegexGeneratorHelper.SourceGenRegexAsync(pattern, options, matchTimeout));
            }

            throw new ArgumentException($"Unknown engine: {engine}");
        }
Exemple #2
0
        public static async Task <Regex> GetRegexAsync(RegexEngine engine, [StringSyntax(StringSyntaxAttribute.Regex)] string pattern, RegexOptions options, Globalization.CultureInfo culture)
        {
            if (engine == RegexEngine.SourceGenerated)
            {
                return(await RegexGeneratorHelper.SourceGenRegexAsync(pattern, culture, options));
            }

            using (new System.Tests.ThreadCultureChange(culture))
            {
                return(await GetRegexAsync(engine, pattern, options));
            }
        }