SDL_strtok_r(3)

πŸ’– SDL_strtok_r(3) πŸ’–

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual, where today's main character is SDL_strtok_r. she slices strings. she's literally the strtok_r() you already know and love, except she does exactly the same thing without needing access to a C runtime. no C runtime? no problem. she's that girl. πŸŽ€


✨ SYNOPSIS ✨

#include <SDL3/SDL_stdinc.h>

char * SDL_strtok_r(char *str, const char *delim, char **saveptr);

like. look at her. iconic signature, no notes. πŸ’…


🌸 DESCRIPTION 🌸

so the whole vibe here is: break a string up into a series of tokens. that's the job. that's the assignment. she ate.

to start tokenizing a brand-new string, str should be the non-NULL address of the string you wanna start tokenizing. then, for all your future calls where you want the next token from that same string, you pass a NULL instead. that's it. that's the pattern. iykyk. πŸ¦‹

⚠️ okay this part is important and i need you to actually read it: this function will overwrite pieces of str with null chars to split it into tokens. so she cannot be used with const/read-only strings! not a suggestion, not a preference, she will genuinely wreck them. 😭

and saveptr? she just needs to point to a char * that can be overwritten. SDL uses that lil guy to save tokenizing state between calls. it gets initialized if str is non-NULL, and it gets used to resume tokenizing when str is NULL. she's remembering where you left off so you don't have to. bestie behavior fr fr. 🫢


🌈 FUNCTION PARAMETERS 🌈

  • str β€” the string to tokenize, or NULL to continue tokenizing. (yes, NULL means "keep going", that's the whole trick πŸ’«)
  • delim β€” the delimiter string that separates tokens.
  • saveptr β€” pointer to a char *, used for ongoing state. she lives here between calls. give her a home. 🏠

⭐ RETURN VALUE ⭐

returns a pointer to the next token, or NULL if no tokens remain. that NULL at the end? that's her little goodbye. πŸ₯Ί


πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this function from any thread. periodt. every thread can have her. she's for the girls and the threads. πŸ‘‘


πŸ¦‹ AVAILABILITY πŸ¦‹

this function is available since SDL 3.2.0. so if you're on something older, no she's not there yet, sorry bestie. πŸ™ˆ


one more time for the people in the back: non-NULL str to start, NULL to continue, saveptr carries the state, and NEVER hand her a const string because she WILL write nulls into it. πŸ’–

mwah, that's the whole page. this gem came to you from the lovely folks at Simple Directmedia Layer, and this particular copy is chillin' at version SDL 3.4.18. go tokenize something. slay. βœ¨πŸ’…

πŸ“¦ where this page comes from