-
Notifications
You must be signed in to change notification settings - Fork 9
More formatting options #21
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
0fa88be
3cbaafd
df7b249
f4251b6
07567ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Object> args = new ArrayList<Object>(); | ||
for (int i = 1; i < argCount; i ++) { | ||
Value v = ss.pop(); | ||
Object o = null; | ||
switch (v.type) { | ||
case Null: o = Value.null_(); break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is wrong, should just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
iirc I used this because I saw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me personally it would be really nice, if this PR is being merged. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check my new commits in case you're interested in accepting this PR. Should be fixed now. |
||
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_(); | ||
} | ||
} |
There was a problem hiding this comment.
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
ArrayList
constructor means this method will potentially cause several object arrays to be allocated as the list will have to be resizedit would be better to just use an object array directly and filling it with a reverse for i loop