# HG changeset patch # User Jaroslav Tulach # Date 1378733172 -7200 # Node ID 3ee4ec9577bc3d9cdbfda8a9d9ddeff5c369ca7a # Parent 46e2b4ef85a443453942bc15e9b4f270a3af16eb Basic implementation of String.split diff -r 46e2b4ef85a4 -r 3ee4ec9577bc rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/RegExpSplitTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/RegExpSplitTest.java Mon Sep 09 15:26:12 2013 +0200 @@ -0,0 +1,50 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import java.util.Arrays; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class RegExpSplitTest { + + public @Compare Object splitSpace() { + return Arrays.asList("How are you today?".split(" ")); + } + + public @Compare Object splitSpaceTrimMinusOne() { + return Arrays.asList(" How are you today? ".split(" ", -1)); + } + + public @Compare Object splitSpaceTrimZero() { + return Arrays.asList(" How are you today? ".split(" ", 0)); + } + + public @Compare Object splitSpaceLimit2() { + return Arrays.asList("How are you today?".split(" ", 2)); + } + + @Factory public static Object[] create() { + return VMTest.create(RegExpSplitTest.class); + } +} diff -r 46e2b4ef85a4 -r 3ee4ec9577bc rt/emul/mini/src/main/java/java/lang/String.java --- a/rt/emul/mini/src/main/java/java/lang/String.java Mon Sep 09 06:08:00 2013 +0200 +++ b/rt/emul/mini/src/main/java/java/lang/String.java Mon Sep 09 15:26:12 2013 +0200 @@ -2315,8 +2315,35 @@ * @spec JSR-51 */ public String[] split(String regex, int limit) { - throw new UnsupportedOperationException("Needs regexp"); + if (limit <= 0) { + Object[] arr = splitImpl(this, regex, Integer.MAX_VALUE); + int to = arr.length; + if (limit == 0) { + while (to > 1 && ((String)arr[--to]).isEmpty()) { + } + to++; + } + String[] ret = new String[to]; + System.arraycopy(arr, 0, ret, 0, to); + return ret; + } else { + Object[] arr = splitImpl(this, regex, limit); + String[] ret = new String[arr.length]; + int pos = 0; + for (int i = 0; i < arr.length; i++) { + final String s = (String)arr[i]; + ret[i] = s; + pos = indexOf(s, pos) + s.length(); + } + ret[arr.length - 1] += substring(pos); + return ret; + } } + + @JavaScriptBody(args = { "str", "regex", "limit"}, body = + "return str.split(new RegExp(regex), limit);" + ) + private static native Object[] splitImpl(String str, String regex, int limit); /** * Splits this string around matches of the given