Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More formatting options #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/org/meteordev/starscript/StandardLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
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;
import java.util.TimeZone;

/** Standard library with some default functions and variables. */
public class StandardLib {
Expand Down Expand Up @@ -35,6 +38,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
Expand Down Expand Up @@ -166,4 +173,47 @@ 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 || argCount > 2) ss.error("formatTime(fmt, timezone) requires 1 to 2 arguments, got %d.", argCount);
try {
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) {
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<Object> args = new ArrayList<Object>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current implementation is less than ideal in performance

  1. using the no-arg ArrayList constructor means this method will potentially cause several object arrays to be allocated as the list will have to be resized
  2. inserting objects at index 0 means that the other elements will have to be offset constantly

it would be better to just use an object array directly and filling it with a reverse for i loop

for (int i = 1; i < argCount; i ++) {
Value v = ss.pop();
Object o = null;
switch (v.type) {
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of initializing Object o as null and simply breaking out of the switch on case Null/default, it would be more readable if o wasn't initialized to a value, but case Null/default set it to null

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_();
}
}