Added SDL_strnstr()

This commit is contained in:
Sam Lantinga
2023-12-03 14:42:28 -08:00
parent 7c71e72193
commit ac0751a652
8 changed files with 119 additions and 21 deletions

View File

@@ -960,6 +960,8 @@ SDL3_0.0.0 {
SDL_GetGamepadMappings;
SDL_GetTouchDevices;
SDL_GetTouchDeviceName;
SDL_strnstr;
SDL_wcsnstr;
# extra symbols go here (don't modify this line)
local: *;
};

View File

@@ -985,3 +985,5 @@
#define SDL_GetGamepadMappings SDL_GetGamepadMappings_REAL
#define SDL_GetTouchDevices SDL_GetTouchDevices_REAL
#define SDL_GetTouchDeviceName SDL_GetTouchDeviceName_REAL
#define SDL_strnstr SDL_strnstr_REAL
#define SDL_wcsnstr SDL_wcsnstr_REAL

View File

@@ -1010,3 +1010,5 @@ SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateRendererWithProperties,(SDL_PropertiesID
SDL_DYNAPI_PROC(char**,SDL_GetGamepadMappings,(int *a),(a),return)
SDL_DYNAPI_PROC(SDL_TouchID*,SDL_GetTouchDevices,(int *a),(a),return)
SDL_DYNAPI_PROC(const char*,SDL_GetTouchDeviceName,(SDL_TouchID a),(a),return)
SDL_DYNAPI_PROC(char*,SDL_strnstr,(const char *a, const char *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(wchar_t*,SDL_wcsnstr,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)

View File

@@ -468,19 +468,28 @@ wchar_t *SDL_wcsdup(const wchar_t *string)
return newstr;
}
wchar_t *SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen)
{
size_t length = SDL_wcslen(needle);
if (length == 0) {
return (wchar_t *)haystack;
}
while (maxlen >= length && *haystack) {
if (maxlen >= length && SDL_wcsncmp(haystack, needle, length) == 0) {
return (wchar_t *)haystack;
}
++haystack;
--maxlen;
}
return NULL;
}
wchar_t *SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
{
#ifdef HAVE_WCSSTR
return SDL_const_cast(wchar_t *, wcsstr(haystack, needle));
#else
size_t length = SDL_wcslen(needle);
while (*haystack) {
if (SDL_wcsncmp(haystack, needle, length) == 0) {
return (wchar_t *)haystack;
}
++haystack;
}
return NULL;
return SDL_wcsnstr(haystack, needle, SDL_wcslen(haystack));
#endif /* HAVE_WCSSTR */
}
@@ -821,19 +830,32 @@ char *SDL_strrchr(const char *string, int c)
#endif /* HAVE_STRRCHR */
}
char *SDL_strnstr(const char *haystack, const char *needle, size_t maxlen)
{
#ifdef HAVE_STRNSTR
return SDL_const_cast(char *, strnstr(haystack, needle, maxlen));
#else
size_t length = SDL_strlen(needle);
if (length == 0) {
return (char *)haystack;
}
while (maxlen >= length && *haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *)haystack;
}
++haystack;
--maxlen;
}
return NULL;
#endif /* HAVE_STRSTR */
}
char *SDL_strstr(const char *haystack, const char *needle)
{
#ifdef HAVE_STRSTR
return SDL_const_cast(char *, strstr(haystack, needle));
#else
size_t length = SDL_strlen(needle);
while (*haystack) {
if (SDL_strncmp(haystack, needle, length) == 0) {
return (char *)haystack;
}
++haystack;
}
return NULL;
return SDL_strnstr(haystack, needle, SDL_strlen(haystack));
#endif /* HAVE_STRSTR */
}