From 43af8f7b3b0ec43723a256ec04de3a571ebf0117 Mon Sep 17 00:00:00 2001 From: Oneric Date: Wed, 16 Dec 2020 22:12:14 +0100 Subject: Add strdup fallback And move fallback declarations to ass_compat.h As ass_compat.h is already included in every source file we no longer need to include _both_ string.h and ass_utils.h to use str(n)dup. Definitions are still in ass_utils.c since a separate source file just for two functions seemed overkill. --- libass/ass_compat.h | 11 +++++++++++ libass/ass_utils.c | 14 +++++++++++++- libass/ass_utils.h | 5 ----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/libass/ass_compat.h b/libass/ass_compat.h index dc3395e..ae16bb7 100644 --- a/libass/ass_compat.h +++ b/libass/ass_compat.h @@ -26,4 +26,15 @@ #define inline __inline #endif +#ifndef HAVE_STRDUP +char *ass_strdup_fallback(const char *s); // definition in ass_utils.c +#define strdup ass_strdup_fallback +#endif + +#ifndef HAVE_STRNDUP +#include +char *ass_strndup_fallback(const char *s, size_t n); // definition in ass_utils.c +#define strndup ass_strndup_fallback +#endif + #endif /* LIBASS_COMPAT_H */ diff --git a/libass/ass_utils.c b/libass/ass_utils.c index 7fba778..65b6f74 100644 --- a/libass/ass_utils.c +++ b/libass/ass_utils.c @@ -66,8 +66,20 @@ int has_avx2(void) #endif // ASM +// Fallbacks +#ifndef HAVE_STRDUP +char *ass_strdup_fallback(const char *str) +{ + size_t len = strlen(str) + 1; + char *new_str = malloc(len); + if (new_str) + memcpy(new_str, str, len); + return new_str; +} +#endif + #ifndef HAVE_STRNDUP -char *ass_strndup(const char *s, size_t n) +char *ass_strndup_fallback(const char *s, size_t n) { char *end = memchr(s, 0, n); size_t len = end ? end - s : n; diff --git a/libass/ass_utils.h b/libass/ass_utils.h index 0379df6..567f5b5 100644 --- a/libass/ass_utils.h +++ b/libass/ass_utils.h @@ -72,11 +72,6 @@ static inline bool ass_string_equal(ASS_StringView str1, ASS_StringView str2) return str1.len == str2.len && !memcmp(str1.str, str2.str, str1.len); } -#ifndef HAVE_STRNDUP -char *ass_strndup(const char *s, size_t n); -#define strndup ass_strndup -#endif - void *ass_aligned_alloc(size_t alignment, size_t size, bool zero); void ass_aligned_free(void *ptr); -- cgit v1.2.3