* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / RegExpPrototype.cs
index f18e6a1bde30d4dec08ebaea24e251e2d700d355..9ea0bb721b5bc2f61af6010a53dbcf5e426e37fd 100644 (file)
@@ -5,6 +5,7 @@
 //     Cesar Lopez Nataren (cesar@ciencias.unam.mx)
 //
 // (C) 2003, Cesar Lopez Nataren
+// (C) Copyright 2005, Novell Inc (http://novell.com)
 //
 
 //
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-namespace Microsoft.JScript.Tmp
-{
-       using System;
+using System;
+using System.Text.RegularExpressions;
+using System.Collections;
 
-       public class RegExpPrototype : JSObject
-       {
+namespace Microsoft.JScript {  
+
+       public class RegExpPrototype : JSObject {
+
+               internal static RegExpPrototype Proto = new RegExpPrototype ();
+
+               [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.RegExp_compile)]
                public static RegExpObject compile (object thisObj, object source, object flags)
                {
-                       throw new NotImplementedException ();
-               }
+                       //
+                       // Note: We always compile RegExp internals so all this method is useful for is for
+                       // changing the properties of the otherwise immutable RegExp objects.
+                       //
+                       RegExpObject re = Convert.ToRegExp (thisObj);
+                       string flag_str = Convert.ToString (flags);
 
+                       re.Initialize (Convert.ToString (source),
+                               flag_str.IndexOfAny (new char [] { 'i' }) > -1,
+                               flag_str.IndexOfAny (new char [] { 'g' }) > -1,
+                               flag_str.IndexOfAny (new char [] { 'm' }) > -1);
+                       return re;
+               }
 
                public static RegExpConstructor constructor {
-                       get { throw new NotImplementedException (); }
+                       get { return RegExpConstructor.Ctr; }
                }
 
-
+               [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.RegExp_exec)]
                public static object exec (object thisObj, object input)
                {
-                       throw new NotImplementedException ();
+                       RegExpObject re = Convert.ToRegExp (thisObj);
+                       string str = null;
+                       if (input == null) {
+                               RegExpConstructor ctr = RegExpConstructor.Ctr;
+                               str = Convert.ToString (ctr.GetField ("$_").GetValue ("$_"));
+                       } else
+                               str = Convert.ToString (input);
+                       bool global = re.global;
+                       int lastIndex = global ? (int) ((double) re.lastIndex) : 0;
+                       bool success = lastIndex >= 0 && lastIndex <= str.Length;
+
+                       Match md = null;
+                       if (success) {
+                               md = re.regex.Match (str, lastIndex);
+                               success = md.Success;
+                       }
+
+                       if (!success) {
+                               re.lastIndex = 0;
+                               return DBNull.Value;
+                       }
+
+                       int index = md.Index;
+                       int endIndex = index + md.Length;
+                       if (global)
+                               re.lastIndex = endIndex;
+                       RegExpConstructor.UpdateLastMatch (md, str);
+
+                       GroupCollection caps = md.Groups;
+                       uint len = (uint) caps.Count;
+                       RegExpMatch result = new RegExpMatch ();
+
+                       result.AddField ("index", index);
+                       result.AddField ("input", input);
+                       result.length = len;
+                       for (uint j = 0; j < len; j++)
+                               result.elems [j] = caps [(int) j].Value;
+
+                       return result;
                }
 
 
+               [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.RegExp_test)]
                public static bool test (object thisObj, object input)
                {
-                       throw new NotImplementedException ();
+                       return exec (thisObj, input) != DBNull.Value;
                }
 
-
+               [JSFunctionAttribute (JSFunctionAttributeEnum.HasThisObject, JSBuiltin.RegExp_toString)]
                public static string toString (object thisObj)
                {
-                       throw new NotImplementedException ();
+                       SemanticAnalyser.assert_type (thisObj, typeof (RegExpObject));
+                       RegExpObject re = (RegExpObject) thisObj;
+                       return re.ToString ();
                }
        }
-}
\ No newline at end of file
+}