public Regex(string pattern, RegexOptions options) { this.refsInitialized = false; if (pattern == null) { throw new ArgumentNullException(); } if ((options < RegexOptions.None) || ((((int) options) >> 9) != 0)) { throw new ArgumentOutOfRangeException(); } if (((options & RegexOptions.ECMAScript) != RegexOptions.None) && ((options & ~(RegexOptions.CultureInvariant | RegexOptions.ECMAScript | RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase)) != RegexOptions.None)) { throw new ArgumentOutOfRangeException(); } string text1 = options + ":" + pattern; this.pattern = pattern; this.roptions = options; 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; }
internal Type FactoryTypeFromCode(RegexCode code, RegexOptions options, string typeprefix) { base._code = code; base._codes = code._codes; base._strings = code._strings; base._fcPrefix = code._fcPrefix; base._bmPrefix = code._bmPrefix; base._anchors = code._anchors; base._trackcount = code._trackcount; base._options = options; string str3 = Interlocked.Increment(ref _typeCount).ToString(CultureInfo.InvariantCulture); string typename = typeprefix + "Runner" + str3; string str2 = typeprefix + "Factory" + str3; this.DefineType(typename, false, typeof(RegexRunner)); this.DefineMethod("Go", null); base.GenerateGo(); this.BakeMethod(); this.DefineMethod("FindFirstChar", typeof(bool)); base.GenerateFindFirstChar(); this.BakeMethod(); this.DefineMethod("InitTrackCount", null); base.GenerateInitTrackCount(); this.BakeMethod(); Type newtype = this.BakeType(); this.DefineType(str2, false, typeof(RegexRunnerFactory)); this.DefineMethod("CreateInstance", typeof(RegexRunner)); this.GenerateCreateInstance(newtype); this.BakeMethod(); return this.BakeType(); }
internal RegexInterpreter(RegexCode code, CultureInfo culture) { Debug.Assert(code != null, "code cannot be null."); Debug.Assert(culture != null, "culture cannot be null."); _code = code; _culture = culture; }
internal RegexInterpreter(RegexCode code, CultureInfo culture) { runcode = code; runcodes = code._codes; runstrings = code._strings; runfcPrefix = code._fcPrefix; runbmPrefix = code._bmPrefix; runanchors = code._anchors; runculture = culture; }
internal CachedCodeEntry(string key, Hashtable capnames, string[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, SharedReference repl) { this._key = key; this._capnames = capnames; this._capslist = capslist; this._code = code; this._caps = caps; this._capsize = capsize; this._runnerref = runner; this._replref = repl; }
public CachedCodeEntry(CachedCodeEntryKey key, Hashtable capnames, string[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, WeakReference <RegexReplacement> replref) { _key = key; _capnames = capnames; _capslist = capslist; _code = code; _caps = caps; _capsize = capsize; _runnerref = runner; _replref = replref; }
internal void GenerateRegexType(string pattern, RegexOptions options, string name, bool isPublic, RegexCode code, TimeSpan matchTimeout) { // Store arguments into the base type's fields _options = options; _code = code; _codes = code.Codes; _strings = code.Strings; _leadingCharClasses = code.LeadingCharClasses; _boyerMoorePrefix = code.BoyerMoorePrefix; _leadingAnchor = code.LeadingAnchor; _trackcount = code.TrackCount; // Pick a name for the class. string typenumString = ((uint)Interlocked.Increment(ref s_typeCount)).ToString(); // Generate the RegexRunner-derived type. TypeBuilder regexRunnerTypeBuilder = DefineType(_module, $"{name}Runner{typenumString}", isPublic: false, isSealed: true, typeof(RegexRunner)); _ilg = DefineMethod(regexRunnerTypeBuilder, "Go", null); GenerateGo(); _ilg = DefineMethod(regexRunnerTypeBuilder, "FindFirstChar", typeof(bool)); GenerateFindFirstChar(); _ilg = DefineMethod(regexRunnerTypeBuilder, "InitTrackCount", null); GenerateInitTrackCount(); Type runnerType = regexRunnerTypeBuilder.CreateType() !; // Generate the RegexRunnerFactory-derived type. TypeBuilder regexRunnerFactoryTypeBuilder = DefineType(_module, $"{name}Factory{typenumString}", isPublic: false, isSealed: true, typeof(RegexRunnerFactory)); _ilg = DefineMethod(regexRunnerFactoryTypeBuilder, "CreateInstance", typeof(RegexRunner)); GenerateCreateInstance(runnerType); Type regexRunnerFactoryType = regexRunnerFactoryTypeBuilder.CreateType() !; // Generate the Regex-derived type. TypeBuilder regexTypeBuilder = DefineType(_module, name, isPublic, isSealed: false, typeof(Regex)); ConstructorBuilder defaultCtorBuilder = regexTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes); _ilg = defaultCtorBuilder.GetILGenerator(); GenerateRegexDefaultCtor(pattern, options, regexRunnerFactoryType, code, matchTimeout); if (matchTimeout != Regex.InfiniteMatchTimeout) { // We only generate a constructor with a timeout parameter if the regex information supplied has a non-infinite timeout. // If it has an infinite timeout, then the generated code is not going to respect the timeout. This is a difference from netfx, // due to the fact that we now special-case an infinite timeout in the code generator to avoid spitting unnecessary code // and paying for the checks at run time. _ilg = regexTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(TimeSpan) }).GetILGenerator(); GenerateRegexTimeoutCtor(defaultCtorBuilder, regexTypeBuilder); } regexTypeBuilder.CreateType(); }
/// <summary> /// Emits a one-argument operation. /// </summary> private void Emit(int op, int opd1) { if (_counting) { _count += 2; if (RegexCode.OpcodeBacktracks(op)) { _trackcount += 1; } return; } _emitted[_curpos++] = op; _emitted[_curpos++] = opd1; }
/// <summary> /// This is the only function that should be called from outside. /// It takes a RegexTree and creates a corresponding RegexCode. /// </summary> internal static RegexCode Write(RegexTree t) { RegexWriter w = new RegexWriter(); RegexCode retval = w.RegexCodeFromRegexTree(t); #if DEBUG if (t.Debug) { t.Dump(); retval.Dump(); } #endif return(retval); }
internal void Emit(int op) { if (this._counting) { this._count++; if (RegexCode.OpcodeBacktracks(op)) { this._trackcount++; } } else { this._emitted[this._curpos++] = op; } }
/* * Emits a two-argument operation. */ internal void Emit(int op, int opd1, int opd2) { if (_counting) { _count += 3; if (RegexCode.OpcodeBacktracks(op)) { _trackcount += 1; } return; } _emitted[_curpos++] = op; _emitted[_curpos++] = opd1; _emitted[_curpos++] = opd2; }
internal CachedCodeEntry(RegexOptions options, Hashtable capnames, String[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, SharedReference repl) { _options = options; _capnames = capnames; _capslist = capslist; _code = code; _caps = caps; _capsize = capsize; _runnerref = runner; _replref = repl; _references = 1; }
/// <summary> /// This is the only function that should be called from outside. /// It takes a RegexTree and creates a corresponding RegexCode. /// </summary> internal static RegexCode Write(RegexTree t) { Span <int> emittedSpan = stackalloc int[EmittedSize]; Span <int> intStackSpan = stackalloc int[IntStackSize]; var w = new RegexWriter(emittedSpan, intStackSpan); RegexCode retval = w.RegexCodeFromRegexTree(t); #if DEBUG if (t.Debug) { t.Dump(); retval.Dump(); } #endif return(retval); }
/// <summary> /// This is the only function that should be called from outside. /// It takes a RegexTree and creates a corresponding RegexCode. /// </summary> public static RegexCode Write(RegexTree tree) { var writer = new RegexWriter(stackalloc int[EmittedSize], stackalloc int[IntStackSize]); RegexCode code = writer.RegexCodeFromRegexTree(tree); writer.Dispose(); #if DEBUG if (tree.Debug) { tree.Dump(); code.Dump(); } #endif return(code); }
internal void Emit(int op, int opd1, int opd2, int opd3) { if (this._counting) { this._count += 4; if (RegexCode.OpcodeBacktracks(op)) { this._trackcount++; } } else { this._emitted[this._curpos++] = op; this._emitted[this._curpos++] = opd1; this._emitted[this._curpos++] = opd2; this._emitted[this._curpos++] = opd3; } }
internal RegexRunnerFactory FactoryInstanceFromCode(RegexCode code, RegexOptions options) { base._code = code; base._codes = code._codes; base._strings = code._strings; base._fcPrefix = code._fcPrefix; base._bmPrefix = code._bmPrefix; base._anchors = code._anchors; base._trackcount = code._trackcount; base._options = options; string str = Interlocked.Increment(ref _regexCount).ToString(CultureInfo.InvariantCulture); DynamicMethod go = this.DefineDynamicMethod("Go" + str, null, typeof(CompiledRegexRunner)); base.GenerateGo(); DynamicMethod firstChar = this.DefineDynamicMethod("FindFirstChar" + str, typeof(bool), typeof(CompiledRegexRunner)); base.GenerateFindFirstChar(); DynamicMethod trackCount = this.DefineDynamicMethod("InitTrackCount" + str, null, typeof(CompiledRegexRunner)); base.GenerateInitTrackCount(); return new CompiledRegexRunnerFactory(go, firstChar, trackCount); }
internal RegexRunnerFactory FactoryInstanceFromCode(RegexCode code, RegexOptions options) { base._code = code; base._codes = code._codes; base._strings = code._strings; base._fcPrefix = code._fcPrefix; base._bmPrefix = code._bmPrefix; base._anchors = code._anchors; base._trackcount = code._trackcount; base._options = options; string str = Interlocked.Increment(ref _regexCount).ToString(CultureInfo.InvariantCulture); DynamicMethod go = this.DefineDynamicMethod("Go" + str, null, typeof(CompiledRegexRunner)); base.GenerateGo(); DynamicMethod firstChar = this.DefineDynamicMethod("FindFirstChar" + str, typeof(bool), typeof(CompiledRegexRunner)); base.GenerateFindFirstChar(); DynamicMethod trackCount = this.DefineDynamicMethod("InitTrackCount" + str, null, typeof(CompiledRegexRunner)); base.GenerateInitTrackCount(); return(new CompiledRegexRunnerFactory(go, firstChar, trackCount)); }
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; } }
public void AddCompiled(RegexRunnerFactory factory) { Factory = factory; Code = null; }
// This method is here for perf reasons: if the call to RegexCompiler is NOT in the // Regex constructor, we don't load RegexCompiler and its reflection classes when // instantiating a non-compiled regex // This method is internal virtual so the jit does not inline it. internal virtual RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions) { return(RegexCompiler.Compile(code, roptions)); }
internal RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions) { return RegexCompiler.Compile(code, roptions); }
internal void GenerateRegexType(string pattern, RegexOptions opts, string name, bool ispublic, RegexCode code, RegexTree tree, Type factory) { FieldInfo ft = this.RegexField("pattern"); FieldInfo info2 = this.RegexField("roptions"); FieldInfo info3 = this.RegexField("factory"); FieldInfo field = this.RegexField("caps"); FieldInfo info5 = this.RegexField("capnames"); FieldInfo info6 = this.RegexField("capslist"); FieldInfo info7 = this.RegexField("capsize"); Type[] parameterTypes = new Type[0]; this.DefineType(name, ispublic, typeof(Regex)); this._methbuilder = null; MethodAttributes @public = MethodAttributes.Public; base._ilg = this._typebuilder.DefineConstructor(@public, CallingConventions.Standard, parameterTypes).GetILGenerator(); base.Ldthis(); base._ilg.Emit(OpCodes.Call, typeof(Regex).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], new ParameterModifier[0])); base.Ldthis(); base.Ldstr(pattern); base.Stfld(ft); base.Ldthis(); base.Ldc((int)opts); base.Stfld(info2); base.Ldthis(); base.Newobj(factory.GetConstructor(parameterTypes)); base.Stfld(info3); if (code._caps != null) { this.GenerateCreateHashtable(field, code._caps); } if (tree._capnames != null) { this.GenerateCreateHashtable(info5, tree._capnames); } if (tree._capslist != null) { base.Ldthis(); base.Ldc(tree._capslist.Length); base._ilg.Emit(OpCodes.Newarr, typeof(string)); base.Stfld(info6); for (int i = 0; i < tree._capslist.Length; i++) { base.Ldthisfld(info6); base.Ldc(i); base.Ldstr(tree._capslist[i]); base._ilg.Emit(OpCodes.Stelem_Ref); } } base.Ldthis(); base.Ldc(code._capsize); base.Stfld(info7); base.Ldthis(); base.Call(typeof(Regex).GetMethod("InitializeReferences", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)); base.Ret(); this._typebuilder.CreateType(); base._ilg = null; this._typebuilder = null; }
private Regex(String pattern, RegexOptions options, TimeSpan matchTimeout, bool useCache) { RegexTree tree; CachedCodeEntry cached = null; string cultureKey = null; 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.CultureInvariant #if DEBUG | RegexOptions.Debug #endif )) != 0) { throw new ArgumentOutOfRangeException(nameof(options)); } ValidateMatchTimeout(matchTimeout); // 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.ToString(); // "English (United States)" } else { cultureKey = CultureInfo.CurrentCulture.ToString(); } var key = new CachedCodeEntryKey(options, cultureKey, pattern); cached = LookupCachedAndUpdate(key); this.pattern = pattern; roptions = options; internalMatchTimeout = matchTimeout; 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; _runnerref = cached._runnerref; _replref = cached._replref; _refsInitialized = true; } }
internal void AddCompiled(RegexRunnerFactory factory) { _factory = factory; _code = null; }
internal void GenerateRegexType(String pattern, RegexOptions opts, String name, bool ispublic, RegexCode code, RegexTree tree, Type factory) { FieldInfo patternF = RegexField("pattern"); FieldInfo optionsF = RegexField("roptions"); FieldInfo factoryF = RegexField("factory"); FieldInfo capsF = RegexField("caps"); FieldInfo capnamesF = RegexField("capnames"); FieldInfo capslistF = RegexField("capslist"); FieldInfo capsizeF = RegexField("capsize"); Type[] noTypeArray = new Type[0]; ConstructorBuilder cbuilder; DefineType(name, ispublic, typeof(Regex)); { // define constructor _methbuilder = null; MethodAttributes ma = System.Reflection.MethodAttributes.Public; cbuilder = _typebuilder.DefineConstructor(ma, CallingConventions.Standard, noTypeArray); _ilg = cbuilder.GetILGenerator(); { // call base constructor Ldthis(); _ilg.Emit(OpCodes.Call, typeof(Regex).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], new ParameterModifier[0])); // set pattern Ldthis(); Ldstr(pattern); Stfld(patternF); // set options Ldthis(); Ldc((int) opts); Stfld(optionsF); // set factory Ldthis(); Newobj(factory.GetConstructor(noTypeArray)); Stfld(factoryF); // set caps if (code._caps != null) GenerateCreateHashtable(capsF, code._caps); // set capnames if (tree._capnames != null) GenerateCreateHashtable(capnamesF, tree._capnames); // set capslist if (tree._capslist != null) { Ldthis(); Ldc(tree._capslist.Length); _ilg.Emit(OpCodes.Newarr, typeof(String)); // create new string array Stfld(capslistF); for (int i=0; i< tree._capslist.Length; i++) { Ldthisfld(capslistF); Ldc(i); Ldstr(tree._capslist[i]); _ilg.Emit(OpCodes.Stelem_Ref); } } // set capsize Ldthis(); Ldc(code._capsize); Stfld(capsizeF); // set runnerref and replref by calling InitializeReferences() Ldthis(); Call(typeof(Regex).GetMethod("InitializeReferences", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)); Ret(); } } // bake the constructor and type, then save the assembly cbuilder = null; _typebuilder.CreateType(); _ilg = null; _typebuilder = null; }
internal void AddCompiled(RegexRunnerFactory factory) { lock(this) { _code = null; _factory = factory; _references += 1; // will never be balanced since we never unload the type } }
private void GenerateRegexDefaultCtor(string pattern, RegexOptions options, Type regexRunnerFactoryType, RegexCode code, TimeSpan matchTimeout) { // Call the base ctor and store pattern, options, and factory. // base.ctor(); // base.pattern = pattern; // base.options = options; // base.factory = new DerivedRegexRunnerFactory(); Ldthis(); _ilg !.Emit(OpCodes.Call, typeof(Regex).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, Array.Empty <ParameterModifier>()) !); Ldthis(); Ldstr(pattern); Stfld(RegexField(nameof(Regex.pattern))); Ldthis(); Ldc((int)options); Stfld(RegexField(nameof(Regex.roptions))); Ldthis(); Newobj(regexRunnerFactoryType.GetConstructor(Type.EmptyTypes) !); Stfld(RegexField(nameof(Regex.factory))); // Store the timeout (no need to validate as it should have happened in RegexCompilationInfo) if (matchTimeout == Regex.InfiniteMatchTimeout) { // base.internalMatchTimeout = Regex.InfiniteMatchTimeout; _ilg.Emit(OpCodes.Ldsfld, RegexField(nameof(Regex.InfiniteMatchTimeout))); } else { // base.internalMatchTimeout = TimeSpan.FromTick(matchTimeout.Ticks); Ldthis(); LdcI8(matchTimeout.Ticks); Call(typeof(TimeSpan).GetMethod(nameof(TimeSpan.FromTicks), BindingFlags.Public | BindingFlags.Static) !); } Stfld(RegexField(nameof(Regex.internalMatchTimeout))); // Set capsize, caps, capnames, capslist. Ldthis(); Ldc(code.CapSize); Stfld(RegexField(nameof(Regex.capsize))); if (code.Caps != null) { // Caps = new Hashtable {{0, 0}, {1, 1}, ... }; GenerateCreateHashtable(RegexField(nameof(Regex.caps)), code.Caps); } if (code.Tree.CapNames != null) { // CapNames = new Hashtable {{"0", 0}, {"1", 1}, ...}; GenerateCreateHashtable(RegexField(nameof(Regex.capnames)), code.Tree.CapNames); } if (code.Tree.CapsList != null) { // capslist = new string[...]; // capslist[0] = "0"; // capslist[1] = "1"; // ... Ldthis(); Ldc(code.Tree.CapsList.Length); _ilg.Emit(OpCodes.Newarr, typeof(string)); // create new string array FieldInfo capslistField = RegexField(nameof(Regex.capslist)); Stfld(capslistField); for (int i = 0; i < code.Tree.CapsList.Length; i++) { Ldthisfld(capslistField); Ldc(i); Ldstr(code.Tree.CapsList[i]); _ilg.Emit(OpCodes.Stelem_Ref); } } // InitializeReferences(); // return; Ldthis(); Call(typeof(Regex).GetMethod("InitializeReferences", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) !); Ret(); }
// 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; } }
// Entry point to dynamically compile a regular expression. The expression is compiled to // an in memory assembly. internal static RegexRunnerFactory Compile(RegexCode code, RegexOptions options) { RegexCompiler c; c = GetThreadCompiler(); Type factory; RegexRunnerFactory rrf; new ReflectionPermission(PermissionState.Unrestricted).Assert(); try { factory = c.FactoryFromCode(code, options, "Regex"); rrf = (RegexRunnerFactory)(Activator.CreateInstance(factory, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null)); } finally { CodeAccessPermission.RevertAssert(); } return rrf; }
/* * Entry point to dynamically compile a regular expression. The expression is compiled to * an in memory assembly. */ internal static RegexRunnerFactory Compile(RegexCode code, RegexOptions options) { RegexLWCGCompiler c = new RegexLWCGCompiler(); RegexRunnerFactory factory; new ReflectionPermission(PermissionState.Unrestricted).Assert(); try { factory = c.FactoryInstanceFromCode(code, options); } finally { CodeAccessPermission.RevertAssert(); } return factory; }
private RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions) { return(RegexCompiler.Compile(code, roptions)); }
/* * The top-level driver. Initializes everything then calls the Generate* methods. */ internal Type FactoryTypeFromCode(RegexCode code, RegexOptions options, String typeprefix) { String runnertypename; String runnerfactoryname; Type runnertype; Type factory; _code = code; _codes = code._codes; _strings = code._strings; _fcPrefix = code._fcPrefix; _bmPrefix = code._bmPrefix; _anchors = code._anchors; _trackcount = code._trackcount; _options = options; // pick a name for the class int typenum = Interlocked.Increment(ref _typeCount); string typenumString = typenum.ToString(CultureInfo.InvariantCulture); runnertypename = typeprefix + "Runner" + typenumString ; runnerfactoryname = typeprefix + "Factory" + typenumString; // Generate a RegexRunner class // (blocks are simply illustrative) DefineType(runnertypename, false, typeof(RegexRunner)); { DefineMethod("Go", null); { GenerateGo(); BakeMethod(); } DefineMethod("FindFirstChar", typeof(bool)); { GenerateFindFirstChar(); BakeMethod(); } DefineMethod("InitTrackCount", null); { GenerateInitTrackCount(); BakeMethod(); } runnertype = BakeType(); } // Generate a RegexRunnerFactory class DefineType(runnerfactoryname, false, typeof(RegexRunnerFactory)); { DefineMethod("CreateInstance", typeof(RegexRunner)); { GenerateCreateInstance(runnertype); BakeMethod(); } factory = BakeType(); } return factory; }
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; } }
/* * The top-level driver. Initializes everything then calls the Generate* methods. */ internal RegexRunnerFactory FactoryInstanceFromCode(RegexCode code, RegexOptions options) { _code = code; _codes = code._codes; _strings = code._strings; _fcPrefix = code._fcPrefix; _bmPrefix = code._bmPrefix; _anchors = code._anchors; _trackcount = code._trackcount; _options = options; // pick a unique number for the methods we generate int regexnum = Interlocked.Increment(ref _regexCount); string regexnumString = regexnum.ToString(CultureInfo.InvariantCulture); DynamicMethod goMethod = DefineDynamicMethod("Go" + regexnumString, null, typeof(CompiledRegexRunner)); GenerateGo(); DynamicMethod firstCharMethod = DefineDynamicMethod("FindFirstChar" + regexnumString, typeof(bool), typeof(CompiledRegexRunner)); GenerateFindFirstChar(); DynamicMethod trackCountMethod = DefineDynamicMethod("InitTrackCount" + regexnumString, null, typeof(CompiledRegexRunner)); GenerateInitTrackCount(); return new CompiledRegexRunnerFactory(goMethod, firstCharMethod, trackCountMethod); }
internal CachedCodeEntry(CachedCodeEntryKey key, Hashtable capnames, string[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, SharedReference repl) { _key = key; _capnames = capnames; _capslist = capslist; _code = code; _caps = caps; _capsize = capsize; _runnerref = runner; _replref = repl; }
internal CachedCodeEntry(CachedCodeEntryKey key, Dictionary <string, int> capnames, String[] capslist, RegexCode code, Dictionary <Int32, Int32> caps, int capsize, ExclusiveReference runner, SharedReference repl) { _key = key; _capnames = capnames; _capslist = capslist; _code = code; _caps = caps; _capsize = capsize; _runnerref = runner; _replref = repl; }
internal void GenerateRegexType(string pattern, RegexOptions opts, string name, bool ispublic, RegexCode code, RegexTree tree, Type factory) { FieldInfo ft = this.RegexField("pattern"); FieldInfo info2 = this.RegexField("roptions"); FieldInfo info3 = this.RegexField("factory"); FieldInfo field = this.RegexField("caps"); FieldInfo info5 = this.RegexField("capnames"); FieldInfo info6 = this.RegexField("capslist"); FieldInfo info7 = this.RegexField("capsize"); Type[] parameterTypes = new Type[0]; this.DefineType(name, ispublic, typeof(Regex)); this._methbuilder = null; MethodAttributes @public = MethodAttributes.Public; base._ilg = this._typebuilder.DefineConstructor(@public, CallingConventions.Standard, parameterTypes).GetILGenerator(); base.Ldthis(); base._ilg.Emit(OpCodes.Call, typeof(Regex).GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[0], new ParameterModifier[0])); base.Ldthis(); base.Ldstr(pattern); base.Stfld(ft); base.Ldthis(); base.Ldc((int) opts); base.Stfld(info2); base.Ldthis(); base.Newobj(factory.GetConstructor(parameterTypes)); base.Stfld(info3); if (code._caps != null) { this.GenerateCreateHashtable(field, code._caps); } if (tree._capnames != null) { this.GenerateCreateHashtable(info5, tree._capnames); } if (tree._capslist != null) { base.Ldthis(); base.Ldc(tree._capslist.Length); base._ilg.Emit(OpCodes.Newarr, typeof(string)); base.Stfld(info6); for (int i = 0; i < tree._capslist.Length; i++) { base.Ldthisfld(info6); base.Ldc(i); base.Ldstr(tree._capslist[i]); base._ilg.Emit(OpCodes.Stelem_Ref); } } base.Ldthis(); base.Ldc(code._capsize); base.Stfld(info7); base.Ldthis(); base.Call(typeof(Regex).GetMethod("InitializeReferences", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)); base.Ret(); this._typebuilder.CreateType(); base._ilg = null; this._typebuilder = 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"); } //XXX: String key = ((int) options).ToString(NumberFormatInfo.InvariantInfo) + ":" + pattern; String key = ((int)options).ToString() + ":" + pattern; #if REGEX_USE_CACHE cached = LookupCached(key); #else cached = null; #endif 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 REGEX_USE_CACHE cachedentry = CacheCode(key); #endif } 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; } // // Singularity does not support compilation of regular expressions // // 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; // } // }
// The top-level driver. Initializes everything then calls the Generate* methods. internal Type FactoryFromCode(RegexCode code, RegexOptions options, String typeprefix) { String runnertypename; String runnerfactoryname; Type runnertype; Type factory; _code = code; _codes = code._codes; _strings = code._strings; _fcPrefix = code._fcPrefix; _scPrefix = code._scPrefix; _bmPrefix = code._bmPrefix; _anchors = code._anchors; _trackcount = code._trackcount; _options = options; // pick a name for the class lock (_syncObject) { // Note: Class names must be unique within assemblies, not just // within modules. We append the modulename to the runner name // to make our name unique across the assembly runnertypename = typeprefix + "Runner" + _typeCount.ToString(); runnerfactoryname = typeprefix + "Factory" + _typeCount.ToString(); _typeCount++; } // Generate a RegexRunner class // (blocks are simply illustrative) DefineType(runnertypename, false, typeof(RegexRunner)); { DefineMethod("Go", null); { GenerateGo(); BakeMethod(); } DefineMethod("FindFirstChar", typeof(bool)); { GenerateFindFirstChar(); BakeMethod(); } DefineMethod("InitTrackCount", null); { GenerateInitTrackCount(); BakeMethod(); } runnertype = BakeType(); } // Generate a RegexRunnerFactory class DefineType(runnerfactoryname, false, typeof(RegexRunnerFactory)); { DefineMethod("CreateInstance", typeof(RegexRunner)); { GenerateCreateInstance(runnertype); BakeMethod(); } factory = BakeType(); } return factory; }
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) { 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; } }
internal void AddCompiled(RegexRunnerFactory factory) { this._factory = factory; this._code = null; }