Revision: 7348 Author: [email protected] Date: Tue Dec 22 13:40:05 2009 Log: Ensure "".split(",") returns { "" }. This is the behavior of Sun's JRE, although the JRE javadoc is somewhat ambiguous.
Review by: jlabanca http://code.google.com/p/google-web-toolkit/source/detail?r=7348 Modified: /trunk/user/super/com/google/gwt/emul/java/lang/String.java /trunk/user/test/com/google/gwt/emultest/java/lang/StringTest.java ======================================= --- /trunk/user/super/com/google/gwt/emul/java/lang/String.java Tue Dec 22 11:53:08 2009 +++ /trunk/user/super/com/google/gwt/emul/java/lang/String.java Tue Dec 22 13:40:05 2009 @@ -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*)/, ''); ======================================= --- /trunk/user/test/com/google/gwt/emultest/java/lang/StringTest.java Mon Oct 26 18:35:41 2009 +++ /trunk/user/test/com/google/gwt/emultest/java/lang/StringTest.java Tue Dec 22 13:40:05 2009 @@ -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() { -- http://groups.google.com/group/Google-Web-Toolkit-Contributors
