public static string Escape(string str) { if (str == null) { throw new ArgumentNullException("str"); } return(Parser.Escape(str)); }
// private private void Compile() { replacement = Parser.Unescape(replacement); int anchor = 0, ptr = 0, saveptr; char c; while (ptr < replacement.Length) { c = replacement [ptr++]; if (c != '$') { continue; } // If the '$' was the last character, just emit it as is if (ptr == replacement.Length) { break; } // If we saw a '$$' if (replacement [ptr] == '$') { // Everthing from 'anchor' upto and including the first '$' is copied from the replacement string AddFromReplacement(anchor, ptr); // skip over the second '$'. anchor = ++ptr; continue; } saveptr = ptr - 1; int from_match = CompileTerm(ref ptr); // We couldn't recognize the term following the '$'. Just treat it as a literal. // 'ptr' has already been advanced, no need to rewind it back if (from_match >= 0) { continue; } AddFromReplacement(anchor, saveptr); AddInt(from_match); anchor = ptr; } // If we never needed to advance anchor, it means the result is the whole replacement string. // We optimize that case by never allocating the pieces array. if (anchor != 0) { AddFromReplacement(anchor, ptr); } }
private static IMachineFactory CreateMachineFactory(string pattern, RegexOptions options) { Parser psr = new Parser(); RegularExpression re = psr.ParseRegularExpression(pattern, options); ICompiler cmp; //if ((options & RegexOptions.Compiled) != 0) // //throw new Exception ("Not implemented."); // cmp = new CILCompiler (); //else cmp = new PatternCompiler(); re.Compile(cmp, (options & RegexOptions.RightToLeft) != 0); IMachineFactory machineFactory = cmp.GetMachineFactory(); machineFactory.Mapping = psr.GetMapping(); machineFactory.NamesMapping = GetGroupNamesArray(machineFactory.GroupCount, machineFactory.Mapping); return(machineFactory); }
public Regex(string pattern, RegexOptions options) { this.pattern = pattern; this.roptions = options; this.machineFactory = cache.Lookup(pattern, options); if (this.machineFactory == null) { // parse and install group mapping Parser psr = new Parser(); RegularExpression re = psr.ParseRegularExpression(pattern, options); this.group_count = re.GroupCount; this.mapping = psr.GetMapping(); // compile ICompiler cmp; //if ((options & RegexOptions.Compiled) != 0) // //throw new Exception ("Not implemented."); // cmp = new CILCompiler (); //else cmp = new PatternCompiler(); re.Compile(cmp, RightToLeft); // install machine factory and add to pattern cache this.machineFactory = cmp.GetMachineFactory(); this.machineFactory.Mapping = mapping; cache.Add(pattern, options, this.machineFactory); } else { this.group_count = this.machineFactory.GroupCount; this.mapping = this.machineFactory.Mapping; } }
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); }
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); } }
public static string Unescape(string str) { return(Parser.Unescape(str)); }
public static string Escape(string str) { return(Parser.Escape(str)); }
public Regex (string pattern, RegexOptions options) { this.pattern = pattern; this.roptions = options; this.machineFactory = cache.Lookup (pattern, options); if (this.machineFactory == null) { // parse and install group mapping Parser psr = new Parser (); RegularExpression re = psr.ParseRegularExpression (pattern, options); this.group_count = re.GroupCount; this.mapping = psr.GetMapping (); // compile ICompiler cmp; //if ((options & RegexOptions.Compiled) != 0) // //throw new Exception ("Not implemented."); // cmp = new CILCompiler (); //else cmp = new PatternCompiler (); re.Compile (cmp, RightToLeft); // install machine factory and add to pattern cache this.machineFactory = cmp.GetMachineFactory (); this.machineFactory.Mapping = mapping; cache.Add (pattern, options, this.machineFactory); } else { this.group_count = this.machineFactory.GroupCount; this.mapping = this.machineFactory.Mapping; } }