[Feature and Questions] String Split and Join

Started by eri0o, Wed 01/05/2024 18:49:08

Previous topic - Next topic

eri0o

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
Code: ags
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 and 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.

Crimson Wizard

#1
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

eri0o

Oh, for some reason I thought it only worked for non-dynamic arrays! That is pretty cool, yeah, we should rely on it. Thanks!

Crimson Wizard

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.

eri0o

#4
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.

SMF spam blocked by CleanTalk