Пример #1
0
        private Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, bool useCache)
        {
            // validate parameters:
            if (options < RegexOptions.None || (((int)options) >> MaxOptionShift) != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options));
            }
            if ((options & RegexOptions.ECMAScript) != 0 &&
                (options & ~(RegexOptions.ECMAScript |
                             RegexOptions.IgnoreCase |
                             RegexOptions.Multiline |
                             RegexOptions.CultureInvariant
#if DEBUG
                             | RegexOptions.Debug
#endif
                             )) != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options));
            }

            ValidateMatchTimeout(matchTimeout);

            this.pattern  = pattern ?? throw new ArgumentNullException(nameof(pattern));
            this.roptions = options;

            // Try to look up this regex in the cache.  We do this regardless of whether useCache is true since there's
            // really no reason not to.
            var key    = new CachedCodeEntryKey(options, pattern);
            var cached = LookupCachedAndUpdate(key);

            internalMatchTimeout = matchTimeout;

            if (cached == null)
            {
                // Parse the input
                var tree = RegexParser.Parse(pattern, roptions);

                // Extract the relevant information
                capnames = tree._capnames;
                capslist = tree._capslist;
                _code    = RegexWriter.Write(tree);
                caps     = _code._caps;
                capsize  = _code._capsize;

                InitializeReferences();

                if (useCache)
                {
                    CacheCode(key);
                }
            }
            else
            {
                caps             = cached._caps;
                capnames         = cached._capnames;
                capslist         = cached._capslist;
                capsize          = cached._capsize;
                _code            = cached._code;
                _runnerref       = cached._runnerref;
                _replref         = cached._replref;
                _refsInitialized = true;
            }
        }
Пример #2
0
        internal CachedCodeEntry(Dictionary <string, int> capnames, string[] capslist, RegexCode code, Dictionary <int, int> caps, int capsize, ExclusiveReference runner, SharedReference repl)
        {
            _capnames = capnames;
            _capslist = capslist;

            _code    = code;
            _caps    = caps;
            _capsize = capsize;

            _runnerref = runner;
            _replref   = repl;
        }