diff --git a/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExp.java b/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExp.java new file mode 100644 index 000000000..d30a7a88a --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExp.java @@ -0,0 +1,83 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.core; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public abstract class JSRegExp implements JSObject { + @JSBody(params = "pattern", script = "return new RegExp(pattern);") + public static native JSRegExp create(String pattern); + + @JSBody(params = { "pattern", "flags" }, script = "return new RegExp(pattern, flags);") + public static native JSRegExp create(String pattern, String flags); + + public static JSRegExp create(String pattern, JSRegExpFlag... flags) { + boolean global = false; + boolean ignoreCase = false; + boolean multiline = false; + for (JSRegExpFlag flag : flags) { + switch (flag) { + case GLOBAL: + global = true; + break; + case IGNORE_CASE: + ignoreCase = true; + break; + case MULTILINE: + multiline = true; + break; + } + } + StringBuilder sb = new StringBuilder(); + if (global) { + sb.append('g'); + } + if (ignoreCase) { + sb.append('i'); + } + if (multiline) { + sb.append('m'); + } + return create(pattern, sb.toString()); + } + + @JSProperty + public abstract boolean isGlobal(); + + @JSProperty + public abstract boolean isIgnoreCase(); + + @JSProperty + public abstract boolean isMultiline(); + + @JSProperty + public abstract int getLastIndex(); + + @JSProperty + public abstract JSString getSource(); + + public abstract JSArray exec(JSString text); + + public abstract boolean test(JSString text); + + public abstract boolean test(String text); +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExpFlag.java b/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExpFlag.java new file mode 100644 index 000000000..3306f06a1 --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/core/JSRegExpFlag.java @@ -0,0 +1,26 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.core; + +/** + * + * @author Alexey Andreev + */ +public enum JSRegExpFlag { + GLOBAL, + IGNORE_CASE, + MULTILINE +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/core/JSReplaceFunction.java b/teavm-jso/src/main/java/org/teavm/jso/core/JSReplaceFunction.java new file mode 100644 index 000000000..ecedc544b --- /dev/null +++ b/teavm-jso/src/main/java/org/teavm/jso/core/JSReplaceFunction.java @@ -0,0 +1,28 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.core; + +import org.teavm.jso.JSFunctor; +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +@JSFunctor +public interface JSReplaceFunction extends JSObject { + String apply(String matched); +} diff --git a/teavm-jso/src/main/java/org/teavm/jso/core/JSString.java b/teavm-jso/src/main/java/org/teavm/jso/core/JSString.java index 1e240be24..da4446f50 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/core/JSString.java +++ b/teavm-jso/src/main/java/org/teavm/jso/core/JSString.java @@ -57,12 +57,32 @@ public abstract class JSString implements JSObject { public abstract int lastIndexOf(JSString a); + public abstract JSArray match(JSRegExp regexp); + + public abstract JSArray match(JSString regexp); + + public abstract JSString replace(JSRegExp regexp, JSString replaceBy); + + public abstract JSString replace(JSRegExp regexp, JSReplaceFunction replaceBy); + + public abstract JSString replace(JSString regexp, JSString replaceBy); + + public abstract JSString replace(JSString regexp, JSReplaceFunction replaceBy); + + public abstract int search(JSRegExp regexp); + + public abstract int search(JSString regexp); + public abstract JSString slice(int beginSlice); public abstract JSString slice(int beginSlice, int endSlice); + public abstract JSString[] split(JSRegExp separator); + public abstract JSString[] split(JSString separator); + public abstract JSString[] split(JSRegExp separator, int limit); + public abstract JSString[] split(JSString separator, int limit); public abstract JSString substr(int start); @@ -73,6 +93,12 @@ public abstract class JSString implements JSObject { public abstract JSString substring(int start, int end); + public abstract JSString toLowerCase(); + + public abstract JSString toUpperCase(); + + public abstract JSString trim(); + @JSBody(params = "obj", script = "return typeof this === 'string';") public static native boolean isInstance(JSObject obj); }