ustrtok(3alleg4)
π ustrtok β the token-slicing bestie π
okay so this is ustrtok(3alleg4) from the Allegro manual, and it's the Allegro game programming library's very own string-tokenizer. she slices, she dices, she ate. let's get into it π«Ά
β¨ SYNOPSIS β¨
#include <allegro.h>
char *ustrtok(char *s, const char *set);
πΈ DESCRIPTION πΈ
okay bestie, here's the vibe: ustrtok pulls tokens out of s, and those tokens are delimited by the characters hanging out in set. like, set is the little crew of characters that count as separators. slay.
To start the search, you pass the string you wanna search as s. For all the remaining tokens after that first one, you pass NULL instead. that's the whole ritual, no cap.
β οΈ warning time, and this one is real: since ustrtok alters the string it is parsing, you should always copy the string to a temporary buffer before parsing it. do not skip this. do not be cute about it. π
also β this function is not re-entrant (ie. you cannot parse two strings at the same time). one string at a time, she's monogamous like that. π
Example, because we love a demonstration:
char *word;
char string[]="some-words with dashes";
char *temp = ustrdup(string);
word = ustrtok(temp, " -");
while (word) {
allegro_message("Found `%s'\n", word);
word = ustrtok(NULL, " -");
}
free(temp);
see how she dup'd into temp first? that's the warning being respected. main character behavior. π
β RETURN VALUE β
Returns a pointer to the token, or NULL if no more are found. so when it hands you NULL, that's your sign the tokens are all gone and the loop can clock out. periodt. π
π¦ SEE ALSO π¦
uconvert(3alleg4)ustrchr(3alleg4)ustrrchr(3alleg4)ustrstr(3alleg4)ustrpbrk(3alleg4)ustrtok_r(3alleg4)allegro_message(3alleg4)ustrncpy(3alleg4)exgui(3alleg4)
and that's the page, bestie π this was Allegro, version 4.4.3, and yes the page itself is called ustrtok(3alleg4) β go forth and tokenize responsibly, and remember: copy your buffer first or it's giving undefined chaos. πβ¨πΈ
π¦ where this page comes from
- debian bookworm: allegro4-doc 2:4.4.3.1-3 Β· Allegro-gift-ware
- debian trixie: allegro4-doc 2:4.4.3.1-5 Β· Allegro-gift-ware
- ubuntu jammy: allegro4-doc 2:4.4.3.1-2 Β· Allegro-gift-ware
- ubuntu noble: allegro4-doc 2:4.4.3.1-4.1build3 Β· Allegro-gift-ware
- ubuntu questing: allegro4-doc 2:4.4.3.1-6
- ubuntu resolute: allegro4-doc 2:4.4.3.1-7 Β· Allegro-gift-ware
- ubuntu stonking: allegro4-doc 2:4.4.3.1-9 Β· Allegro-gift-ware
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms