summaryrefslogtreecommitdiff
path: root/src/lib/eina
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2019-04-30 15:37:54 +0000
committerCedric BAIL <cedric.bail@free.fr>2019-05-01 17:57:36 -0700
commite2634eec6704170541e18df981188859c7f5794d (patch)
treebb3e54e8718ab013a6583ed99cbfc3d861d1690c /src/lib/eina
parent58f5529f704baf4c14afbb8f8ffef9e6c487d2b7 (diff)
Eina: add eina_strndup() API as inlined function
this add strndup implementation that does not seg fault when string is NULL. This also implements strndup on Windows. Reviewed-by: Cedric BAIL <cedric.bail@free.fr> Differential Revision: https://phab.enlightenment.org/D8790
Diffstat (limited to 'src/lib/eina')
-rw-r--r--src/lib/eina/eina_inline_str.x32
-rw-r--r--src/lib/eina/eina_str.h10
2 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/eina/eina_inline_str.x b/src/lib/eina/eina_inline_str.x
index 3b75591..d61eda5 100644
--- a/src/lib/eina/eina_inline_str.x
+++ b/src/lib/eina/eina_inline_str.x
@@ -82,6 +82,38 @@ eina_strdup(const char *str)
82} 82}
83 83
84/** 84/**
85 * @brief strndup function which takes @c NULL without crashing
86 * @param str The string to copy
87 * @param n The maximum number of char to copy
88 * @return the copied string, must be freed
89 * @note this also implements strndup() on Windows
90 * @since 1.23
91 */
92static inline char *
93eina_strndup(const char *str, size_t n)
94{
95#ifdef _WIN32
96 char *ret;
97 size_t slen;
98
99 if (!str)
100 return NULL;
101
102 slen = strnlen(str, n);
103 ret = (char *)malloc(slen + 1); /* cast for C++ code */
104 if (!ret)
105 return NULL;
106 if (slen > 0)
107 memcpy(ret, str, slen);
108 ret[slen] = '\0';
109
110 return ret;
111#else
112 return str ? strndup(str, n) : NULL;
113#endif
114}
115
116/**
85 * @brief streq function which takes @c NULL without crashing 117 * @brief streq function which takes @c NULL without crashing
86 * @param a string a 118 * @param a string a
87 * @param b string b 119 * @param b string b
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index e8bf685..c7d7e4e 100644
--- a/src/lib/eina/eina_str.h
+++ b/src/lib/eina/eina_str.h
@@ -382,6 +382,16 @@ EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool termi
382 */ 382 */
383EAPI char *eina_strftime(const char *format, const struct tm *tm); 383EAPI char *eina_strftime(const char *format, const struct tm *tm);
384 384
385static inline size_t eina_strlen_bounded(const char *str, size_t maxlen);
386
387static inline size_t eina_str_join(char *dst, size_t size, char sep, const char *a, const char *b);
388
389static inline char *eina_strdup(const char *str);
390
391static inline char *eina_strndup(const char *str, size_t n);
392
393static inline Eina_Bool eina_streq(const char *a, const char *b);
394
385#include "eina_inline_str.x" 395#include "eina_inline_str.x"
386 396
387/** 397/**