stdlib: Add SDL_utf8strnlen

This commit is contained in:
Guldoman 2022-05-05 02:23:05 +02:00 committed by Sam Lantinga
parent c7f4103807
commit 74bcc5a0a3
4 changed files with 20 additions and 0 deletions

View file

@ -561,6 +561,7 @@ extern DECLSPEC char *SDLCALL SDL_strrchr(const char *str, int c);
extern DECLSPEC char *SDLCALL SDL_strstr(const char *haystack, const char *needle);
extern DECLSPEC char *SDLCALL SDL_strtokr(char *s1, const char *s2, char **saveptr);
extern DECLSPEC size_t SDLCALL SDL_utf8strlen(const char *str);
extern DECLSPEC size_t SDLCALL SDL_utf8strnlen(const char *str, size_t bytes);
extern DECLSPEC char *SDLCALL SDL_itoa(int value, char *str, int radix);
extern DECLSPEC char *SDLCALL SDL_uitoa(unsigned int value, char *str, int radix);

View file

@ -874,3 +874,4 @@
#define SDL_HasLSX SDL_HasLSX_REAL
#define SDL_HasLASX SDL_HasLASX_REAL
#define SDL_RenderGetD3D12Device SDL_RenderGetD3D12Device_REAL
#define SDL_utf8strnlen SDL_utf8strnlen_REAL

View file

@ -951,3 +951,4 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_HasLASX,(void),(),return)
#ifdef __WIN32__
SDL_DYNAPI_PROC(ID3D12Device*,SDL_RenderGetD3D12Device,(SDL_Renderer *a),(a),return)
#endif
SDL_DYNAPI_PROC(size_t,SDL_utf8strnlen,(const char *a, size_t b),(a,b),return)

View file

@ -675,6 +675,23 @@ SDL_utf8strlen(const char *str)
return retval;
}
size_t
SDL_utf8strnlen(const char *str, size_t bytes)
{
size_t retval = 0;
const char *p = str;
unsigned char ch;
while ((ch = *(p++)) != 0 && bytes-- > 0) {
/* if top two bits are 1 and 0, it's a continuation byte. */
if ((ch & 0xc0) != 0x80) {
retval++;
}
}
return retval;
}
size_t
SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
{