Ensure "".split(",") returns { "" }.  This is the behavior of Sun's
JRE, although the JRE javadoc is somewhat ambiguous.

Review by: jlabanca



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7348 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/String.java b/user/super/com/google/gwt/emul/java/lang/String.java
index 1cd425d..7ed93bf 100644
--- a/user/super/com/google/gwt/emul/java/lang/String.java
+++ b/user/super/com/google/gwt/emul/java/lang/String.java
@@ -23,7 +23,6 @@
 package java.lang;
 
 import com.google.gwt.core.client.JavaScriptObject;
-import com.google.gwt.user.client.Window;
 
 import java.io.Serializable;
 import java.util.Comparator;
@@ -658,11 +657,11 @@
     // progress by intention
     var lastTrail = null;
     // We do the split manually to avoid Javascript incompatibility
-    while(true) {
+    while (true) {
       // None of the information in the match returned are useful as we have no 
       // subgroup handling
       var matchObj = compiled.exec(trail);
-      if( matchObj == null || trail == "" || 
+      if (matchObj == null || trail == "" || 
         (count == (maxMatch - 1) && maxMatch > 0)) {
         out[count] = trail;
         break;
@@ -680,8 +679,10 @@
         count++;
       }
     }
-    // all blank delimiters at the end are supposed to disappear if maxMatch == 0
-    if (maxMatch == 0) {
+    // all blank delimiters at the end are supposed to disappear if maxMatch == 0;
+    // however, if the input string is empty, the output should consist of a
+    // single empty string
+    if (maxMatch == 0 && this.length > 0) {
       var lastNonEmpty = out.length;
       while (lastNonEmpty > 0 && out[lastNonEmpty - 1] == "") {
         --lastNonEmpty;
@@ -742,7 +743,7 @@
   }-*/;
 
   public native String trim() /*-{
-    if(this.length == 0 || (this[0] > '\u0020' && this[this.length-1] > '\u0020')) {
+    if (this.length == 0 || (this[0] > '\u0020' && this[this.length-1] > '\u0020')) {
       return this;
     }
     var r1 = this.replace(/^(\s*)/, '');
diff --git a/user/test/com/google/gwt/emultest/java/lang/StringTest.java b/user/test/com/google/gwt/emultest/java/lang/StringTest.java
index 6c00aa9..fed0ffb 100644
--- a/user/test/com/google/gwt/emultest/java/lang/StringTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/StringTest.java
@@ -429,6 +429,14 @@
         0));
     // issue 2742
     compareList("issue2742", new String[] {}, hideFromCompiler("/").split("/", 0));
+    
+    // Splitting an empty string should result in an array containing a single
+    // empty string.
+    String[] s = "".split(",");
+    assertTrue(s != null);
+    assertTrue(s.length == 1);
+    assertTrue(s[0] != null);
+    assertTrue(s[0].length() == 0);
   }
 
   public void testStartsWith() {