SDL_StepBackUTF8(3)

πŸ¦‹ SDL_StepBackUTF8 β€” bestie's guide to walking a UTF-8 string backwards πŸ’…

ok so real talk, this is a page from the SDL3 functions manual (SDL_StepBackUTF8(3), if we're being formal) and it is genuinely one of the most underrated little gremlins in SDL_stdinc.h. let's get into it.


✨ NAME ✨

SDL_StepBackUTF8 β€” decode a UTF-8 string in reverse, one Unicode codepoint at a time. yes, backwards. yes, on purpose. iconic behaviour honestly 🌈


✨ SYNOPSIS ✨

#include <SDL3/SDL_stdinc.h>

Uint32 SDL_StepBackUTF8(const char *start, const char **pstr);

✨ DESCRIPTION ✨

okay so here's the vibe: this function walks you to the start of the previous Unicode codepoint in the string, moves *pstr to that location, and hands you back that codepoint. it's giving rewind button but for text πŸŽ€

if *pstr is already at the start of the string), it will not advance *pstr at all. no movement, no drama, we stay put. periodt.

generally this function is called in a loop until it returns zero, adjusting its parameter each iteration. that's the whole workflow bestie, you just keep stepping back until it says zero and then you're done 🫢

if an invalid UTF-8 sequence is encountered, this function returns SDL_INVALID_UNICODE_CODEPOINT. not a codepoint, not a vibe, just a flag that something's busted 😭

several things can generate invalid UTF-8 sequences, including:

  • overlong encodings
  • the use of UTF-16 surrogate values
  • truncated data

please refer to RFC3629 at https://www.ietf.org/rfc/rfc3629.txt for details. like, actually refer to it. it's the lore.


✨ FUNCTION PARAMETERS ✨

  • start β€” a pointer to the beginning of the UTF-8 string. this is your anchor, your home base, the place you can never walk past 🏠
  • pstr β€” a pointer to a UTF-8 string pointer to be read and adjusted. yes it's a pointer to a pointer. yes that's the bit that moves. don't fight it πŸ’€

✨ RETURN VALUE ✨

Returns the previous Unicode codepoint in the string. that's it, that's the tea β˜•


✨ THREAD SAFETY ✨

It is safe to call this function from any thread. slay, no locking discourse required πŸ”₯


✨ AVAILABILITY ✨

This function is available since SDL 3.2.0. so if you're on something older... no you're not, upgrade bestie πŸ’–


reminder: loop it until it returns zero, and if you get SDL_INVALID_UNICODE_CODEPOINT back, something in your data is not valid UTF-8. that's a you-and-your-input problem, not a function problem πŸ™ˆ

ok bye ily, this page came from Simple Directmedia Layer docs, SDL 3.4.18 era, dated with the SDL_StepBackUTF8(3) energy. mwah πŸ’œβœ¨

πŸ“¦ where this page comes from