Skip to content

Commit

Permalink
remove some stray regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 15, 2020
1 parent e8d37ed commit 55742ef
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,12 @@ public boolean matchesBoolean() {

public static AsciiMatcher INTEGER_MATCHER = new AsciiMatcher("0123456789+-");

public static AsciiMatcher DOUBLE_MATCHER = new AsciiMatcher("0123456789.+-eE");

public boolean matchesInteger() {
return matchesFloat();
}

public boolean matchesFloat() {
return lower_value.length() > 0 && DOUBLE_MATCHER.isOnlyMatches(lower_value);
return ArgumentHelper.matchesDouble(lower_value);
}

// Check if this argument matches a certain ObjectTag type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.denizenscript.denizencore.objects;

import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.debugging.Debug;

import java.util.*;
import java.util.regex.Pattern;

public class ArgumentHelper {

public final static Pattern floatPrimitive = Pattern.compile("^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$");

// <--[language]
// @name Number and Decimal
// @group Common Terminology
Expand All @@ -20,7 +18,6 @@ public class ArgumentHelper {
// Numbers can be verified with the 'if' commands' 'matches' functionality.
// For example: "- if <number> matches number" ... will return true if <number> is a valid number.
// -->
public final static Pattern doublePrimitive = floatPrimitive;

// <--[language]
// @name Percentage
Expand All @@ -41,8 +38,6 @@ public class ArgumentHelper {
//
// -->

public final static Pattern integerPrimitive = Pattern.compile("(-)?[0-9]+");

/**
* Turns a list of string arguments (separated by buildArgs) into Argument
* Objects for easy matching and ObjectTag creation throughout Denizen.
Expand Down Expand Up @@ -137,11 +132,13 @@ public static String debugUniqueObj(String prefix, String id, Object value) {
return "<G>" + prefix + "='<A>" + id + "<Y>(" + (value != null ? value.toString() : "null") + ")<G>' ";
}

public static AsciiMatcher DOUBLE_MATCHER = new AsciiMatcher("0123456789.+-eE");

public static boolean matchesDouble(String arg) {
return doublePrimitive.matcher(arg).matches();
return arg.length() > 0 && DOUBLE_MATCHER.isOnlyMatches(arg);
}

public static boolean matchesInteger(String arg) {
return doublePrimitive.matcher(arg).matches();
return arg.length() > 0 && Argument.INTEGER_MATCHER.isOnlyMatches(arg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Pattern;

public class ObjectFetcher {

Expand Down Expand Up @@ -140,7 +139,9 @@ public static Class getObjectClass(String id) {
}
}

public final static Pattern DESCRIBED_PATTERN = Pattern.compile("[^\\[]+\\[.+=.+\\]", Pattern.DOTALL | Pattern.MULTILINE);
public static boolean isObjectWithProperties(String input) {
return input.indexOf('[') != -1 && input.lastIndexOf(']') == input.length() - 1;
}

public static boolean checkMatch(Class<? extends ObjectTag> dClass, String value) {
if (value == null || dClass == null) {
Expand All @@ -167,7 +168,7 @@ public static <T extends ObjectTag> T getObjectFrom(Class<T> dClass, String valu
}

public static List<String> separateProperties(String input) {
if (input.indexOf('[') == -1 || input.lastIndexOf(']') != input.length() - 1) {
if (!isObjectWithProperties(input)) {
return null;
}
ArrayList<String> output = new ArrayList<>(input.length() / 7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public static CustomObjectTag valueOf(String string, TagContext context) {

///////
// Handle objects with properties through the object fetcher
m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
if (ObjectFetcher.isObjectWithProperties(string)) {
return ObjectFetcher.getObjectFrom(CustomObjectTag.class, string, context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public class ElementTag implements ObjectTag {
// In some cases, this will also be documented as "<#.#>".
// -->

final static Pattern VALUE_PATTERN =
Pattern.compile("el@val(?:ue)?\\[([^\\[\\]]+)\\].*",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);

public static ElementTag valueOf(String string) {
return valueOf(string, null);
}
Expand All @@ -109,15 +105,10 @@ public static ElementTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}

Matcher m = VALUE_PATTERN.matcher(string);

// Allow construction of elements with el@val[<value>]
if (m.matches()) {
String value = m.group(1);
return new ElementTag(value);
if (string.startsWith("el@val[") && string.endsWith("]")) {
return new ElementTag(string.substring("el@val[".length(), string.length() - "el@val[]".length()));
}

return new ElementTag(CoreUtilities.toLowerCase(string).startsWith("el@") ? string.substring(3) : string);
}

Expand Down Expand Up @@ -479,7 +470,7 @@ public static void registerTags() {
// Returns whether the element is a valid decimal number (the decimal point is optional).
// -->
registerTag("is_decimal", (attribute, object) -> {
if (!ArgumentHelper.doublePrimitive.matcher(object.element).matches()) {
if (!ArgumentHelper.matchesDouble(object.element)) {
return new ElementTag(false);
}
try {
Expand All @@ -499,8 +490,7 @@ public static void registerTags() {
// -->
registerTag("is_odd", (attribute, object) -> {
String element = object.element;
return new ElementTag(ArgumentHelper.doublePrimitive.matcher(element).matches()
&& (object.asBigDecimal().longValue() % 2) == 1);
return new ElementTag(ArgumentHelper.matchesDouble(element) && (object.asBigDecimal().longValue() % 2) == 1);
});

// <--[tag]
Expand All @@ -512,8 +502,7 @@ public static void registerTags() {
// -->
registerTag("is_even", (attribute, object) -> {
String element = object.element;
return new ElementTag(ArgumentHelper.doublePrimitive.matcher(element).matches()
&& (object.asBigDecimal().longValue() % 2) == 0);
return new ElementTag(ArgumentHelper.matchesDouble(element) && (object.asBigDecimal().longValue() % 2) == 0);
});

// <--[tag]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.BracedCommand;

Expand Down

0 comments on commit 55742ef

Please sign in to comment.