SDL_strlcpy(3)

πŸ’– SDL_strlcpy β€” from the SDL3 FUNCTIONS manual, bestie πŸ’–

✨ NAME ✨

SDL_strlcpy β€” copy a string, but like, safely. no cap. 🫢


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_stdinc.h>

size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe. SDL_strlcpy copies up to maxlen - 1 characters from src into dst, and then it appends a null terminator. that's it. that's the whole thing. iconic behaviour honestly. πŸ’…

but listen, the edge cases are where it gets real:

  • if maxlen is 0, no characters are copied and no null terminator is written. nothing happens. it's giving "i'm not doing that" energy. πŸ’€
  • if you're copying a UTF-8 string and you need to make sure multi-byte sequences don't get chopped mid-character (which would be so embarrassing for everyone involved 😭), consider using SDL_utf8strlcpy() instead. she's the specialist for that job. ✨

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • dst β€” the destination buffer. she must not be NULL, and she must not overlap with src. two queens can't share the same spotlight, ok? πŸ‘‘
  • src β€” the null-terminated string you're copying from. also must not be NULL, and also must not overlap with dst. the boundaries are boundaries. 🚧
  • maxlen β€” the length (in characters) of the destination buffer. basically: how much room does dst have? that's your budget, bestie, spend it wisely. πŸ’Έ

⭐ RETURN VALUE ⭐

it returns the length (in characters, excluding the null terminator) of src. so like, the source length, not the amount copied. important distinction, don't get it twisted. 🧚


🌈 THREAD SAFETY 🌈

it is safe to call this function from any thread. yes ma'am, thread-safe, we love a low-maintenance function. 🫢


πŸ‘‘ AVAILABILITY πŸ‘‘

this function is available since SDL 3.2.0. if you're on something older... bestie. update. 😭


πŸ’œ SEE ALSO πŸ’œ


psa: dst can't be NULL, src can't be NULL, and they can't overlap. that's not a suggestion, that's the rule. πŸ™ˆ

okay that's the whole page, slay. this was SDL_strlcpy(3) from the Simple Directmedia Layer crew, page version SDL 3.4.18 β€” mwah, go write some safe string copies, you absolute main character. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from