aboutsummaryrefslogtreecommitdiffstats
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
parentefl_ui_spec: we can add efl_ui_pager now (diff)
downloadefl-e2634eec6704170541e18df981188859c7f5794d.tar.gz
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
-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 3b75591e78..d61eda5d7a 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)
}
/**
+ * @brief strndup function which takes @c NULL without crashing
+ * @param str The string to copy
+ * @param n The maximum number of char to copy
+ * @return the copied string, must be freed
+ * @note this also implements strndup() on Windows
+ * @since 1.23
+ */
+static inline char *
+eina_strndup(const char *str, size_t n)
+{
+#ifdef _WIN32
+ char *ret;
+ size_t slen;
+
+ if (!str)
+ return NULL;
+
+ slen = strnlen(str, n);
+ ret = (char *)malloc(slen + 1); /* cast for C++ code */
+ if (!ret)
+ return NULL;
+ if (slen > 0)
+ memcpy(ret, str, slen);
+ ret[slen] = '\0';
+
+ return ret;
+#else
+ return str ? strndup(str, n) : NULL;
+#endif
+}
+
+/**
* @brief streq function which takes @c NULL without crashing
* @param a string a
* @param b string b
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index e8bf685067..c7d7e4ecca 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
*/
EAPI char *eina_strftime(const char *format, const struct tm *tm);
+static inline size_t eina_strlen_bounded(const char *str, size_t maxlen);
+
+static inline size_t eina_str_join(char *dst, size_t size, char sep, const char *a, const char *b);
+
+static inline char *eina_strdup(const char *str);
+
+static inline char *eina_strndup(const char *str, size_t n);
+
+static inline Eina_Bool eina_streq(const char *a, const char *b);
+
#include "eina_inline_str.x"
/**