private Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, bool addToCache) { if (pattern == null) { throw new ArgumentNullException(nameof(pattern)); } 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.Compiled | RegexOptions.CultureInvariant #if DEBUG | RegexOptions.Debug #endif )) != 0) { throw new ArgumentOutOfRangeException(nameof(options)); } ValidateMatchTimeout(matchTimeout); // After parameter validation assign this.pattern = pattern; roptions = options; internalMatchTimeout = matchTimeout; // Cache handling. Try to look up this regex in the cache. CultureInfo culture = (options & RegexOptions.CultureInvariant) != 0 ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture; var key = new CachedCodeEntryKey(options, culture.ToString(), pattern); CachedCodeEntry cached = GetCachedCode(key, false); if (cached == null) { // Parse the input RegexTree tree = RegexParser.Parse(pattern, roptions, culture); // Extract the relevant information capnames = tree.CapNames; capslist = tree.CapsList; _code = RegexWriter.Write(tree); caps = _code.Caps; capsize = _code.CapSize; InitializeReferences(); tree = null; if (addToCache) { cached = GetCachedCode(key, true); } } else { caps = cached.Caps; capnames = cached.Capnames; capslist = cached.Capslist; capsize = cached.Capsize; _code = cached.Code; #if FEATURE_COMPILED factory = cached.Factory; #endif // Cache runner and replacement _runnerref = cached.Runnerref; _replref = cached.ReplRef; _refsInitialized = true; } #if FEATURE_COMPILED // if the compile option is set, then compile the code if it's not already if (UseOptionC() && factory == null) { factory = Compile(_code, roptions); if (addToCache && cached != null) { cached.AddCompiled(factory); } _code = null; } #endif }
private Regex(string pattern, RegexOptions options, bool useCache) { CachedCodeEntry cachedAndUpdate = null; string str = null; if (pattern == null) { throw new ArgumentNullException("pattern"); } if ((options < RegexOptions.None) || ((((int)options) >> 10) != 0)) { throw new ArgumentOutOfRangeException("options"); } if (((options & RegexOptions.ECMAScript) != RegexOptions.None) && ((options & ~(RegexOptions.CultureInvariant | RegexOptions.ECMAScript | RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase)) != RegexOptions.None)) { throw new ArgumentOutOfRangeException("options"); } if ((options & RegexOptions.CultureInvariant) != RegexOptions.None) { str = CultureInfo.InvariantCulture.ToString(); } else { str = CultureInfo.CurrentCulture.ToString(); } string[] strArray = new string[] { ((int)options).ToString(NumberFormatInfo.InvariantInfo), ":", str, ":", pattern }; string key = string.Concat(strArray); cachedAndUpdate = LookupCachedAndUpdate(key); this.pattern = pattern; this.roptions = options; if (cachedAndUpdate == null) { RegexTree t = RegexParser.Parse(pattern, this.roptions); this.capnames = t._capnames; this.capslist = t._capslist; this.code = RegexWriter.Write(t); this.caps = this.code._caps; this.capsize = this.code._capsize; this.InitializeReferences(); t = null; if (useCache) { cachedAndUpdate = this.CacheCode(key); } } else { this.caps = cachedAndUpdate._caps; this.capnames = cachedAndUpdate._capnames; this.capslist = cachedAndUpdate._capslist; this.capsize = cachedAndUpdate._capsize; this.code = cachedAndUpdate._code; this.factory = cachedAndUpdate._factory; this.runnerref = cachedAndUpdate._runnerref; this.replref = cachedAndUpdate._replref; this.refsInitialized = true; } if (this.UseOptionC() && (this.factory == null)) { this.factory = this.Compile(this.code, this.roptions); if (useCache && (cachedAndUpdate != null)) { cachedAndUpdate.AddCompiled(this.factory); } this.code = null; } }
// Returns a Regex object corresponding to the given pattern, compiled with // the specified options. /// <include file='doc\Regex.uex' path='docs/doc[@for="Regex.Regex1"]/*' /> /// <devdoc> /// <para> /// Creates and compiles a regular expression object for the /// specified regular expression /// with options that modify the pattern. /// </para> /// </devdoc> public Regex(String pattern, RegexOptions options) { RegexTree tree; CachedCodeEntry cached; if (pattern == null) { throw new ArgumentNullException("pattern"); } if (options < RegexOptions.None || (((int)options) >> MaxOptionShift) != 0) { throw new ArgumentOutOfRangeException("options"); } if ((options & RegexOptions.ECMAScript) != 0 && (options & ~(RegexOptions.ECMAScript | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled #if DBG | RegexOptions.Debug #endif )) != 0) { throw new ArgumentOutOfRangeException("options"); } String key = ((int)options).ToString(NumberFormatInfo.InvariantInfo) + ":" + pattern; cached = LookupCached(key); this.pattern = pattern; this.roptions = options; if (cached == null) { // Parse the input 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(); tree = null; cachedentry = CacheCode(key); } else { caps = cached._caps; capnames = cached._capnames; capslist = cached._capslist; capsize = cached._capsize; code = cached._code; factory = cached._factory; runnerref = cached._runnerref; replref = cached._replref; refsInitialized = true; cachedentry = cached; } // if the compile option is set, then compile the code if it's not already if (UseOptionC() && factory == null) { factory = Compile(code, roptions); cachedentry.AddCompiled(factory); code = null; } }
private Regex(String pattern, RegexOptions options, bool useCache) { RegexTree tree; CachedCodeEntry cached = null; string cultureKey = null; if (pattern == null) { throw new ArgumentNullException("pattern"); } if (options < RegexOptions.None || (((int)options) >> MaxOptionShift) != 0) { throw new ArgumentOutOfRangeException("options"); } if ((options & RegexOptions.ECMAScript) != 0 && (options & ~(RegexOptions.ECMAScript | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.CultureInvariant #if DBG | RegexOptions.Debug #endif )) != 0) { throw new ArgumentOutOfRangeException("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. if ((options & RegexOptions.CultureInvariant) != 0) { cultureKey = CultureInfo.InvariantCulture.ThreeLetterWindowsLanguageName; } else { cultureKey = CultureInfo.CurrentCulture.ThreeLetterWindowsLanguageName; } String key = ((int)options).ToString(NumberFormatInfo.InvariantInfo) + ":" + cultureKey + ":" + pattern; cached = LookupCachedAndUpdate(key); this.pattern = pattern; this.roptions = options; if (cached == null) { // Parse the input 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(); tree = null; if (useCache) { cached = CacheCode(key); } } else { caps = cached._caps; capnames = cached._capnames; capslist = cached._capslist; capsize = cached._capsize; code = cached._code; factory = cached._factory; runnerref = cached._runnerref; replref = cached._replref; refsInitialized = true; } // if the compile option is set, then compile the code if it's not already if (UseOptionC() && factory == null) { factory = Compile(code, roptions); if (useCache && cached != null) { cached.AddCompiled(factory); } code = null; } }