SortedSet: Enable set comparision operations on views, and improve performance
[mono.git] / mcs / class / System / System.Text.RegularExpressions / interpreter.cs
index 84a889ff81aae5c0d2fbfc06db8d36ac9094d3a2..0edc3239f6b177cb4c614ae99b2617dc553c2b04 100644 (file)
@@ -34,28 +34,34 @@ using System.Globalization;
 
 namespace System.Text.RegularExpressions {
 
-       class Interpreter : IMachine {
+       partial class Interpreter : BaseMachine {
+               private int ReadProgramCount (int ptr)
+               {
+                       int ret = program [ptr + 1];
+                       ret <<= 16;
+                       ret += program [ptr];
+                       return ret;
+               }
+
                public Interpreter (ushort[] program) {
                        this.program = program;
                        this.qs = null;
 
                        // process info block
-
                        Debug.Assert ((OpCode)program[0] == OpCode.Info, "Regex", "Cant' find info block");
-
-                       this.group_count = program[1] + 1;
-                       this.match_min = program[2];
-                       //this.match_max = program[3];
+                       this.group_count = ReadProgramCount (1) + 1;
+                       this.match_min = ReadProgramCount (3);
+                       //this.match_max = ReadProgramCount (5);
 
                        // setup
 
-                       this.program_start = 4;
+                       this.program_start = 7;
                        this.groups = new int [group_count];
                }
 
                // IMachine implementation
 
-               public Match Scan (Regex regex, string text, int start, int end) {
+               public override Match Scan (Regex regex, string text, int start, int end) {
                        this.text = text;
                        this.text_end = end;
                        this.scan_ptr = start;
@@ -106,16 +112,16 @@ namespace System.Text.RegularExpressions {
                                                //      True
 
                                                switch ((Position)program[pc + 4]) {
-                                               case Position.StartOfString:                                                    
+                                               case Position.StartOfString:
                                                        if (anch_reverse || anch_offset == 0) {
-                                                               ptr = anch_offset;
+                                                               if (anch_reverse)
+                                                                       ptr = anch_offset;
                                                                if (TryMatch (ref ptr, pc + skip))
                                                                        goto Pass;
                                                        }
                                                        break;
                                                
                                                case Position.StartOfLine:
-                                                                                                       
                                                         if (anch_ptr == 0) {
                                                                ptr = 0;
                                                                if (TryMatch (ref ptr, pc + skip))
@@ -161,11 +167,10 @@ namespace System.Text.RegularExpressions {
                                                //      True
                                 
                                                bool reverse = ((OpFlags)program[pc + 3] & OpFlags.RightToLeft) != 0;
-                                               string substring = GetString (pc + 3);
 
                                                if (qs == null) {
                                                        bool ignore = ((OpFlags)program[pc + 3] & OpFlags.IgnoreCase) != 0;
-
+                                                       string substring = GetString (pc + 3);
                                                        qs = new QuickSearch (substring, ignore, reverse);
                                                }
                                                while ((anch_reverse && anch_ptr >= anch_begin) 
@@ -175,7 +180,7 @@ namespace System.Text.RegularExpressions {
                                                        {
                                                                anch_ptr = qs.Search (text, anch_ptr, anch_begin);
                                                                if (anch_ptr != -1)
-                                                                       anch_ptr += substring.Length ;
+                                                                       anch_ptr += qs.Length ;
                                                                
                                                        }
                                                        else
@@ -300,12 +305,13 @@ namespace System.Text.RegularExpressions {
                                                goto Fail;
 
                                        pc += 2;
-                                       for (int i = 0; i < len; ++ i) {
-                                               if (ignore) {
+                                       if (ignore) {
+                                               for (int i = 0; i < len; ++ i) {
                                                        if (Char.ToLower (text[ptr + i]) != Char.ToLower (text[str + i]))
                                                                goto Fail;
                                                }
-                                               else {
+                                       } else {
+                                               for (int i = 0; i < len; ++ i) {
                                                        if (text[ptr + i] != text[str + i])
                                                                goto Fail;
                                                }
@@ -420,10 +426,10 @@ namespace System.Text.RegularExpressions {
                                case OpCode.Repeat: {
                                        this.repeat = new RepeatContext (
                                                this.repeat,                    // previous context
-                                               program[pc + 2],                // minimum
-                                               program[pc + 3],                // maximum
+                                               ReadProgramCount (pc + 2),              // minimum
+                                               ReadProgramCount (pc + 4),              // maximum
                                                (flags & OpFlags.Lazy) != 0,    // lazy
-                                               pc + 4                          // subexpression
+                                               pc + 6                          // subexpression
                                        );
 
                                        if (Eval (Mode.Match, ref ptr, pc + program[pc + 1]))
@@ -561,10 +567,10 @@ namespace System.Text.RegularExpressions {
                                case OpCode.FastRepeat: {
                                        this.fast = new RepeatContext (
                                                fast,
-                                               program[pc + 2],                // minimum
-                                               program[pc + 3],                // maximum
+                                               ReadProgramCount (pc + 2),              // minimum
+                                               ReadProgramCount (pc + 4),              // maximum
                                                (flags & OpFlags.Lazy) != 0,    // lazy
-                                               pc + 4                          // subexpression
+                                               pc + 6                          // subexpression
                                        );
 
                                        fast.Start = ptr;
@@ -574,13 +580,17 @@ namespace System.Text.RegularExpressions {
                                        pc += program[pc + 1];          // tail expression
                                        ushort tail_word = program[pc];
 
-                                       int c1, c2;                     // first character of tail operator
-                                       int coff;                       // 0 or -1 depending on direction
+                                       int c1 = -1;            // first character of tail operator
+                                       int c2 = -1;            // ... and the same character, in upper case if ignoring case
+                                       int coff = 0;           // 0 or -1 depending on direction
 
                                        OpCode tail_op = (OpCode)(tail_word & 0xff);
                                        if (tail_op == OpCode.Character || tail_op == OpCode.String) {
                                                OpFlags tail_flags = (OpFlags)(tail_word & 0xff00);
 
+                                               if ((tail_flags & OpFlags.Negate) != 0)
+                                                       goto skip;
+
                                                if (tail_op == OpCode.String)
                                                {
                                                        int offset = 0;
@@ -605,11 +615,8 @@ namespace System.Text.RegularExpressions {
                                                else
                                                        coff = 0;
                                        }
-                                       else {
-                                               c1 = c2 = -1;
-                                               coff = 0;
-                                       }
 
+                               skip:
                                        if (fast.IsLazy) {
                                                if (!fast.IsMinimum && !Eval (Mode.Count, ref ptr, fast.Expression)) {
                                                        //Console.WriteLine ("lazy fast: failed mininum.");
@@ -913,9 +920,7 @@ namespace System.Text.RegularExpressions {
                                //Group not previously matched
                                return false;
                        }
-
                        Debug.Assert (marks [b].IsDefined, "Regex", "Balancng group not closed");
-
                        if (gid > 0 && capture){ 
                                Open (gid, marks [b].Index + marks [b].Length);
                                Close (gid, ptr);
@@ -933,7 +938,6 @@ namespace System.Text.RegularExpressions {
 
                private void Backtrack (int cp) {
                        Debug.Assert (cp > mark_start, "Regex", "Attempt to backtrack forwards");
-
                        for (int i = 0; i < groups.Length; ++ i) {
                                int m = groups [i];
                                while (cp <= m)
@@ -1012,6 +1016,11 @@ namespace System.Text.RegularExpressions {
                        int n_caps, first_mark_index;
                        Group g;
                        GetGroupInfo (0, out first_mark_index, out n_caps);
+
+                       // Avoid fully populating the Match instance if not needed
+                       if (!needs_groups_or_captures)
+                               return new Match (regex, this, text, text_end, 0, marks [first_mark_index].Index, marks [first_mark_index].Length);
+
                        Match retval = new Match (regex, this, text, text_end, groups.Length, 
                                                  marks [first_mark_index].Index, marks [first_mark_index].Length, n_caps);
                        PopulateGroup (retval, first_mark_index, n_caps);