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

Conversation

Xenapte
Copy link

@Xenapte Xenapte commented Apr 23, 2024

This commit adds format and formatDateTime to Starscript. Yes, I know one of the selling points of Starscript is to be faster than String.format, but sometimes you do need more formatting options.

format(fmt, ...args)

Adds arbitrary formatting by calling String.format. This can be considered as a fix to [#7] too.

formatDateTime(fmt)

Calls SimpleDateFormat because currently Starscript date and time don't allow the user to choose a custom format.

@RealMuffinTime
Copy link

Hi, would you like to add a timezone variable to formatDateTime(fmt)?

Value v = ss.pop();
Object o = null;
switch (v.type) {
case Null: o = Value.null_(); break;
Copy link
Contributor

Choose a reason for hiding this comment

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

this is wrong, should just be o = null

Copy link
Author

Choose a reason for hiding this comment

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

this is wrong, should just be o = null

iirc I used this because I saw Value.null_() being used somewhere else. Anyways do you want me to edit and fix this, if you are going to accept this pr?

Choose a reason for hiding this comment

The 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. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

this is wrong, should just be o = null

iirc I used this because I saw Value.null_() being used somewhere else. Anyways do you want me to edit and fix this, if you are going to accept this pr?

Value.null_() is the starscript null type, but in this case you’re feeding it into the java String.format() method, which takes in the java null

Copy link
Author

Choose a reason for hiding this comment

The 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.

@Xenapte
Copy link
Author

Xenapte commented May 25, 2024

Hi, would you like to add a timezone variable to formatDateTime(fmt)?

Java SimpleDateFormat already supports displaying of the timezone. Do you mean adding the option to switch to another timezone?

@RealMuffinTime
Copy link

Yeah, I mean the option to switch the timezone.

@Xenapte
Copy link
Author

Xenapte commented Jun 28, 2024

Yeah, I mean the option to switch the timezone.

I added that as an optional argument to formatDateTime. Don't know if this PR is getting merged though.


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

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

@Xenapte
Copy link
Author

Xenapte commented Jun 30, 2024

Fixed. Any more suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants