SDL_strncmp(3)

✨ SDL_strncmp ✨

okay bestie, welcome to the SDL3 FUNCTIONS corner of the manual πŸŽ€ β€” this one is SDL_strncmp, and she is literally the "compare two UTF-8 strings, but like, only up to a certain number of bytes" girlie. no cap. πŸ’…


🌸 NAME 🌸

SDL_strncmp β€” compare two UTF-8 strings up to a number of bytes. πŸ¦‹


✨ SYNOPSIS ✨

#include <SDL3/SDL_stdinc.h>

int SDL_strncmp(const char *str1, const char *str2, size_t maxlen);

πŸ’– DESCRIPTION πŸ’–

so here's the tea πŸ“: because UTF-8 is built the way it is, this function just works on Unicode strings β€” like, it's literally comparing bytes until it bumps into a null-terminating character. that's the whole vibe. and because UTF-8 is that girl, you can pair this with SDL_qsort() to sort your strings into (roughly) alphabetical order. iconic behavior, honestly. πŸ‘‘

BUT β€” and this is the important part, bestie, don't skip it πŸ™ˆ β€” even though this function is meant for UTF-8, what it's actually doing is a bytewise comparison, and maxlen is a _byte_ limit, not a character limit! so if that limit lands right in the middle of a multi-byte UTF-8 sequence, it will only compare a portion of the final character. like it just stops mid-emoji. 😭 lowkey cursed but that's the deal.

so what does maxlen actually mean? it's the maximum number of bytes to compare. if the strings match all the way up to that many bytes β€” or if both of them hit a null-terminator before that many bytes β€” then they get considered equal. periodt. ⭐


🧚 FUNCTION PARAMETERS 🧚

  • str1 β€” the first string to compare. NULL is not permitted! 🚫
  • str2 β€” the second string to compare. NULL is not permitted! 🚫
  • maxlen β€” the maximum number of _bytes_ to compare. (bytes, not characters, iykyk πŸ’€)

🌈 RETURN VALUE 🌈

  • less than zero if str1 is "less than" str2 πŸ’€
  • greater than zero if str1 is "greater than" str2 πŸ”₯
  • zero if the strings match exactly ✨ (slay)

πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this function from any thread. 🫢 no drama, no notes.


⭐ AVAILABILITY ⭐

this function has been available since SDL 3.2.0. she's not new new, but she's not ancient either. πŸŽ€


and that's the whole page, bestie!! 🌸 this was SDL_strncmp(3), served to you straight from Simple Directmedia Layer, version SDL 3.4.18, dated SDL_strncmp(3). go forth and compare bytes responsibly πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from