adds cx_bstr() and cx_bstr_m()

Mon, 22 Dec 2025 16:21:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 22 Dec 2025 16:21:04 +0100
changeset 1654
5ab3fe0b7859
parent 1653
6a842bd49fea
child 1655
745ae507ee14

adds cx_bstr() and cx_bstr_m()

CHANGELOG file | annotate | diff | comparison | revisions
docs/Writerside/topics/about.md file | annotate | diff | comparison | revisions
docs/Writerside/topics/mempool.h.md file | annotate | diff | comparison | revisions
src/cx/buffer.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
tests/test_json.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Mon Dec 22 16:12:20 2025 +0100
+++ b/CHANGELOG	Mon Dec 22 16:21:04 2025 +0100
@@ -11,6 +11,7 @@
  * adds cxMapCompare()
  * adds line continuation support to CxProperties / CxPropertiesConfig
  * adds cx_hash_key_as_string()
+ * adds cx_bstr() and cx_bstr_m()
  * adds cxBufferMaximumCapacity()
  * adds cxBufferAppendString()
  * adds CX_BUFFER_DO_NOT_FREE buffer flag
--- a/docs/Writerside/topics/about.md	Mon Dec 22 16:12:20 2025 +0100
+++ b/docs/Writerside/topics/about.md	Mon Dec 22 16:21:04 2025 +0100
@@ -38,6 +38,7 @@
 * adds cxMapCompare()
 * adds line continuation support to CxProperties / CxPropertiesConfig
 * adds cx_hash_key_as_string()
+* adds cx_bstr() and cx_bstr_m()
 * adds cxBufferMaximumCapacity()
 * adds cxBufferAppendString()
 * adds CX_BUFFER_DO_NOT_FREE buffer flag
--- a/docs/Writerside/topics/mempool.h.md	Mon Dec 22 16:12:20 2025 +0100
+++ b/docs/Writerside/topics/mempool.h.md	Mon Dec 22 16:21:04 2025 +0100
@@ -173,7 +173,7 @@
         f, content, (cx_read_func) fread, cxBufferWriteFunc
     );
     fclose(f);
-    cxstring contentstr = cx_strn(content->space, content->size);
+    cxstring contentstr = cx_bstr(content);
 
     // split the string into lines
     // use the memory pool to allocate the target array
--- a/src/cx/buffer.h	Mon Dec 22 16:12:20 2025 +0100
+++ b/src/cx/buffer.h	Mon Dec 22 16:21:04 2025 +0100
@@ -613,6 +613,26 @@
 cx_attr_nonnull
 CX_EXPORT int cxBufferGet(CxBuffer *buffer);
 
+/**
+ * Gets the data in a buffer as a @c cxstring.
+ *
+ * @param buffer the buffer
+ * @return the data in the buffer interpreted as a @c cxstring
+ */
+CX_INLINE cxstring cx_bstr(CxBuffer *buffer) {
+    return cx_strn(buffer->space, buffer->size);
+}
+
+/**
+ * Gets the data in a buffer as a @c cxmutstr.
+ *
+ * @param buffer the buffer
+ * @return the data in the buffer interpreted as a @c cxmutstr
+ */
+CX_INLINE cxmutstr cx_bstr_m(CxBuffer *buffer) {
+    return cx_mutstrn(buffer->space, buffer->size);
+}
+
 #ifdef __cplusplus
 }
 #endif
--- a/src/json.c	Mon Dec 22 16:12:20 2025 +0100
+++ b/src/json.c	Mon Dec 22 16:21:04 2025 +0100
@@ -1442,7 +1442,7 @@
         return (cxmutstr){NULL, 0};
         // LCOV_EXCL_STOP
     } else {
-        cxmutstr str = cx_mutstrn(buffer.space, buffer.size);
+        cxmutstr str = cx_bstr_m(&buffer);
         cxBufferDestroy(&buffer);
         return str;
     }
--- a/tests/test_json.c	Mon Dec 22 16:12:20 2025 +0100
+++ b/tests/test_json.c	Mon Dec 22 16:21:04 2025 +0100
@@ -1778,7 +1778,7 @@
     CX_TEST_ASSERT(result == 0);
 
     // compare the string
-    CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected));
+    CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), expected));
 
     // destroy everything
     cxBufferDestroy(&buf);
@@ -1919,7 +1919,7 @@
         CX_TEST_ASSERT(result == 0);
 
         // compare the string
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), expected));
 
         // destroy everything
         cxBufferDestroy(&buf);
@@ -1938,45 +1938,45 @@
     CX_TEST_DO {
         // test default settings (6 digits)
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "3.141592"));
 
         // test too many digits
         cxBufferReset(&buf);
         writer.frac_max_digits = 200;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141592653589793"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "3.141592653589793"));
         
         // test 0 digits
         cxBufferReset(&buf);
         writer.frac_max_digits = 0;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "3"));
 
         // test 2 digits
         cxBufferReset(&buf);
         writer.frac_max_digits = 2;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.14"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "3.14"));
 
         // test 3 digits
         cxBufferReset(&buf);
         writer.frac_max_digits = 3;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "3.141"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "3.141"));
 
         // test 6 digits, but two are left of the decimal point
         num->number = 47.110815;
         cxBufferReset(&buf);
         writer.frac_max_digits = 6;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "47.110815"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "47.110815"));
 
         // test 4 digits with exponent
         num->number = 5.11223344e23;
         cxBufferReset(&buf);
         writer.frac_max_digits = 4;
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, num, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "5.1122e+23"));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "5.1122e+23"));
     }
     cxBufferDestroy(&buf);
     cxJsonValueFree(num);
@@ -2003,7 +2003,7 @@
     cxBufferInit(&buf, NULL, NULL, 128, 0);
     CX_TEST_DO {
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf),
             "\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\""));
     }
     cxBufferDestroy(&buf);
@@ -2019,7 +2019,7 @@
     cxBufferInit(&buf, NULL, NULL, 128, 0);
     CX_TEST_DO {
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, obj, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size),
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf),
             "{\"hello\\twörld\\r\\nthis is\\\\a \\\"string\\\"\\b in \\u0007 string\\f\":true}"));
     }
     cxBufferDestroy(&buf);
@@ -2034,13 +2034,13 @@
     CX_TEST_DO {
         // default: do not escape
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test/solidus\""));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "\"test/solidus\""));
 
         // enable escaping
         writer.escape_slash = true;
         cxBufferReset(&buf);
         CX_TEST_ASSERT(0 == cxJsonWrite(&buf, str, cxBufferWriteFunc, &writer));
-        CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), "\"test\\/solidus\""));
+        CX_TEST_ASSERT(0 == cx_strcmp(cx_bstr(&buf), "\"test\\/solidus\""));
     }
     cxBufferDestroy(&buf);
     cxJsonValueFree(str);

mercurial