cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=e2634eec6704170541e18df981188859c7f5794d

commit e2634eec6704170541e18df981188859c7f5794d
Author: Vincent Torri <[email protected]>
Date:   Tue Apr 30 15:37:54 2019 +0000

    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 <[email protected]>
    Differential Revision: https://phab.enlightenment.org/D8790
---
 src/lib/eina/eina_inline_str.x | 32 ++++++++++++++++++++++++++++++++
 src/lib/eina/eina_str.h        | 10 ++++++++++
 2 files changed, 42 insertions(+)

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
@@ -81,6 +81,38 @@ eina_strdup(const char *str)
    return str ? strdup(str) : NULL;
 }
 
+/**
+ * @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
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"
 
 /**

-- 


Reply via email to