string documentation.
[apps/madmutt.git] / lib-lib / str.h
index 8c793fb..3133585 100644 (file)
  * strings.
  *
  * The strong point that have to be followed are:
- *  - strings are always \c '\\0' terminated, meaning that we don't have
+ *  - strings are always \c \\0 terminated, meaning that we don't have
  *    stupid semantics à la strncpy.
  *  - function try to always work on buffers with its size (including the
- *    ending \c '\\0') to prevent buffer overflows.
+ *    ending \c \\0) to prevent buffer overflows.
  *  - string and buffers sizes are \c ssize_t, negative values are allowed and
  *    supported.
  *  - functions use a à la sprintf semantics (for those that produce strings)
  *    meaning that they all return the len that could have fit in the buffer
  *    if it would have been big enough. We never try to reallocate the
  *    buffers, it's up to the caller if it's needed.
+ *
+ * Many of the function do no difference between \c NULL and \c "" and will
+ * behave the same when you pass either the former or the latter (m_strlen(),
+ * m_strcpy(), ... to cite a few).
  */
 /*@{*/
 
 #define LONG_STRING     1024   /**< \brief Long buffers */
 #define STRING          256    /**< \brief Usual buffers */
 
-#define NONULL(x)       (x ? x : "")  /**< \brief replace \c NULL strings
-                                                  with emtpy strings */
-#define ISSPACE(c)      isspace((unsigned char)c) /**< \brief safe isspace */
+/** \brief replace \c NULL strings with emtpy strings */
+#define NONULL(x)       (x ? x : "")
+/** \brief safe isspace */
+#define ISSPACE(c)      isspace((unsigned char)c)
 
 /** \brief Convert ascii digits into ints.
  *
@@ -66,7 +71,7 @@
  *
  * Translating a digit \c c into its numerical value in base \c x is just doing:
  * \code
- *   return (c & ~127) && __m_strdigits[c] < x ? __m_strdigits[c] : -1;
+ *   return !(c & ~127) && __m_strdigits[c] < x ? __m_strdigits[c] : -1;
  * \endcode
  */
 extern unsigned char const __m_strdigits[128];
@@ -89,19 +94,41 @@ extern char const __m_b36chars_upper[36];
 /* conversions                                                              */
 /****************************************************************************/
 
+/** \brief Converts an hexadecimal digit into an int.
+ * \param[in]  c    the hexadecimal char
+ * \return
+ *   - 0&ndash;15 if c is a valid hexadecimal digit,
+ *   - -1 on error.
+ */
 static inline int hexval(int c) {
     return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1;
 }
 
+/** \brief Converts a base64 digit into an int.
+ * \param[in]  c    the base64 char
+ * \return
+ *   - 0&ndash;15 if c is a valid base64 digit,
+ *   - -1 on error.
+ */
 static inline int base64val(int c) {
     return (c & ~127) ? -1 : __m_b64digits[c];
 }
 
-static inline void m_strtolower(char *p) {
+/** \brief Converts a string to lowercase.
+ * \param[in] p     the string, shall not be \c NULL.
+ * \return a pointer to the terminating \c \\0.
+ */
+__attribute__((nonnull(1)))
+static inline char *m_strtolower(char *p) {
     for (; *p; p++)
         *p = tolower((unsigned char)*p);
+    return p;
 }
 
+/** \brief Converts a lower case ascii char to upper case.
+ * \param[in]  c    the character.
+ * \return the upper case character.
+ */
 static inline int ascii_toupper(int c) {
     if ('a' <= c && c <= 'z')
         return c & ~32;
@@ -109,6 +136,10 @@ static inline int ascii_toupper(int c) {
     return c;
 }
 
+/** \brief Converts a upper case ascii char to lower case.
+ * \param[in]  c    the character.
+ * \return the lower case character.
+ */
 static inline int ascii_tolower(int c) {
     if ('A' <= c && c <= 'Z')
         return c | 32;
@@ -120,35 +151,89 @@ static inline int ascii_tolower(int c) {
 /* length related                                                           */
 /****************************************************************************/
 
+/** \brief Short hand to test if a string is empty or not.
+ * \param[in]  s    the string.
+ * \return \c true iff s is an empty string.
+ */
 static inline int m_strisempty(const char *s) {
     return !s || !*s;
 }
 
+/** \brief \c NULL resistant strlen.
+ *
+ * Unlinke it's libc sibling, m_strlen returns a ssize_t, and supports its
+ * argument beeing NULL.
+ *
+ * \param[in]  s    the string.
+ * \return the string length (or 0 if \c s is \c NULL).
+ */
 static inline ssize_t m_strlen(const char *s) {
     return s ? strlen(s) : 0;
 }
 
+/** \brief \c NULL resistant strnlen.
+ *
+ * Unlinke it's GNU libc sibling, m_strnlen returns a ssize_t, and supports
+ * its argument beeing NULL.
+ *
+ * The m_strnlen() function returns the number of characters in the string
+ * pointed to by \c s, not including the terminating \c \\0 character, but at
+ * most \c n. In doing this, m_strnlen() looks only at the first \c n
+ * characters at \c s and never beyond \c s+n.
+ *
+ * \param[in]  s    the string.
+ * \param[in]  n    the maximum length to return.
+ * \return \c m_strlen(s) if less than \c n, else \c n.
+ */
 static inline ssize_t m_strnlen(const char *s, ssize_t n) {
-    const char *p = memchr(s, '\0', n);
-    return p ? p - s : n;
+    if (s) {
+        const char *p = memchr(s, '\0', n);
+        return p ? p - s : n;
+    }
+    return 0;
 }
 
 /****************************************************************************/
 /* comparisons                                                              */
 /****************************************************************************/
 
+/** \brief \c NULL resistant strcmp.
+ * \param[in]  a     the first string.
+ * \param[in]  b     the second string.
+ * \return <tt>strcmp(a, b)</tt>, and treats \c NULL strings like \c "" ones.
+ */
 static inline int m_strcmp(const char *a, const char *b) {
     return strcmp(NONULL(a), NONULL(b));
 }
 
+/** \brief \c NULL resistant strcasecmp.
+ * \param[in]  a     the first string.
+ * \param[in]  b     the second string.
+ * \return <tt>strcasecmp(a, b)</tt>, and treats \c NULL strings like \c ""
+ * ones.
+ */
 static inline int m_strcasecmp(const char *a, const char *b) {
     return strcasecmp(NONULL(a), NONULL(b));
 }
 
+/** \brief \c NULL resistant strncmp.
+ * \param[in]  a     the first string.
+ * \param[in]  b     the second string.
+ * \param[in]  n     the number of maximum chars to compare.
+ * \return <tt>strncmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
+ * ones.
+ */
 static inline int m_strncmp(const char *a, const char *b, ssize_t n) {
     return strncmp(NONULL(a), NONULL(b), n);
 }
 
+/** \brief \c NULL resistant strncasecmp.
+ * \param[in]  a     the first string.
+ * \param[in]  b     the second string.
+ * \param[in]  n     the number of maximum chars to compare.
+ * \return <tt>strcasecmp(a, b, n)</tt>, and treats \c NULL strings like \c ""
+ * ones.
+ */
 static inline int m_strncasecmp(const char *a, const char *b, ssize_t n) {
     return strncasecmp(NONULL(a), NONULL(b), n);
 }
@@ -160,20 +245,54 @@ int ascii_strncasecmp(const char *a, const char *b, ssize_t n);
 /* making copies                                                            */
 /****************************************************************************/
 
+/** \brief \c NULL resistant strdup.
+ *
+ * the m_strdup() function returns a pointer to a new string, which is a
+ * duplicate of \c s. Memory should be freed using p_delete().
+ *
+ * \warning when s is \c "", it returns NULL !
+ *
+ * \param[in]  s    the string to duplicate.
+ * \return a pointer to the duplicated string.
+ */
 static inline char *m_strdup(const char *s) {
     ssize_t len = m_strlen(s);
     return len ? p_dup(s, len + 1) : NULL;
 }
 
+/** \brief Duplicate substrings.
+ * \deprecated API IS NOT GOOD, I WILL DEPRECATE IT IN A NEAR FUTURE.
+ */
 static inline char *m_substrdup(const char *s, const char *end) {
     return p_dupstr(s, end ? end - s : m_strlen(s));
 }
 
+/** \brief Replace an allocated string with another.
+ *
+ * Replace the string pointed by \c *p with a copy of the string \c s.
+ * \c *p must point to a buffer allocated with p_new() or one of its alias.
+ *
+ * \param[in,out]  p    a pointer on a string (<tt>char **</tt>)
+ * \param[in]      s    the string to copy into p.
+ * \return a pointer on the duplicated string (aka \c *p).
+ */
+__attribute__((nonnull(1)))
 static inline char *m_strreplace(char **p, const char *s) {
     p_delete(p);
     return (*p = m_strdup(s));
 }
 
+/** \brief Puts a char in a string buffer.
+ *
+ * Puts a char at position 0 of a string buffer of size \c n.
+ * Then \c \\0 terminate the buffer.
+ *
+ * \param[in]  dst   pointer to the buffer.
+ * \param[in]  n     size of that buffer (negative values allowed).
+ * \param[in]  c     the character to append.
+ * \return always return 1.
+ */
+__attribute__((nonnull(1)))
 static inline ssize_t m_strputc(char *dst, ssize_t n, int c) {
     if (n > 1) {
         dst[0] = c;
@@ -188,11 +307,33 @@ ssize_t m_strcpy(char *dst, ssize_t n, const char *src)
 ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
     __attribute__((nonnull(1)));
 
+/** \brief safe strcat.
+ *
+ * The m_strcat() function appends the string \c src at the end of the buffer
+ * \c dst if space is available.
+ *
+ * \param[in]  dst   destination buffer.
+ * \param[in]  n     size of the buffer, Negative sizes are allowed.
+ * \param[in]  src   the string to append.
+ * \return <tt>m_strlen(dst) + m_strlen(src)</tt>
+ */
 static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) {
     ssize_t dlen = m_strnlen(dst, n - 1);
     return dlen + m_strcpy(dst + dlen, n - dlen, src);
 }
 
+/** \brief safe strncat.
+ *
+ * The m_strncat() function appends at most \c n chars from the string \c src
+ * at the end of the buffer \c dst if space is available.
+ *
+ * \param[in]  dst   destination buffer.
+ * \param[in]  n     size of the buffer, Negative sizes are allowed.
+ * \param[in]  src   the string to append.
+ * \param[in]  l     maximum number of chars of src to consider.
+ * \return the smallest value between <tt>m_strlen(dst) + m_strlen(src)</tt>
+ *         and <tt>m_strlen(dst) + l</tt>
+ */
 static inline ssize_t
 m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) {
     ssize_t dlen = m_strnlen(dst, n - 1);