ustrtok_r(3alleg4)

πŸ’– ustrtok_r β€” the Allegro manual, but make it cute ✨

okay bestie, welcome to ustrtok_r(3alleg4), aka the Allegro game programming library page for the reentrant token-grabber. she's giving "I split strings but I don't forget where I was" and honestly? iconic. πŸŽ€


✨ NAME ✨

ustrtok_r β€” a reentrant function that pulls tokens out of a string. part of the Allegro game programming library. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro.h>

char *ustrtok_r(char *s, const char *set, char **last);

🌸 DESCRIPTION 🌸

so this is the reentrant version of ustrtok. the glow-up is the last parameter: she's the one holding the receipts on where the parsing is up to. πŸ“

here's the thing though β€” last has to be a pointer to a char * variable that you allocate yourself, and it has to stay the same variable the whole time you're parsing the same string. no swapping her out mid-parse, bestie, she's the memory of the operation. 🧠✨

example time, look at her go:

char *word, *last;
char string[]="some-words with dashes";
char *temp = ustrdup(string);
word = ustrtok_r(string, " -", &last);
while (word) {
   allegro_message("Found `%s'\n", word);
   word = ustrtok_r(NULL, " -", &last);
}
free(temp);

the way she just keeps passing NULL in and letting last do the heavy lifting… that's the whole vibe. 🫢


πŸ‘‘ RETURN VALUE πŸ‘‘

she returns a pointer to the token, or NULL if there are no more tokens left to find. πŸ“

and here's a lil gift: once NULL comes back, you can go ahead and free the memory that last points to. clean up after yourself, main character. πŸ’…βœ¨


πŸ”₯ SEE ALSO πŸ”₯


okay that's the whole page, bestie, no cap. this was Allegro version 4.4.3, and the manual name is ustrtok_r(3alleg4) in case you need to cite her. mwah, go tokenize something cute πŸ’œπŸŒˆβœ¨