fixes hard to use documentation in string.h

Sun, 03 Nov 2019 16:22:46 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Nov 2019 16:22:46 +0100
changeset 363
8175ba2b3bcb
parent 362
39cef8f37eb5
child 364
5577d6c27a33

fixes hard to use documentation in string.h

src/string.c file | annotate | diff | comparison | revisions
src/ucx/string.h file | annotate | diff | comparison | revisions
src/ucx/ucx.h file | annotate | diff | comparison | revisions
--- a/src/string.c	Sun Nov 03 15:35:29 2019 +0100
+++ b/src/string.c	Sun Nov 03 16:22:46 2019 +0100
@@ -70,6 +70,8 @@
 
 
 size_t scstrnlen(size_t n, ...) {
+    if (n == 0) return 0;
+    
     va_list ap;
     va_start(ap, n);
     
--- a/src/ucx/string.h	Sun Nov 03 15:35:29 2019 +0100
+++ b/src/ucx/string.h	Sun Nov 03 16:22:46 2019 +0100
@@ -83,6 +83,7 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
+  
 /**
  * The UCX string structure.
  */
@@ -112,7 +113,7 @@
 
 #ifdef __cplusplus
 /**
- * One of two type adjustment functions that return a scstr_t.
+ * One of two type adjustment functions that return an scstr_t.
  * 
  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
  * 
@@ -129,7 +130,7 @@
 }
 
 /**
- * One of two type adjustment functions that return a scstr_t.
+ * One of two type adjustment functions that return an scstr_t.
  * 
  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
  * This variant is used, when the string is already immutable and no operation
@@ -147,13 +148,13 @@
 /**
  * Converts a UCX string to an immutable UCX string (scstr_t).
  * @param str some UCX string
- * @return the an immutable version of the provided string
+ * @return an immutable version of the provided string
  */
 #define SCSTR(s) s2scstr(s)
 #else
 
 /**
- * One of two type adjustment functions that return a scstr_t.
+ * One of two type adjustment functions that return an scstr_t.
  * 
  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
  * This variant is used, when the string is already immutable and no operation
@@ -167,7 +168,7 @@
 scstr_t ucx_sc2sc(scstr_t str);
 
 /**
- * One of two type adjustment functions that return a scstr_t.
+ * One of two type adjustment functions that return an scstr_t.
  * 
  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
  * 
@@ -182,7 +183,7 @@
 /**
  * Converts a UCX string to an immutable UCX string (scstr_t).
  * @param str some UCX string
- * @return the an immutable version of the provided string
+ * @return an immutable version of the provided string
  */
 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
 
@@ -191,7 +192,7 @@
 /**
  * Converts a UCX string to an immutable UCX string (scstr_t).
  * @param str some UCX string
- * @return the an immutable version of the provided string
+ * @return an immutable version of the provided string
  */
 #define SCSTR(str) __builtin_choose_expr( \
         __builtin_types_compatible_p(typeof(str), sstr_t), \
@@ -244,8 +245,8 @@
  * 
  * The length is implicitly inferred by using a call to <code>strlen()</code>.
  *
- * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
- * do want a copy, use sstrdup() on the return value of this function.
+ * <b>Note:</b> the sstr_t will share the specified pointer to the C string.
+ * If you do want a copy, use sstrdup() on the return value of this function.
  * 
  * If you need to wrap a constant string, use scstr().
  * 
@@ -259,8 +260,8 @@
 /**
  * Creates a new sstr_t of the specified length based on a C string.
  *
- * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
- * do want a copy, use sstrdup() on the return value of this function.
+ * <b>Note:</b> the sstr_t will share the specified pointer to the C string.
+ * If you do want a copy, use sstrdup() on the return value of this function.
  * 
  * If you need to wrap a constant string, use scstrn().
  * 
@@ -278,8 +279,8 @@
  * 
  * The length is implicitly inferred by using a call to <code>strlen()</code>.
  *
- * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
- * do want a copy, use scstrdup() on the return value of this function.
+ * <b>Note:</b> the scstr_t will share the specified pointer to the C string.
+ * If you do want a copy, use scstrdup() on the return value of this function.
  * 
  * @param cstring the C string to wrap
  * @return a new scstr_t containing the C string
@@ -292,9 +293,8 @@
 /**
  * Creates a new scstr_t of the specified length based on a constant C string.
  *
- * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
- * do want a copy, use scstrdup() on the return value of this function.
- * 
+ * <b>Note:</b> the scstr_t will share the specified pointer to the C string.
+ * If you do want a copy, use scstrdup() on the return value of this function. * 
  * 
  * @param cstring  the C string to wrap
  * @param length   the length of the string
@@ -305,21 +305,24 @@
 scstr_t scstrn(const char *cstring, size_t length);
 
 /**
- * Returns the cumulated length of all specified strings.
+ * Returns the accumulated length of all specified strings.
  * 
- * <b>Attention:</b> if the count argument does not match the count of the
+ * <b>Attention:</b> if the count argument is larger than the count of the
  * specified strings, the behavior is undefined.
  *
- * @param count    the total number of specified strings (so at least 1)
+ * @param count    the total number of specified strings
  * @param ...      all strings
- * @return the cumulated length of all strings
+ * @return the accumulated length of all strings
  */
 size_t scstrnlen(size_t count, ...);
 
 /**
- * Alias for scstrnlen() which automatically converts the arguments.
+ * Returns the accumulated length of all specified strings.
  * 
- * @param count    the total number of specified strings (so at least 1)
+ * <b>Attention:</b> if the count argument is larger than the count of the
+ * specified strings, the behavior is undefined.
+ * 
+ * @param count    the total number of specified strings
  * @param ...      all strings
  * @return the cumulated length of all strings
  */
@@ -342,7 +345,13 @@
 sstr_t scstrcat(size_t count, scstr_t s1, ...);
 
 /**
- * Alias for scstrcat() which automatically converts the arguments.
+ * Concatenates two or more strings.
+ * 
+ * The resulting string will be allocated by standard <code>malloc()</code>. 
+ * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
+ * 
+ * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
+ * terminated.
  * 
  * @param count   the total number of strings to concatenate
  * @param s1      first string
@@ -354,35 +363,47 @@
 /**
  * Concatenates two or more strings using a UcxAllocator.
  * 
- * See scstrcat() for details.
+ * The resulting string must be freed by the allocators <code>free()</code>
+ * implementation.
+ * 
+ * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
+ * terminated.
  *
- * @param a       the allocator to use
+ * @param alloc   the allocator to use
  * @param count   the total number of strings to concatenate
  * @param s1      first string
  * @param ...     all remaining strings
  * @return the concatenated string
+ * 
+ * @see scstrcat()
  */
-sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
+sstr_t scstrcat_a(UcxAllocator *alloc, size_t count, scstr_t s1, ...);
 
 /**
- * Alias for scstrcat_a() which automatically converts the arguments.
+ * Concatenates two or more strings using a UcxAllocator.
+ * 
+ * The resulting string must be freed by the allocators <code>free()</code>
+ * implementation.
  * 
- * See sstrcat() for details.
+ * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
+ * terminated.
  *
- * @param a       the allocator to use
+ * @param alloc   the allocator to use
  * @param count   the total number of strings to concatenate
  * @param s1      first string
  * @param ...     all remaining strings
  * @return the concatenated string
+ * 
+ * @see sstrcat()
  */
-#define sstrcat_a(a, count, s1, ...) \
-    scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
+#define sstrcat_a(alloc, count, s1, ...) \
+    scstrcat_a(alloc, count, SCSTR(s1), __VA_ARGS__)
 
 /**
  * Returns a substring starting at the specified location.
  * 
  * <b>Attention:</b> the new string references the same memory area as the
- * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
+ * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
  * Use sstrdup() to get a copy.
  * 
  * @param string input string
@@ -395,10 +416,10 @@
 sstr_t sstrsubs(sstr_t string, size_t start);
 
 /**
- * Returns a substring with a maximum length starting at the specified location.
+ * Returns a substring with the given length starting at the specified location.
  * 
  * <b>Attention:</b> the new string references the same memory area as the
- * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
+ * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
  * Use sstrdup() to get a copy.
  * 
  * @param string input string
@@ -417,7 +438,7 @@
  * location.
  * 
  * <b>Attention:</b> the new string references the same memory area as the
- * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
+* input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
  * Use scstrdup() to get a copy.
  * 
  * @param string input string
@@ -434,7 +455,7 @@
  * at the specified location.
  * 
  * <b>Attention:</b> the new string references the same memory area as the
- * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
+ * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
  * Use scstrdup() to get a copy.
  * 
  * @param string input string
@@ -522,7 +543,13 @@
 sstr_t scstrsstr(sstr_t string, scstr_t match);
 
 /**
- * Alias for scstrsstr() which automatically converts the match string.
+ * Returns a substring starting at the location of the first occurrence of the
+ * specified string.
+ * 
+ * If the string does not contain the other string, an empty string is returned.
+ * 
+ * If <code>match</code> is an empty string, the complete <code>string</code> is
+ * returned.
  * 
  * @param string the string to be scanned
  * @param match  string containing the sequence of characters to match
@@ -550,7 +577,13 @@
 scstr_t scstrscstr(scstr_t string, scstr_t match);
 
 /**
- * Alias for scstrscstr() which automatically converts the match string.
+ * Returns an immutable substring starting at the location of the
+ * first occurrence of the specified immutable string.
+ * 
+ * If the string does not contain the other string, an empty string is returned.
+ * 
+ * If <code>match</code> is an empty string, the complete <code>string</code> is
+ * returned.
  * 
  * @param string the string to be scanned
  * @param match  string containing the sequence of characters to match
@@ -595,6 +628,55 @@
  * delimiter.
  * 
  * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
+ * items must be manually passed to <code>free()</code>. Use scstrsplit_a() with
+ * an allocator to managed memory, to avoid this.
+ *
+ * @param string the string to split
+ * @param delim  the delimiter string
+ * @param count  IN: the maximum size of the resulting array (0 = no limit),
+ *               OUT: the actual size of the array
+ * @return a sstr_t array containing the split strings or
+ * <code>NULL</code> on error
+ * 
+ * @see scstrsplit_a()
+ */
+sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
+
+/**
+ * Splits a string into parts by using a delimiter string.
+ * 
+ * This function will return <code>NULL</code>, if one of the following happens:
+ * <ul>
+ *   <li>the string length is zero</li>
+ *   <li>the delimeter length is zero</li>
+ *   <li>the string equals the delimeter</li>
+ *   <li>memory allocation fails</li>
+ * </ul>
+ * 
+ * The integer referenced by <code>count</code> is used as input and determines
+ * the maximum size of the resulting array, i.e. the maximum count of splits to
+ * perform + 1.
+ * 
+ * The integer referenced by <code>count</code> is also used as output and is
+ * set to
+ * <ul>
+ *   <li>-2, on memory allocation errors</li>
+ *   <li>-1, if either the string or the delimiter is an empty string</li>
+ *   <li>0, if the string equals the delimiter</li>
+ *   <li>1, if the string does not contain the delimiter</li>
+ *   <li>the count of array items, otherwise</li>
+ * </ul>
+ * 
+ * If the string starts with the delimiter, the first item of the resulting
+ * array will be an empty string.
+ * 
+ * If the string ends with the delimiter and the maximum list size is not
+ * exceeded, the last array item will be an empty string.
+ * In case the list size would be exceeded, the last array item will be the
+ * remaining string after the last split, <i>including</i> the terminating
+ * delimiter.
+ * 
+ * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
  * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
  * an allocator to managed memory, to avoid this.
  *
@@ -605,20 +687,6 @@
  * @return a sstr_t array containing the split strings or
  * <code>NULL</code> on error
  * 
- * @see scstrsplit_a()
- */
-sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
-
-/**
- * Alias for scstrsplit() which automatically converts the arguments.
- * 
- * @param string the string to split
- * @param delim  the delimiter string
- * @param count  IN: the maximum size of the resulting array (0 = no limit),
- *               OUT: the actual size of the array
- * @return a sstr_t array containing the split strings or
- * <code>NULL</code> on error
- * 
  * @see sstrsplit_a()
  */
 #define sstrsplit(string, delim, count) \
@@ -633,9 +701,6 @@
  * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
  * function.
  * 
- * <b>Note:</b> the allocator is not used for memory that is freed within the
- * same call of this function (locally scoped variables).
- * 
  * @param allocator the UcxAllocator used for allocating memory
  * @param string the string to split
  * @param delim  the delimiter string
@@ -650,7 +715,13 @@
         ssize_t *count);
 
 /**
- * Alias for scstrsplit_a() which automatically converts the arguments.
+ * Performing sstrsplit() using a UcxAllocator.
+ * 
+ * <i>Read the description of sstrsplit() for details.</i>
+ * 
+ * The memory for the sstr_t.ptr pointers of the array items and the memory for
+ * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
+ * function.
  * 
  * @param allocator the UcxAllocator used for allocating memory
  * @param string the string to split
@@ -680,7 +751,10 @@
 int scstrcmp(scstr_t s1, scstr_t s2);
 
 /**
- * Alias for scstrcmp() which automatically converts its arguments.
+ * Compares two UCX strings with standard <code>memcmp()</code>.
+ * 
+ * At first it compares the sstr_t.length attribute of the two strings. The
+ * <code>memcmp()</code> function is called, if and only if the lengths match.
  * 
  * @param s1 the first string
  * @param s2 the second string
@@ -706,7 +780,11 @@
 int scstrcasecmp(scstr_t s1, scstr_t s2);
 
 /**
- * Alias for scstrcasecmp() which automatically converts the arguments.
+ * Compares two UCX strings ignoring the case.
+ * 
+ * At first it compares the sstr_t.length attribute of the two strings. If and
+ * only if the lengths match, both strings are compared char by char ignoring
+ * the case.
  * 
  * @param s1 the first string
  * @param s2 the second string
@@ -733,7 +811,14 @@
 sstr_t scstrdup(scstr_t string);
 
 /**
- * Alias for scstrdup() which automatically converts the argument.
+ * Creates a duplicate of the specified string.
+ * 
+ * The new sstr_t will contain a copy allocated by standard
+ * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
+ * <code>free()</code>.
+ * 
+ * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
+ * terminated, regardless of the argument.
  * 
  * @param string the string to duplicate
  * @return a duplicate of the string
@@ -760,7 +845,15 @@
 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
 
 /**
- * Alias for scstrdup_a() which automatically converts the argument.
+ * Creates a duplicate of the specified string using a UcxAllocator.
+ * 
+ * The new sstr_t will contain a copy allocated by the allocators
+ * UcxAllocator.malloc() function. So it is implementation depended, whether the
+ * returned sstr_t.ptr pointer must be passed to the allocators
+ * UcxAllocator.free() function manually.
+ * 
+ * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
+ * terminated, regardless of the argument.
  * 
  * @param allocator a valid instance of a UcxAllocator
  * @param string the string to duplicate
@@ -810,6 +903,7 @@
 
 /**
  * Checks, if a string has a specific prefix.
+ * 
  * @param string the string to check
  * @param prefix the prefix the string should have
  * @return 1, if and only if the string has the specified prefix, 0 otherwise
@@ -817,7 +911,7 @@
 int scstrprefix(scstr_t string, scstr_t prefix);
 
 /**
- * Alias for scstrprefix() which automatically converts the arguments.
+ * Checks, if a string has a specific prefix.
  * 
  * @param string the string to check
  * @param prefix the prefix the string should have
@@ -827,6 +921,7 @@
 
 /**
  * Checks, if a string has a specific suffix.
+ * 
  * @param string the string to check
  * @param suffix the suffix the string should have
  * @return 1, if and only if the string has the specified suffix, 0 otherwise
@@ -834,7 +929,7 @@
 int scstrsuffix(scstr_t string, scstr_t suffix);
 
 /**
- * Alias for scstrsuffix() which automatically converts the arguments.
+ * Checks, if a string has a specific suffix.
  *
  * @param string the string to check
  * @param suffix the suffix the string should have
@@ -845,8 +940,8 @@
 /**
  * Returns a lower case version of a string.
  * 
- * This function creates a duplicate of the input string, first. See the
- * documentation of scstrdup() for the implications.
+ * This function creates a duplicate of the input string, first
+ * (see scstrdup()).
  * 
  * @param string the input string
  * @return the resulting lower case string
@@ -855,7 +950,10 @@
 sstr_t scstrlower(scstr_t string);
 
 /**
- * Alias for scstrlower() which automatically converts the argument.
+ * Returns a lower case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first
+ * (see sstrdup()).
  * 
  * @param string the input string
  * @return the resulting lower case string
@@ -865,8 +963,8 @@
 /**
  * Returns a lower case version of a string.
  * 
- * This function creates a duplicate of the input string, first. See the
- * documentation of scstrdup_a() for the implications.
+  * This function creates a duplicate of the input string, first
+ * (see scstrdup_a()).
  * 
  * @param allocator the allocator used for duplicating the string
  * @param string the input string
@@ -877,7 +975,10 @@
 
 
 /**
- * Alias for scstrlower_a() which automatically converts the argument.
+ * Returns a lower case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first
+ * (see sstrdup_a()).
  * 
  * @param allocator the allocator used for duplicating the string
  * @param string the input string
@@ -888,8 +989,8 @@
 /**
  * Returns a upper case version of a string.
  * 
- * This function creates a duplicate of the input string, first. See the
- * documentation of scstrdup() for the implications.
+ * This function creates a duplicate of the input string, first
+ * (see scstrdup()).
  * 
  * @param string the input string
  * @return the resulting upper case string
@@ -898,7 +999,10 @@
 sstr_t scstrupper(scstr_t string);
 
 /**
- * Alias for scstrupper() which automatically converts the argument.
+ * Returns a upper case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first
+ * (see sstrdup()).
  * 
  * @param string the input string
  * @return the resulting upper case string
@@ -908,8 +1012,8 @@
 /**
  * Returns a upper case version of a string.
  * 
- * This function creates a duplicate of the input string, first. See the
- * documentation of scstrdup_a() for the implications.
+ * This function creates a duplicate of the input string, first
+ * (see scstrdup_a()).
  * 
  * @param allocator the allocator used for duplicating the string
  * @param string the input string
@@ -919,7 +1023,10 @@
 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
 
 /**
- * Alias for scstrupper_a() which automatically converts the argument.
+ * Returns a upper case version of a string.
+ * 
+ * This function creates a duplicate of the input string, first
+ * (see sstrdup_a()).
  * 
  * @param allocator the allocator used for duplicating the string
  * @param string the input string
--- a/src/ucx/ucx.h	Sun Nov 03 15:35:29 2019 +0100
+++ b/src/ucx/ucx.h	Sun Nov 03 16:22:46 2019 +0100
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
+ * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:

mercurial