コード例 #1
0
ファイル: Regex.cs プロジェクト: brass9/RegexStringBuilder
 public static string Escape(string str)
 {
     if (str == null)
     {
         throw new ArgumentNullException("str");
     }
     return(Parser.Escape(str));
 }
コード例 #2
0
ファイル: Regex.cs プロジェクト: brass9/RegexStringBuilder
        private static IMachineFactory CreateMachineFactory(string pattern, RegexOptions options)
        {
            Parser            psr = new Parser();
            RegularExpression re  = psr.ParseRegularExpression(pattern, options);

#if NET_2_1
            ICompiler cmp = new PatternCompiler();
#else
            ICompiler cmp;
            if (!old_rx)
            {
                if ((options & RegexOptions.Compiled) != 0)
                {
                    cmp = new CILCompiler();
                }
                else
                {
                    cmp = new RxCompiler();
                }
            }
            else
            {
                cmp = new PatternCompiler();
            }
#endif

            re.Compile(cmp, (options & RegexOptions.RightToLeft) != 0);

            IMachineFactory machineFactory = cmp.GetMachineFactory();
            Hashtable       mapping        = new Hashtable();
            machineFactory.Gap          = psr.GetMapping(mapping);
            machineFactory.Mapping      = mapping;
            machineFactory.NamesMapping = GetGroupNamesArray(machineFactory.GroupCount, machineFactory.Mapping);

            return(machineFactory);
        }
コード例 #3
0
        private int CompileTerm(ref int ptr)
        {
            char c = replacement [ptr];

            if (Char.IsDigit(c))                        // numbered group
            {
                int n = Parser.ParseDecimal(replacement, ref ptr);
                if (n < 0 || n > regex.GroupCount)
                {
                    return(0);
                }

                return(-n - 4);
            }

            ++ptr;

            switch (c)
            {
            case '{': {                                 // named group
                string name;
                int    n = -1;

                try {
                    // The parser is written such that there are few explicit range checks
                    // and depends on 'IndexOutOfRangeException' being thrown.

                    if (Char.IsDigit(replacement [ptr]))
                    {
                        n    = Parser.ParseDecimal(replacement, ref ptr);
                        name = "";
                    }
                    else
                    {
                        name = Parser.ParseName(replacement, ref ptr);
                    }
                } catch (IndexOutOfRangeException) {
                    ptr = replacement.Length;
                    return(0);
                }

                if (ptr == replacement.Length || replacement[ptr] != '}' || name == null)
                {
                    return(0);
                }
                ++ptr;                                  // Swallow the '}'

                if (name != "")
                {
                    n = regex.GroupNumberFromName(name);
                }

                if (n < 0 || n > regex.GroupCount)
                {
                    return(0);
                }

                return(-n - 4);
            }

            case '&':                                   // entire match.  Value should be same as $0
                return(-4);

            case '`':                                   // text before match
                return(-2);

            case '\'':                                  // text after match
                return(-3);

            case '+':                                   // last group
                return(-regex.GroupCount - 4);

            case '_':                                   // entire text
                return(-1);

            default:
                return(0);
            }
        }