removes some unnecessary string functions - fixes #561 default tip

Wed, 15 Jan 2025 19:42:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 15 Jan 2025 19:42:49 +0100
changeset 1127
1fd31909a3f8
parent 1126
20c9212b3a47

removes some unnecessary string functions - fixes #561

CHANGELOG file | annotate | diff | comparison | revisions
src/cx/string.h file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
tests/test_string.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Wed Jan 15 19:32:53 2025 +0100
+++ b/CHANGELOG	Wed Jan 15 19:42:49 2025 +0100
@@ -31,6 +31,7 @@
  * moves cx_compare_func typedef to compare.h
  * moves cx_szmul() to common.h
  * moves stream copy functions to new streams.h
+ * removes several *_m variants of functions in string.h in favor of automatic conversion
  * removes utils.h
  * removes flag_removal function from iterator
  * removes cxMapDetach() and makes cxMapRemoveAndGet() compatible with both map variants
--- a/src/cx/string.h	Wed Jan 15 19:32:53 2025 +0100
+++ b/src/cx/string.h	Wed Jan 15 19:42:49 2025 +0100
@@ -298,7 +298,8 @@
 /**
  * Passes the pointer in this string to @c free().
  *
- * The pointer in the struct is set to @c NULL and the length is set to zero.
+ * The pointer in the struct is set to @c NULL and the length is set to zero
+ * which means that this function protects you against double-free.
  *
  * @note There is no implementation for cxstring, because it is unlikely that
  * you ever have a <code>const char*</code> you are really supposed to free.
@@ -311,7 +312,8 @@
 /**
  * Passes the pointer in this string to the allocators free function.
  *
- * The pointer in the struct is set to @c NULL and the length is set to zero.
+ * The pointer in the struct is set to @c NULL and the length is set to zero
+ * which means that this function protects you against double-free.
  *
  * @note There is no implementation for cxstring, because it is unlikely that
  * you ever have a <code>const char*</code> you are really supposed to free.
@@ -845,41 +847,13 @@
  *
  * @note The returned string is guaranteed to be zero-terminated.
  *
- * @param string (@c cxstring) the string to duplicate
+ * @param string the string to duplicate
  * @return (@c cxmutstr) a duplicate of the string
  * @see cx_strdup_a()
+ * @see cx_strfree()
  */
 #define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
 
-
-/**
- * Creates a duplicate of the specified string.
- *
- * The new string will contain a copy allocated by @p allocator.
- *
- * @note The returned string is guaranteed to be zero-terminated.
- *
- * @param allocator (@c CxAllocator*) the allocator to use
- * @param string (@c cxmutstr) the string to duplicate
- * @return (@c cxmutstr) a duplicate of the string
- * @see cx_strdup_m()
- */
-#define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string))
-
-/**
- * Creates a duplicate of the specified string.
- *
- * The new string will contain a copy allocated by standard
- * @c malloc(). So developers @em must pass the return value to cx_strfree().
- *
- * @note The returned string is guaranteed to be zero-terminated.
- *
- * @param string (@c cxmutstr) the string to duplicate
- * @return (@c cxmutstr) a duplicate of the string
- * @see cx_strdup_ma()
- */
-#define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string))
-
 /**
  * Omits leading and trailing spaces.
  *
@@ -1086,21 +1060,6 @@
 );
 
 /**
-* Creates a string tokenization context for a mutable string.
-*
-* @param str the string to tokenize
-* @param delim the delimiter (must not be empty)
-* @param limit the maximum number of tokens that shall be returned
-* @return a new string tokenization context
-*/
-cx_attr_nodiscard
-CxStrtokCtx cx_strtok_m(
-        cxmutstr str,
-        cxstring delim,
-        size_t limit
-);
-
-/**
  * Returns the next token.
  *
  * The token will point to the source string.
@@ -1122,6 +1081,8 @@
  * Returns the next token of a mutable string.
  *
  * The token will point to the source string.
+ *
+ * @attention
  * If the context was not initialized over a mutable string, modifying
  * the data of the returned token is undefined behavior.
  *
@@ -1545,6 +1506,33 @@
  */
 #define cx_strtod(str, output) cx_strtod_lc(str, output, '.', ",")
 
+/**
+ * Creates a duplicate of the specified string.
+ *
+ * The new string will contain a copy allocated by @p allocator.
+ *
+ * @note The returned string is guaranteed to be zero-terminated.
+ *
+ * @param allocator (@c CxAllocator*) the allocator to use
+ * @param string the string to duplicate
+ * @return (@c cxmutstr) a duplicate of the string
+ * @see cx_strdup()
+ * @see cx_strfree_a()
+ */
+#define cx_strdup_a(allocator, string) \
+    cx_strdup_a((allocator), cx_strcast(string))
+
+/**
+ * Creates a string tokenization context.
+ *
+ * @param str the string to tokenize
+ * @param delim the delimiter string (must not be empty)
+ * @param limit (@c @size_t) the maximum number of tokens that shall be returned
+ * @return (@c CxStrtokCtx) a new string tokenization context
+ */
+#define cx_strtok(str, delim, limit) \
+    cx_strtok(cx_strcast(str), cx_strcast(delim), (limit))
+
 #endif
 
 #ifdef __cplusplus
--- a/src/string.c	Wed Jan 15 19:32:53 2025 +0100
+++ b/src/string.c	Wed Jan 15 19:42:49 2025 +0100
@@ -753,14 +753,6 @@
     return ctx;
 }
 
-CxStrtokCtx cx_strtok_m(
-        cxmutstr str,
-        cxstring delim,
-        size_t limit
-) {
-    return cx_strtok(cx_strcast(str), delim, limit);
-}
-
 bool cx_strtok_next(
         CxStrtokCtx *ctx,
         cxstring *token
--- a/tests/test_string.c	Wed Jan 15 19:32:53 2025 +0100
+++ b/tests/test_string.c	Wed Jan 15 19:42:49 2025 +0100
@@ -779,25 +779,6 @@
     }
 }
 
-CX_TEST(test_strtok_m) {
-    cxmutstr str = cx_strdup(cx_str("a,comma,separated,string"));
-    cxstring delim = CX_STR(",");
-    CX_TEST_DO {
-        CxStrtokCtx ctx = cx_strtok_m(str, delim, 3);
-        CX_TEST_ASSERT(ctx.str.ptr == str.ptr);
-        CX_TEST_ASSERT(ctx.str.length == str.length);
-        CX_TEST_ASSERT(ctx.delim.ptr == delim.ptr);
-        CX_TEST_ASSERT(ctx.delim.length == delim.length);
-        CX_TEST_ASSERT(ctx.limit == 3);
-        CX_TEST_ASSERT(ctx.found == 0);
-        CX_TEST_ASSERT(ctx.pos == 0);
-        CX_TEST_ASSERT(ctx.next_pos == 0);
-        CX_TEST_ASSERT(ctx.delim_more == NULL);
-        CX_TEST_ASSERT(ctx.delim_more_count == 0);
-    }
-    cx_strfree(&str);
-}
-
 CX_TEST(test_strtok_delim) {
     cxstring str = CX_STR("an,arbitrarily|separated;string");
     cxstring delim = CX_STR(",");
@@ -920,7 +901,7 @@
     cxstring delim = CX_STR(",");
     cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")};
     CX_TEST_DO {
-        CxStrtokCtx ctx = cx_strtok_m(str, delim, 10);
+        CxStrtokCtx ctx = cx_strtok(str, delim, 10);
         cx_strtok_delim(&ctx, delim_more, 2);
         bool ret;
         cxmutstr tok;
@@ -1272,7 +1253,6 @@
     cx_test_register(suite, test_strupper);
     cx_test_register(suite, test_strlower);
     cx_test_register(suite, test_strtok);
-    cx_test_register(suite, test_strtok_m);
     cx_test_register(suite, test_strtok_delim);
     cx_test_register(suite, test_strtok_next_easy);
     cx_test_register(suite, test_strtok_next_unlimited);

mercurial