SDL_wcsnlen(3)

✨ SDL_wcsnlen ✨

okay bestie, so this is SDL_wcsnlen from the SDL3 functions lineup, and it is exactly the vibe you think it is: it does the same thing as wcsnlen() except it doesn't need access to a C runtime. that's the whole point. no CRT, no drama, no cap. πŸ’…


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_stdinc.h>

size_t SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);

like. look at her. one function, two args, size_t out. iconic. πŸŽ€


πŸ’– DESCRIPTION πŸ’–

so what does she actually do? she counts up to a max of maxlen wchar_t values in wstr, and she does NOT count the null terminator. that's it. that's the job.

now here's the part where you gotta pay attention, bestie, because this is where people get got:

just like SDL_strnlen only counts bytes and not codepoints in a UTF-8 string, this one counts wchar_t values in a string β€” even if that string's encoding is variable width, like UTF-16. so it's counting wide-char units, not "characters" in the vibes sense. 🫢

also, huge one: wchar_t is a different size on different platforms. it's 4 bytes on Linux, 2 on Windows, etc. so the same string can give you different numbers depending on where you're running. it's giving ✨portable✨ in the way that it's portable but you still have to know this.

and one more time for the people in the back: maxlen is a count of wide characters, not bytes! i cannot stress this enough. it's not bytes. it's not bytes. it's wide characters. 😭


πŸ¦‹ FUNCTION PARAMETERS πŸ¦‹

  • wstr β€” the null-terminated wide string you wanna read. > ⚠️ it must not be NULL. do not do that. do not pass NULL. we don't do that here. πŸ’€
  • maxlen β€” the maximum amount of wide characters to count. wide characters, babes. not bytes. πŸ‘‘

⭐ RETURN VALUE ⭐

she returns the length of wstr β€” in wide characters, null terminator excluded β€” but never more than maxlen. so worst case you get maxlen back and she just stops counting. queen behavior, honestly. πŸŽ€


🌈 THREAD SAFETY 🌈

it is safe to call this function from any thread. go off, concurrent bestie. 🧚


πŸ’œ AVAILABILITY πŸ’œ

this function is available since SDL 3.2.0. if you're on something older... my condolences. πŸ™ˆ


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, iykyk. πŸ”₯


and that's the whole page, bestie! this came to you from Simple Directmedia Layer β€” SDL 3.4.18, SDL_wcsnlen(3) β€” so go forth and count your wide characters responsibly, mwah πŸ’–βœ¨πŸŒΈ

πŸ“¦ where this page comes from