Skip to content

Commit

Permalink
ElementTag number_to_hex and hex_to_number
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 23, 2021
1 parent 0d8b3a0 commit 640a5ac
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Expand Up @@ -18,6 +18,8 @@ public class SystemTimeScriptEvent extends ScriptEvent {
//
// @Regex ^on system time( (\d\d\:\d\d|hourly|minutely|secondly))?$
//
// @Synonyms cron
//
// @Group Core
//
// @Triggers when the system time changes to the specified value.
Expand Down
Expand Up @@ -110,11 +110,12 @@ public static <T extends ObjectTag> String debugList(String prefix, Collection<T
}
}

private static String DIGITS = "0123456789", PREFIXES = "+-", DOUBLE_CHARS = "eE";
private static AsciiMatcher DIGIT_MATCHER = new AsciiMatcher(DIGITS);
private static AsciiMatcher INTEGER_MATCHER = new AsciiMatcher(DIGITS + PREFIXES);
private static AsciiMatcher DOUBLE_SPECIAL_MATCHER = new AsciiMatcher(DOUBLE_CHARS);
private static AsciiMatcher PREFIX_MATCHER = new AsciiMatcher(PREFIXES);
public static String DIGITS = "0123456789", PREFIXES = "+-", DOUBLE_CHARS = "eE";
public static AsciiMatcher DIGIT_MATCHER = new AsciiMatcher(DIGITS);
public static AsciiMatcher INTEGER_MATCHER = new AsciiMatcher(DIGITS + PREFIXES);
public static AsciiMatcher DOUBLE_SPECIAL_MATCHER = new AsciiMatcher(DOUBLE_CHARS);
public static AsciiMatcher PREFIX_MATCHER = new AsciiMatcher(PREFIXES);
public static AsciiMatcher HEX_MATCHER = new AsciiMatcher("abcdefABCDEF0123456789");

public static boolean matchesDouble(String arg) {
if (arg.length() == 0) {
Expand Down
Expand Up @@ -2302,6 +2302,40 @@ else if (CoreUtilities.toLowerCase(element).contains(CoreUtilities.toLowerCase(c
return new ElementTag(Math.round(ele.asDouble()));
});

// <--[tag]
// @attribute <ElementTag.number_to_hex>
// @returns ElementTag
// @group conversion
// @description
// Encodes base-10 integer number to hexadecimal (base-16) format.
// For example input of "15" will return "F".
// See also <@link tag ElementTag.hex_to_number>
// -->
registerTag("number_to_hex", (attribute, object) -> {
if (!object.isInt()) {
attribute.echoError("Element '" + object + "' is not a valid number!");
return null;
}
return new ElementTag(Long.toHexString(object.asLong()));
});

// <--[tag]
// @attribute <ElementTag.hex_to_number>
// @returns ElementTag(Number)
// @group conversion
// @description
// Encodes base-16 hexadecimal value to an integer number.
// For example input of "F" will return "15".
// See also <@link tag ElementTag.number_to_hex>
// -->
registerTag("hex_to_number", (attribute, object) -> {
if (!ArgumentHelper.HEX_MATCHER.isOnlyMatches(object.element)) {
attribute.echoError("Element '" + object + "' is not a valid hexadecimal number!");
return null;
}
return new ElementTag(Long.parseLong(object.element, 16));
});

// <--[tag]
// @attribute <ElementTag.base64_encode>
// @returns ElementTag
Expand Down

0 comments on commit 640a5ac

Please sign in to comment.