C API: Parse value(s) from string

The SDK provides playdate->system->formatString() for processing values into text:

int playdate->system->formatString(
  char **ret,
  const char *format,

Documentation request: Inside Playdate with C does not describe the significance of the format argument or the return type. I assume these match what sprintf() does (the return value is either the length of the resulting string not including the null terminator, or negative on error), but this really should be detailed in Inside Playdate with C.

Unlike sprintf(), formatString() allocates a new string buffer and provides a pointer to it. This buffer needs to be deleted by the program via playdate->system->realloc().

SDK request: I'd like to propose a companion function:

int playdate->system->parseString(
  const char *src,
  const char *format,

Unsurprisngly, this would work like sscanf(), where the variadic arguments are all pointers to receive the parsed contents. Unlike sscanf(), I would have the return value work as follows: the number of parsed values if successful, or negative on error.

Intuitively, converting text to other types shouldn't be necessary since, well, why are you representing your assets as text? But some things to consider:

  • If the program accepts text input from the user via the on-screen keyboard, there may be situations where the data is literally unavailable in any other format.
  • Some widespread and/or standard asset formats do use text, such as XML or 3D models/OBJ, as this user points out.
  • Lua has built-in support for this via tonumber(), so this would help provide parity for the C environment.
  • Users won't have to dance around that nosys.specs thing that keeps cropping up for this specific use case (example, example).
1 Like