rgbds icon indicating copy to clipboard operation
rgbds copied to clipboard

Don't terminate RGBDS strings with a NUL

Open ISSOtm opened this issue 5 years ago • 3 comments

The man page mentions that a NUL truncates a string; this is because RGBDS maps transparently to C strings. People may want to use other terminators (hello Pokémon and its $50, though there 0 is a mostly useless char anyways), or just use 0 in the middle of their strings. Thus, and especially as fixed-size buffers are mostly being used anyways, RGBDS should use Pascal strings instead.

ISSOtm avatar Apr 07 '20 17:04 ISSOtm

AFAIK, Pascal strings store their length in a single byte just before the contents. This has the disadvantage of limiting the maximum length of a string. I'd like to suggest Rust-style strings instead, where strings are essentially manipulated as a pair of (pointer to start, length). Apart from removing the string length limit, this also allows to pass substrings around much more efficiently.

meithecatte avatar Apr 07 '20 17:04 meithecatte

Okay, I wasn't intending exactly on using a byte, but the more general idea of storing the length separately. Wasn't aware of Rust's behavior, which is basically what I'm shooting for.

ISSOtm avatar Apr 07 '20 17:04 ISSOtm

If you're using C99, you can just use a flexible array member to store the data, which makes the Pascal-like approach a lot simpler:

struct string {
  unsigned length;
  char data[];
};

aaaaaa123456789 avatar Apr 07 '20 21:04 aaaaaa123456789