Adventure Game Studio

AGS Development => Engine Development => Topic started by: eri0o on Wed 01/05/2024 18:49:08

Title: [Feature and Questions] String Split and Join
Post by: eri0o on Wed 01/05/2024 18:49:08
First, String Split and Join are possible to implement in AGS Script - below I give String Split implementation but Join is easy to imagine.

Spoiler
int CountToken(this String*, String token)
{
  int count = 0, cur = 0, next = 0;
  String sub = this;

  while(sub.Length > 0)
  {
    if(sub.IndexOf(token)==-1) return count;
    sub = sub.Substring(sub.IndexOf(token)+token.Length, sub.Length);
    count++;
  }
  return count;
}

String[] Split(this String*, String token)
{
  int i = 0, cur = 0, count;
  count = this.CountToken(token);
  if(count<=0)
  {
    String r[] = new String[1];
    r[0] = null;
    return r;
  }

  String r[] = new String[count+2];
  String sub = this;

  while(i < count)
  {
    cur = sub.IndexOf(token);
    if(cur==-1) cur=sub.Length;
    r[i] = sub.Substring(0, cur);
    sub = sub.Substring(sub.IndexOf(token)+token.Length, sub.Length);
    i++;
  }

  r[i] = sub.Substring(0, sub.Length);
  i++;
  r[i] = null;
  return  r;
}
[close]

With that out of the way, I have been imagining having String Split and Join directly in the AGS Engine Script API - in AGS4. In C# .NET, both String.Split (https://learn.microsoft.com/en-us/dotnet/api/system.string.split) and String.Join (https://learn.microsoft.com/en-us/dotnet/api/system.string.join) are there.

But there's a catch. We don't have Length in Dynamically allocated arrays. So how does an API for this would work?

So in my AGS Script implementation of Split the way it works is the last element is a null element. Unfortunately AGS Script already have dictionaries and sets that already return arrays of strings, but they also have the count in themselves, meaning their API do not add the last element as null, so this would not be consistent.

So mostly trying to figure it out if String Split and Join would make sense in the Script String API or it's best to have it as a module, and how to design it.
Title: Re: [Feature and Questions] String Split and Join
Post by: Crimson Wizard on Wed 01/05/2024 18:58:11
Quote from: eri0o on Wed 01/05/2024 18:49:08But there's a catch. We don't have Length in Dynamically allocated arrays. So how does an API for this would work?

We do have array.Length in AGS 4, with the new compiler. I suppose it should be relied upon at this point.

Here's the list of new compiler's capabilities:
https://github.com/adventuregamestudio/ags/wiki/New-compiler%27s-end-user-cheat-sheet
Title: Re: [Feature and Questions] String Split and Join
Post by: eri0o on Wed 01/05/2024 20:09:53
Oh, for some reason I thought it only worked for non-dynamic arrays! That is pretty cool, yeah, we should rely on it. Thanks!
Title: Re: [Feature and Questions] String Split and Join
Post by: Crimson Wizard on Wed 01/05/2024 20:34:15
Quote from: eri0o on Wed 01/05/2024 20:09:53Oh, for some reason I thought it only worked for non-dynamic arrays!

It is opposite: it does not work for non-dynamic arrays.
Title: Re: [Feature and Questions] String Split and Join
Post by: eri0o on Sat 04/05/2024 16:59:40
https://github.com/adventuregamestudio/ags/pull/2401

Hi, I added String Join and Split here if someone wants to check out.

Ah, I am feeling that in the settings the new compiler could just be "the compiler" and the previous compiler could be renamed as the legacy compiler.