mirror of https://github.com/libsdl-org/SDL
Added SDL_strnlen() and SDL_wcsnlen()
This commit is contained in:
parent
4d63c919f9
commit
0a4e6f6d29
|
@ -1106,7 +1106,7 @@ if(SDL_LIBC)
|
|||
set(${_HAVE_H} 1)
|
||||
endforeach()
|
||||
set(HAVE_SIGNAL_H 1)
|
||||
foreach(_FN abs acos acosf asin asinf atan atan2 atan2f atanf atof atoi bsearch calloc ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf free itoa log log10 log10f logf lround lroundf _ltoa malloc memcmp memcpy memmove memset modf modff pow powf qsort realloc round roundf scalbn scalbnf sin sinf sqrt sqrtf sscanf strchr strcmp _stricmp strlen _strlwr strncmp _strnicmp strrchr _strrev strstr strtod strtol strtoll strtoul _strupr tan tanf trunc truncf _ultoa wcscmp _wcsdup wcsdup _wcsicmp wcslen wcsncmp _wcsnicmp wcsstr wcstol)
|
||||
foreach(_FN abs acos acosf asin asinf atan atan2 atan2f atanf atof atoi bsearch calloc ceil ceilf copysign copysignf cos cosf exp expf fabs fabsf floor floorf fmod fmodf free itoa log log10 log10f logf lround lroundf _ltoa malloc memcmp memcpy memmove memset modf modff pow powf qsort realloc round roundf scalbn scalbnf sin sinf sqrt sqrtf sscanf strchr strcmp _stricmp strlen strnlen _strlwr strncmp _strnicmp strrchr _strrev strstr strtod strtol strtoll strtoul _strupr tan tanf trunc truncf _ultoa wcscmp _wcsdup wcsdup _wcsicmp wcslen wcsnlen wcsncmp _wcsnicmp wcsstr wcstol)
|
||||
string(TOUPPER ${_FN} _UPPER)
|
||||
set(HAVE_${_UPPER} 1)
|
||||
endforeach()
|
||||
|
@ -1150,7 +1150,7 @@ if(SDL_LIBC)
|
|||
check_c_source_compiles("#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
int main(void) { return 0; }" HAVE_MPROTECT)
|
||||
foreach(_FN abs atof atoi bcopy bsearch calloc _Exit fopen64 free fseeko fseeko64 getenv _i64toa index itoa _ltoa malloc memcmp memcpy memmove memset putenv qsort realloc rindex setenv sscanf strcasecmp strcasestr strchr strcmp _stricmp strlcat strlcpy strlen _strlwr strncasecmp strncmp _strnicmp strrchr _strrev strstr strtod strtok_r strtol strtoll strtoul strtoull _strupr _ui64toa _uitoa _ultoa unsetenv vsnprintf vsscanf wcscasecmp wcscmp _wcsdup wcsdup _wcsicmp wcslcat wcslcpy wcslen wcsncasecmp wcsncmp _wcsnicmp wcsstr wcstol)
|
||||
foreach(_FN abs atof atoi bcopy bsearch calloc _Exit fopen64 free fseeko fseeko64 getenv _i64toa index itoa _ltoa malloc memcmp memcpy memmove memset putenv qsort realloc rindex setenv sscanf strcasecmp strcasestr strchr strcmp _stricmp strlcat strlcpy strlen strnlen _strlwr strncasecmp strncmp _strnicmp strrchr _strrev strstr strtod strtok_r strtol strtoll strtoul strtoull _strupr _ui64toa _uitoa _ultoa unsetenv vsnprintf vsscanf wcscasecmp wcscmp _wcsdup wcsdup _wcsicmp wcslcat wcslcpy wcslen wcsnlen wcsncasecmp wcsncmp _wcsnicmp wcsstr wcstol)
|
||||
string(TOUPPER ${_FN} _UPPER)
|
||||
set(LIBC_HAS_VAR "LIBC_HAS_${_UPPER}")
|
||||
check_symbol_exists("${_FN}" "${STDC_HEADER_NAMES}" ${LIBC_HAS_VAR})
|
||||
|
|
|
@ -533,6 +533,7 @@ extern DECLSPEC void *SDLCALL SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN
|
|||
extern DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len);
|
||||
|
||||
extern DECLSPEC size_t SDLCALL SDL_wcslen(const wchar_t *wstr);
|
||||
extern DECLSPEC size_t SDLCALL SDL_wcsnlen(const wchar_t *wstr, size_t maxlen);
|
||||
extern DECLSPEC size_t SDLCALL SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
|
||||
extern DECLSPEC size_t SDLCALL SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen);
|
||||
extern DECLSPEC wchar_t *SDLCALL SDL_wcsdup(const wchar_t *wstr);
|
||||
|
@ -545,6 +546,7 @@ extern DECLSPEC int SDLCALL SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *
|
|||
extern DECLSPEC long SDLCALL SDL_wcstol(const wchar_t *str, wchar_t **endp, int base);
|
||||
|
||||
extern DECLSPEC size_t SDLCALL SDL_strlen(const char *str);
|
||||
extern DECLSPEC size_t SDLCALL SDL_strnlen(const char *str, size_t maxlen);
|
||||
extern DECLSPEC size_t SDLCALL SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
|
||||
extern DECLSPEC size_t SDLCALL SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes);
|
||||
extern DECLSPEC size_t SDLCALL SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen);
|
||||
|
|
|
@ -91,6 +91,7 @@
|
|||
#cmakedefine HAVE_MEMMOVE 1
|
||||
#cmakedefine HAVE_MEMCMP 1
|
||||
#cmakedefine HAVE_WCSLEN 1
|
||||
#cmakedefine HAVE_WCSNLEN 1
|
||||
#cmakedefine HAVE_WCSLCPY 1
|
||||
#cmakedefine HAVE_WCSLCAT 1
|
||||
#cmakedefine HAVE__WCSDUP 1
|
||||
|
@ -104,6 +105,7 @@
|
|||
#cmakedefine HAVE__WCSNICMP 1
|
||||
#cmakedefine HAVE_WCSTOL 1
|
||||
#cmakedefine HAVE_STRLEN 1
|
||||
#cmakedefine HAVE_STRNLEN 1
|
||||
#cmakedefine HAVE_STRLCPY 1
|
||||
#cmakedefine HAVE_STRLCAT 1
|
||||
#cmakedefine HAVE__STRREV 1
|
||||
|
|
|
@ -875,6 +875,8 @@ SDL3_0.0.0 {
|
|||
SDL_GetGamepadStringForType;
|
||||
SDL_GetRealGamepadInstanceType;
|
||||
SDL_GetRealGamepadType;
|
||||
SDL_wcsnlen;
|
||||
SDL_strnlen;
|
||||
# extra symbols go here (don't modify this line)
|
||||
local: *;
|
||||
};
|
||||
|
|
|
@ -901,3 +901,5 @@
|
|||
#define SDL_GetGamepadStringForType SDL_GetGamepadStringForType_REAL
|
||||
#define SDL_GetRealGamepadInstanceType SDL_GetRealGamepadInstanceType_REAL
|
||||
#define SDL_GetRealGamepadType SDL_GetRealGamepadType_REAL
|
||||
#define SDL_wcsnlen SDL_wcsnlen_REAL
|
||||
#define SDL_strnlen SDL_strnlen_REAL
|
||||
|
|
|
@ -946,3 +946,5 @@ SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadTypeFromString,(const char *a),(a)
|
|||
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadStringForType,(SDL_GamepadType a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetRealGamepadInstanceType,(SDL_JoystickID a),(a),return)
|
||||
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetRealGamepadType,(SDL_Gamepad *a),(a),return)
|
||||
SDL_DYNAPI_PROC(size_t,SDL_wcsnlen,(const wchar_t *a, size_t b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(size_t,SDL_strnlen,(const char *a, size_t b),(a,b),return)
|
||||
|
|
|
@ -390,6 +390,19 @@ size_t SDL_strlen(const char *string)
|
|||
#endif /* HAVE_STRLEN */
|
||||
}
|
||||
|
||||
size_t SDL_strnlen(const char *string, size_t maxlen)
|
||||
{
|
||||
#ifdef HAVE_STRNLEN
|
||||
return strnlen(string, maxlen);
|
||||
#else
|
||||
size_t len = 0;
|
||||
while (len < maxlen && *string++) {
|
||||
++len;
|
||||
}
|
||||
return len;
|
||||
#endif /* HAVE_STRNLEN */
|
||||
}
|
||||
|
||||
size_t SDL_wcslen(const wchar_t *string)
|
||||
{
|
||||
#ifdef HAVE_WCSLEN
|
||||
|
@ -403,6 +416,19 @@ size_t SDL_wcslen(const wchar_t *string)
|
|||
#endif /* HAVE_WCSLEN */
|
||||
}
|
||||
|
||||
size_t SDL_wcsnlen(const wchar_t *string, size_t maxlen)
|
||||
{
|
||||
#ifdef HAVE_WCSNLEN
|
||||
return wcsnlen(string, maxlen);
|
||||
#else
|
||||
size_t len = 0;
|
||||
while (len < maxlen && *string++) {
|
||||
++len;
|
||||
}
|
||||
return len;
|
||||
#endif /* HAVE_WCSNLEN */
|
||||
}
|
||||
|
||||
size_t SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
|
||||
{
|
||||
#ifdef HAVE_WCSLCPY
|
||||
|
@ -702,11 +728,11 @@ char *SDL_strdup(const char *string)
|
|||
|
||||
char *SDL_strndup(const char *string, size_t maxlen)
|
||||
{
|
||||
size_t len = SDL_min(SDL_strlen(string), maxlen) + 1;
|
||||
char *newstr = (char *)SDL_malloc(len);
|
||||
size_t len = SDL_strnlen(string, maxlen);
|
||||
char *newstr = (char *)SDL_malloc(len + 1);
|
||||
if (newstr) {
|
||||
SDL_memcpy(newstr, string, len);
|
||||
newstr[len - 1] = '\0';
|
||||
newstr[len] = '\0';
|
||||
}
|
||||
return newstr;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,34 @@
|
|||
|
||||
/* Test case functions */
|
||||
|
||||
/**
|
||||
* \brief Call to SDL_strnlen
|
||||
*/
|
||||
#undef SDL_strnlen
|
||||
static int stdlib_strnlen(void *arg)
|
||||
{
|
||||
size_t result;
|
||||
char *text_result;
|
||||
const char *text = "food";
|
||||
const char *expected;
|
||||
|
||||
result = SDL_strnlen(text, 6);
|
||||
SDLTest_AssertPass("Call to SDL_strndup(\"food\", 6)");
|
||||
SDLTest_AssertCheck(result == 4, "Check result value, expected: 4, got: %d", (int)result);
|
||||
|
||||
result = SDL_strnlen(text, 3);
|
||||
SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
|
||||
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", (int)result);
|
||||
|
||||
text_result = SDL_strndup(text, 3);
|
||||
expected = "foo";
|
||||
SDLTest_AssertPass("Call to SDL_strndup(\"food\", 3)");
|
||||
SDLTest_AssertCheck(SDL_strcmp(text_result, expected) == 0, "Check text, expected: %s, got: %s", expected, text_result);
|
||||
SDL_free(text_result);
|
||||
|
||||
return TEST_COMPLETED;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Call to SDL_strlcpy
|
||||
*/
|
||||
|
@ -814,26 +842,30 @@ stdlib_overflow(void *arg)
|
|||
|
||||
/* Standard C routine test cases */
|
||||
static const SDLTest_TestCaseReference stdlibTest1 = {
|
||||
stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
|
||||
stdlib_strnlen, "stdlib_strnlen", "Call to SDL_strnlen", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest2 = {
|
||||
stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
|
||||
stdlib_strlcpy, "stdlib_strlcpy", "Call to SDL_strlcpy", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest3 = {
|
||||
stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED
|
||||
stdlib_snprintf, "stdlib_snprintf", "Call to SDL_snprintf", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest4 = {
|
||||
stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
|
||||
stdlib_swprintf, "stdlib_swprintf", "Call to SDL_swprintf", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest5 = {
|
||||
stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
|
||||
stdlib_getsetenv, "stdlib_getsetenv", "Call to SDL_getenv and SDL_setenv", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest6 = {
|
||||
stdlib_sscanf, "stdlib_sscanf", "Call to SDL_sscanf", TEST_ENABLED
|
||||
};
|
||||
|
||||
static const SDLTest_TestCaseReference stdlibTest7 = {
|
||||
stdlib_aligned_alloc, "stdlib_aligned_alloc", "Call to SDL_aligned_alloc", TEST_ENABLED
|
||||
};
|
||||
|
||||
|
@ -849,6 +881,7 @@ static const SDLTest_TestCaseReference *stdlibTests[] = {
|
|||
&stdlibTest4,
|
||||
&stdlibTest5,
|
||||
&stdlibTest6,
|
||||
&stdlibTest7,
|
||||
&stdlibTestOverflow,
|
||||
NULL
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue