From 0fa88be0f2a8b1e91be684c5a129d5810cc20416 Mon Sep 17 00:00:00 2001 From: Xenapte <17661805+Xenapte@users.noreply.github.com> Date: Tue, 23 Apr 2024 14:06:14 +0800 Subject: [PATCH 1/4] Add `format` and `formatDateTime` --- .../org/meteordev/starscript/StandardLib.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/main/java/org/meteordev/starscript/StandardLib.java b/src/main/java/org/meteordev/starscript/StandardLib.java index 1e5af49..983f810 100644 --- a/src/main/java/org/meteordev/starscript/StandardLib.java +++ b/src/main/java/org/meteordev/starscript/StandardLib.java @@ -3,7 +3,9 @@ import org.meteordev.starscript.value.Value; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.IllegalFormatException; import java.util.Random; /** Standard library with some default functions and variables. */ @@ -35,6 +37,10 @@ public static void init(Starscript ss) { ss.set("contains", StandardLib::contains); ss.set("replace", StandardLib::replace); ss.set("pad", StandardLib::pad); + + // Formatters + ss.set("formatDateTime", StandardLib::formatDateTime); + ss.set("format", StandardLib::format); } // Numbers @@ -166,4 +172,44 @@ public static Value pad(Starscript ss, int argCount) { return Value.string(new String(padded)); } + + public static Value formatDateTime(Starscript ss, int argCount) { + if (argCount != 1) ss.error("formatTime() requires 1 argument, got %d.", argCount); + try { + String fmt = ss.popString("Argument to formatTime(fmt) needs to be a string."); + SimpleDateFormat formatter = new SimpleDateFormat(fmt); + return Value.string(formatter.format(new Date())); + } + catch (IllegalArgumentException e) { + ss.error(e.toString()); + } + return Value.null_(); + } + + public static Value format(Starscript ss, int argCount) { + if (argCount < 1) ss.error("format(fmt, ...args) requires at least 1 argument, got %d.", argCount); + ArrayList args = new ArrayList(); + for (int i = 1; i < argCount; i ++) { + Value v = ss.pop(); + Object o = null; + switch (v.type) { + case Null: o = Value.null_(); break; + case Boolean: o = v.getBool(); break; + case Number: o = v.getNumber(); break; + case String: o = v.getString(); break; + case Function: o = v.getFunction(); break; + case Map: o = v.getMap(); break; + default: break; + } + args.add(0, o); + } + String fmt = ss.popString("Argument `fmt` to format() needs to be a string."); + try { + return Value.string(String.format(fmt, args.toArray())); + } + catch (IllegalFormatException e) { + ss.error(e.toString()); + } + return Value.null_(); + } } From df7b2493a401e0942154959f4b73e0bafb587005 Mon Sep 17 00:00:00 2001 From: Xenapte <17661805+Xenapte@users.noreply.github.com> Date: Fri, 28 Jun 2024 09:35:10 +0800 Subject: [PATCH 2/4] fix null formatting --- src/main/java/org/meteordev/starscript/StandardLib.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/meteordev/starscript/StandardLib.java b/src/main/java/org/meteordev/starscript/StandardLib.java index 983f810..f27b6ae 100644 --- a/src/main/java/org/meteordev/starscript/StandardLib.java +++ b/src/main/java/org/meteordev/starscript/StandardLib.java @@ -193,12 +193,12 @@ public static Value format(Starscript ss, int argCount) { Value v = ss.pop(); Object o = null; switch (v.type) { - case Null: o = Value.null_(); break; case Boolean: o = v.getBool(); break; case Number: o = v.getNumber(); break; case String: o = v.getString(); break; case Function: o = v.getFunction(); break; case Map: o = v.getMap(); break; + case Null: default: break; } args.add(0, o); From f4251b60d4b2234d418d5fe2923f908abafef24d Mon Sep 17 00:00:00 2001 From: Xenapte <17661805+Xenapte@users.noreply.github.com> Date: Fri, 28 Jun 2024 09:40:28 +0800 Subject: [PATCH 3/4] add option to specify timezone --- src/main/java/org/meteordev/starscript/StandardLib.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/meteordev/starscript/StandardLib.java b/src/main/java/org/meteordev/starscript/StandardLib.java index f27b6ae..8e6f7ea 100644 --- a/src/main/java/org/meteordev/starscript/StandardLib.java +++ b/src/main/java/org/meteordev/starscript/StandardLib.java @@ -7,6 +7,7 @@ import java.util.Date; import java.util.IllegalFormatException; import java.util.Random; +import java.util.TimeZone; /** Standard library with some default functions and variables. */ public class StandardLib { @@ -174,10 +175,13 @@ public static Value pad(Starscript ss, int argCount) { } public static Value formatDateTime(Starscript ss, int argCount) { - if (argCount != 1) ss.error("formatTime() requires 1 argument, got %d.", argCount); + if (argCount < 1 || argCount > 2) ss.error("formatTime(fmt, timezone) requires 1 to 2 arguments, got %d.", argCount); try { - String fmt = ss.popString("Argument to formatTime(fmt) needs to be a string."); + String timeZone = null; + if (argCount == 2) timeZone = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string."); + String fmt = ss.popString("Argument to formatTime(fmt, timezone) needs to be a string."); SimpleDateFormat formatter = new SimpleDateFormat(fmt); + if (timeZone != null && !timeZone.isEmpty()) formatter.setTimeZone(TimeZone.getTimeZone(timeZone)); return Value.string(formatter.format(new Date())); } catch (IllegalArgumentException e) { From 07567ba33f34d51a0704c9f442fb1837190cb5c1 Mon Sep 17 00:00:00 2001 From: Xenapte <17661805+Xenapte@users.noreply.github.com> Date: Sun, 30 Jun 2024 13:30:36 +0800 Subject: [PATCH 4/4] style improvements --- .../java/org/meteordev/starscript/StandardLib.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/meteordev/starscript/StandardLib.java b/src/main/java/org/meteordev/starscript/StandardLib.java index 8e6f7ea..e8f58c8 100644 --- a/src/main/java/org/meteordev/starscript/StandardLib.java +++ b/src/main/java/org/meteordev/starscript/StandardLib.java @@ -174,6 +174,8 @@ public static Value pad(Starscript ss, int argCount) { return Value.string(new String(padded)); } + // Formatters + public static Value formatDateTime(Starscript ss, int argCount) { if (argCount < 1 || argCount > 2) ss.error("formatTime(fmt, timezone) requires 1 to 2 arguments, got %d.", argCount); try { @@ -192,10 +194,10 @@ public static Value formatDateTime(Starscript ss, int argCount) { public static Value format(Starscript ss, int argCount) { if (argCount < 1) ss.error("format(fmt, ...args) requires at least 1 argument, got %d.", argCount); - ArrayList args = new ArrayList(); - for (int i = 1; i < argCount; i ++) { + Object[] args = new Object[argCount]; + for (int i = argCount - 1; i >= 1; i --) { Value v = ss.pop(); - Object o = null; + Object o; switch (v.type) { case Boolean: o = v.getBool(); break; case Number: o = v.getNumber(); break; @@ -203,13 +205,13 @@ public static Value format(Starscript ss, int argCount) { case Function: o = v.getFunction(); break; case Map: o = v.getMap(); break; case Null: - default: break; + default: o = null; break; } - args.add(0, o); + args[i] = o; } String fmt = ss.popString("Argument `fmt` to format() needs to be a string."); try { - return Value.string(String.format(fmt, args.toArray())); + return Value.string(String.format(fmt, args)); } catch (IllegalFormatException e) { ss.error(e.toString());