docs/Writerside/topics/string.h.md

changeset 1297
0811fb9a8dba
parent 1226
129ef9bb1477
equal deleted inserted replaced
1296:2a2403c63439 1297:0811fb9a8dba
40 cxmutstr cx_strdup_a(const CxAllocator *allocator, AnyStr string); 40 cxmutstr cx_strdup_a(const CxAllocator *allocator, AnyStr string);
41 41
42 void cx_strfree(cxmutstr *str); 42 void cx_strfree(cxmutstr *str);
43 43
44 void cx_strfree_a(const CxAllocator *alloc, cxmutstr *str); 44 void cx_strfree_a(const CxAllocator *alloc, cxmutstr *str);
45
46
47 #define CX_SFMT(s) (int) (s).length, (s).ptr
48 #define CX_PRIstr ".*s"
45 ``` 49 ```
46 50
47 The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*` 51 The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*`
48 and compute the length with a call to stdlib `strlen()`. 52 and compute the length with a call to stdlib `strlen()`.
49 In case you already know the length, or the string is not zero-terminated, you can use `cx_strn()` or `cx_mutstrn()`. 53 In case you already know the length, or the string is not zero-terminated, you can use `cx_strn()` or `cx_mutstrn()`.
54 58
55 Allocated strings are always of type `cxmutstr` and can be deallocated by a call to `cx_strfree()` or `cx_strfree_a()`. 59 Allocated strings are always of type `cxmutstr` and can be deallocated by a call to `cx_strfree()` or `cx_strfree_a()`.
56 The caller must make sure to use the correct allocator for deallocating a string. 60 The caller must make sure to use the correct allocator for deallocating a string.
57 It is safe to call these functions multiple times on a given string, as the pointer will be nulled and the length set to zero. 61 It is safe to call these functions multiple times on a given string, as the pointer will be nulled and the length set to zero.
58 It is also safe to call the functions with a `NULL`-pointer, just like any other `free()`-like function. 62 It is also safe to call the functions with a `NULL`-pointer, just like any other `free()`-like function.
63
64 When you want to use a UCX string in a `printf`-like function, you can use the macro `CX_PRIstr` for the format specifier,
65 and the `CX_SFMT(s)` macro to expand the arguments.
59 66
60 > When you want to convert a string _literal_ into a UCX string, you can also use the `CX_STR(lit)` macro. 67 > When you want to convert a string _literal_ into a UCX string, you can also use the `CX_STR(lit)` macro.
61 > This macro uses the fact that `sizeof(lit)` for a string literal `lit` is always the string length plus one, 68 > This macro uses the fact that `sizeof(lit)` for a string literal `lit` is always the string length plus one,
62 > effectively saving an invocation of `strlen()`. 69 > effectively saving an invocation of `strlen()`.
63 > However, this only works for literals - in all other cases you must use `cx_str()` or `cx_strn`. 70 > However, this only works for literals - in all other cases you must use `cx_str()` or `cx_strn`.

mercurial