Beispiel #1
0
        /*
         * This is the only function that should be called from outside.
         * It takes a RegexTree and creates a corresponding RegexCode.
         */
        internal static RegexCode Write(RegexTree t)
        {
            RegexWriter w      = new RegexWriter();
            RegexCode   retval = w.RegexCodeFromRegexTree(t);

#if DBG
            if (t.Debug)
            {
                t.Dump();
                retval.Dump();
            }
#endif
            return(retval);
        }
Beispiel #2
0
 /*
  * Emits a one-argument operation.
  */
 internal void Emit(int op, int opd1)
 {
     if (_counting)
     {
         _count += 2;
         if (RegexCode.OpcodeBacktracks(op))
         {
             _trackcount += 1;
         }
         return;
     }
     _emitted[_curpos++] = op;
     _emitted[_curpos++] = opd1;
 }
Beispiel #3
0
        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 |
                             RegexOptions.Timeboxed
#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;
            }

            if (UseOptionC())
            {
                runnerref = new ExclusiveReference();
                runnerref.Release(factory.CreateInstance());
            }
        }
Beispiel #4
0
 internal RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions)
 {
     return(RegexCompiler.Compile(code, roptions));
 }
Beispiel #5
0
 internal void AddCompiled(RegexRunnerFactory factory)
 {
     _factory = factory;
     _code    = null;
 }