Browse Source

fix string.replace unit tests suites 15_5_4_10 15_5_4_11

fix string.replace unit tests suites 15_5_4_10 15_5_4_11
Frederic Torres 11 years ago
parent
commit
b9b14a09d4
2 changed files with 38 additions and 12 deletions
  1. 20 6
      Jint/Native/RegExp/RegExpPrototype.cs
  2. 18 6
      Jint/Native/String/StringPrototype.cs

+ 20 - 6
Jint/Native/RegExp/RegExpPrototype.cs

@@ -96,6 +96,14 @@ namespace Jint.Native.RegExp
                 i = 0;
                 i = 0;
             }
             }
 
 
+            if (R.Source == "(?:)")  // Reg Exp is really ""
+            {
+                // "aaa".match() => [ '', index: 0, input: 'aaa' ]
+                var aa = InitReturnValueArray(Engine.Array.Construct(Arguments.Empty), s, 1, 0);
+                aa.DefineOwnProperty("0", new PropertyDescriptor("", true, true, true), true);
+                return aa;
+            }
+
             Match r = null;
             Match r = null;
             if (i < 0 || i >= length)
             if (i < 0 || i >= length)
             {
             {
@@ -118,20 +126,26 @@ namespace Jint.Native.RegExp
                 R.Put("lastIndex", (double) e, true);
                 R.Put("lastIndex", (double) e, true);
             }
             }
             var n = r.Groups.Count;
             var n = r.Groups.Count;
-            var a = Engine.Array.Construct(Arguments.Empty);
             var matchIndex = r.Index;
             var matchIndex = r.Index;
-            a.DefineOwnProperty("index", new PropertyDescriptor(matchIndex, writable: true, enumerable: true, configurable: true), true);
-            a.DefineOwnProperty("input", new PropertyDescriptor(s, writable: true, enumerable: true, configurable: true), true);
-            a.DefineOwnProperty("length", new PropertyDescriptor(value: n, writable:false, enumerable: false, configurable:false), true);
+
+            var a = InitReturnValueArray(Engine.Array.Construct(Arguments.Empty), s, n, matchIndex);
+            
             for (var k = 0; k < n; k++)
             for (var k = 0; k < n; k++)
             {
             {
                 var group = r.Groups[k];
                 var group = r.Groups[k];
                 var value = group.Success ? group.Value : Undefined.Instance;
                 var value = group.Success ? group.Value : Undefined.Instance;
-                a.DefineOwnProperty(k.ToString(), new PropertyDescriptor(value, true, true, true), true);
-            
+                a.DefineOwnProperty(k.ToString(), new PropertyDescriptor(value, true, true, true), true);            
             }
             }
 
 
             return a;
             return a;
         }
         }
+
+        private static Object.ObjectInstance InitReturnValueArray(Object.ObjectInstance array, string inputValue, int lengthValue, int indexValue)
+        {
+            array.DefineOwnProperty("index", new PropertyDescriptor(indexValue, writable: true, enumerable: true, configurable: true), true);
+            array.DefineOwnProperty("input", new PropertyDescriptor(inputValue, writable: true, enumerable: true, configurable: true), true);
+            array.DefineOwnProperty("length", new PropertyDescriptor(value: lengthValue, writable: false, enumerable: false, configurable: false), true);
+            return array;
+        }
     }
     }
 }
 }

+ 18 - 6
Jint/Native/String/StringPrototype.cs

@@ -252,6 +252,7 @@ namespace Jint.Native.String
             var searchValue = arguments.At(0);
             var searchValue = arguments.At(0);
             var replaceValue = arguments.At(1);
             var replaceValue = arguments.At(1);
 
 
+            // If the second parameter is not a function we create one
             var replaceFunction = replaceValue.TryCast<FunctionInstance>();
             var replaceFunction = replaceValue.TryCast<FunctionInstance>();
             if (replaceFunction == null)
             if (replaceFunction == null)
             {
             {
@@ -299,16 +300,16 @@ namespace Jint.Native.String
                                     matchNumber2 = matchNumber1 * 10 + (replaceString[i + 1] - '0');
                                     matchNumber2 = matchNumber1 * 10 + (replaceString[i + 1] - '0');
 
 
                                 // Try the two digit capture first.
                                 // Try the two digit capture first.
-                                if (matchNumber2 > 0 && matchNumber2 < args.Length - 3)
+                                if (matchNumber2 > 0 && matchNumber2 < args.Length - 2)
                                 {
                                 {
                                     // Two digit capture replacement.
                                     // Two digit capture replacement.
-                                    replacementBuilder.Append(TypeConverter.ToString(args[matchNumber2 + 1]));
+                                    replacementBuilder.Append(TypeConverter.ToString(args[matchNumber2]));
                                     i++;
                                     i++;
                                 }
                                 }
-                                else if (matchNumber1 > 0 && matchNumber1 < args.Length - 3)
+                                else if (matchNumber1 > 0 && matchNumber1 < args.Length - 2)
                                 {
                                 {
                                     // Single digit capture replacement.
                                     // Single digit capture replacement.
-                                    replacementBuilder.Append(TypeConverter.ToString(args[matchNumber1 + 1]));
+                                    replacementBuilder.Append(TypeConverter.ToString(args[matchNumber1]));
                                 }
                                 }
                                 else
                                 else
                                 {
                                 {
@@ -333,6 +334,16 @@ namespace Jint.Native.String
             }
             }
 
 
             // searchValue is a regular expression
             // searchValue is a regular expression
+
+            if (searchValue.IsNull()) 
+            {
+                searchValue = new JsValue(Null.Text);
+            }
+            if (searchValue.IsUndefined())
+            {
+                searchValue = new JsValue(Undefined.Text);
+            }
+            
             var rx = TypeConverter.ToObject(Engine, searchValue) as RegExpInstance;
             var rx = TypeConverter.ToObject(Engine, searchValue) as RegExpInstance;
             if (rx != null)
             if (rx != null)
             {
             {
@@ -340,7 +351,7 @@ namespace Jint.Native.String
                 string result = rx.Value.Replace(thisString, match =>
                 string result = rx.Value.Replace(thisString, match =>
                 {
                 {
                     var args = new List<JsValue>();
                     var args = new List<JsValue>();
-                    args.Add(match.Value);
+                    //if (match.Groups.Count == 0)  args.Add(match.Value);
                     for (var k = 0; k < match.Groups.Count; k++)
                     for (var k = 0; k < match.Groups.Count; k++)
                     {
                     {
                         var group = match.Groups[k];
                         var group = match.Groups[k];
@@ -351,7 +362,8 @@ namespace Jint.Native.String
                     args.Add(match.Index);
                     args.Add(match.Index);
                     args.Add(thisString);
                     args.Add(thisString);
 
 
-                    return TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
+                    var v = TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
+                    return v;
                 }, rx.Global == true ? -1 : 1);
                 }, rx.Global == true ? -1 : 1);
 
 
                 // Set the deprecated RegExp properties if at least one match was found.
                 // Set the deprecated RegExp properties if at least one match was found.