Use native regexp helper in all user emulated classes. Change-Id: Ic4d20eecec33eb992ef010a1a55149e16b91b45a
diff --git a/user/super/com/google/gwt/emul/java/lang/Character.java b/user/super/com/google/gwt/emul/java/lang/Character.java index e699e2c..36077fa 100644 --- a/user/super/com/google/gwt/emul/java/lang/Character.java +++ b/user/super/com/google/gwt/emul/java/lang/Character.java
@@ -18,7 +18,8 @@ import static javaemul.internal.InternalPreconditions.checkCriticalArgument; import java.io.Serializable; -import java.lang.String.NativeRegExp; + +import javaemul.internal.NativeRegExp; /** * Wraps a native <code>char</code> as an object.
diff --git a/user/super/com/google/gwt/emul/java/lang/Number.java b/user/super/com/google/gwt/emul/java/lang/Number.java index c1da463..d775032 100644 --- a/user/super/com/google/gwt/emul/java/lang/Number.java +++ b/user/super/com/google/gwt/emul/java/lang/Number.java
@@ -18,6 +18,7 @@ import java.io.Serializable; import javaemul.internal.JsUtils; +import javaemul.internal.NativeRegExp; import jsinterop.annotations.JsMethod; /** @@ -28,7 +29,7 @@ /** * Stores a regular expression object to verify the format of float values. */ - private static Object floatRegex; + private static NativeRegExp floatRegex; // CHECKSTYLE_OFF: A special need to use unusual identifiers to avoid // introducing name collisions. @@ -341,17 +342,13 @@ if (floatRegex == null) { floatRegex = createFloatRegex(); } - return regexTest(floatRegex, str); + return floatRegex.test(str); } - private static native Object createFloatRegex() /*-{ + private static native NativeRegExp createFloatRegex() /*-{ return /^\s*[+-]?(NaN|Infinity|((\d+\.?\d*)|(\.\d+))([eE][+-]?\d+)?[dDfF]?)\s*$/; }-*/; - private static native boolean regexTest(Object regex, String value) /*-{ - return regex.test(value); - }-*/; - // CHECKSTYLE_ON public byte byteValue() {
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 c254a8a..d3c877e 100644 --- a/user/super/com/google/gwt/emul/java/lang/String.java +++ b/user/super/com/google/gwt/emul/java/lang/String.java
@@ -29,6 +29,7 @@ import javaemul.internal.EmulatedCharset; import javaemul.internal.HashCodes; import javaemul.internal.JsUtils; +import javaemul.internal.NativeRegExp; import javaemul.internal.annotations.DoNotInline; import jsinterop.annotations.JsMethod; import jsinterop.annotations.JsPackage; @@ -762,15 +763,6 @@ public native String toUpperCase(); } - @JsType(isNative = true, name = "RegExp", namespace = JsPackage.GLOBAL) - static class NativeRegExp { - public int lastIndex; - public NativeRegExp(String regex) { } - public NativeRegExp(String regex, String mode) { } - public native Object exec(String value); - public native boolean test(String value); - } - // CHECKSTYLE_OFF: Utility Methods for unboxed String. @JsMethod(name = "$create")
diff --git a/user/super/com/google/gwt/emul/java/math/BigDecimal.java b/user/super/com/google/gwt/emul/java/math/BigDecimal.java index a18d2f4..40098f3 100644 --- a/user/super/com/google/gwt/emul/java/math/BigDecimal.java +++ b/user/super/com/google/gwt/emul/java/math/BigDecimal.java
@@ -39,6 +39,7 @@ import java.io.Serializable; import javaemul.internal.JsUtils; +import javaemul.internal.NativeRegExp; /** * This class represents immutable arbitrary precision decimal numbers. Each @@ -138,7 +139,7 @@ /** * Stores a regular expression object to verify the format of unscaled bigdecimal values. */ - private static Object unscaledRegex; + private static NativeRegExp unscaledRegex; private static final int BI_SCALED_BY_ZERO_LENGTH = 11; @@ -405,17 +406,13 @@ unscaledRegex = createBigDecimalUnscaledRegex(); } - return regexTest(unscaledRegex, str); + return unscaledRegex.test(str); } - private static native Object createBigDecimalUnscaledRegex() /*-{ + private static native NativeRegExp createBigDecimalUnscaledRegex() /*-{ return /^[+-]?\d*$/i; }-*/; - private static native boolean regexTest(Object regex, String value) /*-{ - return regex.test(value); - }-*/; - private static double parseUnscaled(String str) { return isValidBigUnscaledDecimal(str) ? JsUtils.parseInt(str, 10) : Double.NaN; }
diff --git a/user/super/com/google/gwt/emul/java/math/MathContext.java b/user/super/com/google/gwt/emul/java/math/MathContext.java index fec0b4d..468e6fe 100644 --- a/user/super/com/google/gwt/emul/java/math/MathContext.java +++ b/user/super/com/google/gwt/emul/java/math/MathContext.java
@@ -39,6 +39,8 @@ import java.io.Serializable; +import javaemul.internal.NativeRegExp; + /** * Immutable objects describing settings such as rounding mode and digit * precision for the numerical operations provided by class {@link BigDecimal}. @@ -140,15 +142,15 @@ public MathContext(String val) { checkNotNull(val, "null string"); - String[] extractedValues = parseValue(val); + Object[] extractedValues = (Object[]) createParseRegexp().exec(val); if (extractedValues == null || extractedValues.length != 3) { throw new IllegalArgumentException("bad string format"); } try { - this.precision = Integer.parseInt(extractedValues[1]); + this.precision = Integer.parseInt((String) extractedValues[1]); // Can use RoundingMode.valueOf here because it is blacklisted in enum obfuscation. - this.roundingMode = RoundingMode.valueOf(extractedValues[2]); + this.roundingMode = RoundingMode.valueOf((String) extractedValues[2]); } catch (RuntimeException re) { // Ensure that we only throw IllegalArgumentException for any illegal value. throw new IllegalArgumentException("bad string format"); @@ -157,8 +159,8 @@ checkCriticalArgument(this.precision >= 0, "Digits < 0"); } - private static native String[] parseValue(String val) /*-{ - return /^precision=(\d+)\ roundingMode=(\w+)$/.exec(val); + private static native NativeRegExp createParseRegexp() /*-{ + return /^precision=(\d+)\ roundingMode=(\w+)$/; }-*/; /* Public Methods */
diff --git a/user/super/com/google/gwt/emul/java/nio/charset/Charset.java b/user/super/com/google/gwt/emul/java/nio/charset/Charset.java index b332dfb..1c70857 100644 --- a/user/super/com/google/gwt/emul/java/nio/charset/Charset.java +++ b/user/super/com/google/gwt/emul/java/nio/charset/Charset.java
@@ -23,6 +23,7 @@ import java.util.TreeMap; import javaemul.internal.EmulatedCharset; +import javaemul.internal.NativeRegExp; /** * A minimal emulation of {@link Charset}. @@ -56,15 +57,15 @@ return EmulatedCharset.UTF_8; } - if (!isLegalCharsetName(charsetName)) { + if (!createLegalCharsetNameRegex().test(charsetName)) { throw new IllegalCharsetNameException(charsetName); } else { throw new UnsupportedCharsetException(charsetName); } } - private static native boolean isLegalCharsetName(String name) /*-{ - return /^[A-Za-z0-9][\w-:\.\+]*$/.test(name); + private static native NativeRegExp createLegalCharsetNameRegex() /*-{ + return /^[A-Za-z0-9][\w-:\.\+]*$/; }-*/; private final String name;
diff --git a/user/super/com/google/gwt/emul/javaemul/internal/NativeRegExp.java b/user/super/com/google/gwt/emul/javaemul/internal/NativeRegExp.java new file mode 100644 index 0000000..e536763 --- /dev/null +++ b/user/super/com/google/gwt/emul/javaemul/internal/NativeRegExp.java
@@ -0,0 +1,31 @@ +/* + * Copyright 2016 Google Inc. + * + * 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 javaemul.internal; + +import jsinterop.annotations.JsPackage; +import jsinterop.annotations.JsType; + +/** + * Simple class to work with native js regular expressions. + */ +@JsType(isNative = true, name = "RegExp", namespace = JsPackage.GLOBAL) +public class NativeRegExp { + public int lastIndex; + public NativeRegExp(String regex) { } + public NativeRegExp(String regex, String mode) { } + public native Object exec(String value); + public native boolean test(String value); +}