Browse Source

fixed method string.split() and unit tests part 2

fixed method string.split() and unit tests part 2
Frederic Torres 11 years ago
parent
commit
c5c9051d47
1 changed files with 25 additions and 6 deletions
  1. 25 6
      Jint/Native/String/StringPrototype.cs

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

@@ -1,5 +1,6 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 using System.Text;
 using Jint.Native.Array;
 using Jint.Native.Array;
 using Jint.Native.Function;
 using Jint.Native.Function;
@@ -162,10 +163,20 @@ namespace Jint.Native.String
 
 
             var rx = TypeConverter.ToObject(Engine, separator) as RegExpInstance;
             var rx = TypeConverter.ToObject(Engine, separator) as RegExpInstance;
 
 
-            if (rx != null)
+            const string regExpForMatchingAllCharactere = "(?:)";
+
+            if (rx != null &&
+                rx.Source != regExpForMatchingAllCharactere // We need pattern to be defined -> for s.split(new RegExp) 
+                )
             {
             {
                 var match = rx.Value.Match(s, 0);
                 var match = rx.Value.Match(s, 0);
 
 
+                if (!match.Success) // No match at all return the string in an array
+                {
+                    a.DefineOwnProperty("0", new PropertyDescriptor(s, true, true, true), false);
+                    return a;
+                }
+
                 int lastIndex = 0;
                 int lastIndex = 0;
                 int index = 0;
                 int index = 0;
                 while (match.Success && index < limit)
                 while (match.Success && index < limit)
@@ -213,17 +224,25 @@ namespace Jint.Native.String
             }
             }
             else
             else
             {
             {
-                var sep = TypeConverter.ToString(separator);
-                
-                var segments = s.Split(new [] { sep }, StringSplitOptions.None);
-                for (int i = 0; i < segments.Length && i < limit; i++)
+                var segments = new List<string>();
+
+                if (rx != null && rx.Source == regExpForMatchingAllCharactere) // for s.split(new RegExp)
+                {
+                    segments.AddRange(from object c in s select c.ToString());
+                }
+                else
+                {
+                    var sep = TypeConverter.ToString(separator);
+                    segments = s.Split(new[] {sep}, StringSplitOptions.None).ToList();
+                }
+
+                for (int i = 0; i < segments.Count && i < limit; i++)
                 {
                 {
                     a.DefineOwnProperty(i.ToString(), new PropertyDescriptor(segments[i], true, true, true), false);
                     a.DefineOwnProperty(i.ToString(), new PropertyDescriptor(segments[i], true, true, true), false);
                 }
                 }
             
             
                 return a;
                 return a;
             }
             }
-            
         }
         }
 
 
         private JsValue Slice(JsValue thisObj, JsValue[] arguments)
         private JsValue Slice(JsValue thisObj, JsValue[] arguments)