adventuregamestudio / ags

AGS editor and engine source code
Other
708 stars 159 forks source link

AGS 4: Script API: add "split options" for String.Split method #2458

Closed ivan-mogilko closed 5 months ago

ivan-mogilko commented 5 months ago

I propose to add optional "split options" as a set of flags for the String.Split methods, equivalent or similar to the C# library function: https://learn.microsoft.com/en-us/dotnet/api/system.stringsplitoptions?view=net-8.0

Something like

enum StringSplitOptions {
    eStrSplit_None = 0,
    eStrSplit_RemoveEmpty = 0x0001,
    eStrSplit_Trim = 0x0002
};

import String[] Split(const string separator, StringSplitOptions options = eStrSplit_None);
AlanDrake commented 5 months ago

I agree, it's a good convenience for several cases.

ericoporto commented 5 months ago

It misses the actual flags, but I believe the gist of the changes would be something like this

https://github.com/ericoporto/ags/commit/de5d29cd1d2f339dcaa8ca75336fe98b933a2e12

Not sure if there is a pretty way to get the trim in a single function.

ericoporto commented 5 months ago

Uhm, I wonder if a string like " , , , ," is passed with both flags, what the result should be, would it be null or does an empty array exists in AGS script?

ivan-mogilko commented 5 months ago

The trim implementation from above commit looks fine, similar to what I've imagined. (I'd only use camelcase function names similar to all names around, according to our code style)

Uhm, I wonder if a string like " , , , ," is passed with both flags, what the result should be, would it be null or does an empty array exists in AGS script?

AGS does not currently allow creating a zero-length dynamic array in script (with the new operator). But if this is a matter of convenience, we may investigate if it's possible to support such array, and return one. In fact, maybe it's already technically possible.

ericoporto commented 5 months ago

I put a PR in #2461, please test it!

ivan-mogilko commented 5 months ago

Resolved by #2461