| 72 It is also safe to call the functions with a `NULL`-pointer, just like any other `free()`-like function. |
72 It is also safe to call the functions with a `NULL`-pointer, just like any other `free()`-like function. |
| 73 |
73 |
| 74 When you want to use a UCX string in a `printf`-like function, you can use the macro `CX_PRIstr` for the format specifier, |
74 When you want to use a UCX string in a `printf`-like function, you can use the macro `CX_PRIstr` for the format specifier, |
| 75 and the `CX_SFMT(s)` macro to expand the arguments. |
75 and the `CX_SFMT(s)` macro to expand the arguments. |
| 76 |
76 |
| 77 > When you want to convert a string _literal_ into a UCX string, you can also use the `CX_STR(lit)` macro. |
77 > When you want to convert a string _literal_ into a UCX string, you can also use the `cx_str()` function. |
| 78 > This macro uses the fact that `sizeof(lit)` for a string literal `lit` is always the string length plus one, |
78 > With optimizations turned on, this function gets inlined and optimizes the call to `strlen()` out, giving |
| 79 > effectively saving an invocation of `strlen()`. |
79 > you a `cxstring` structure at zero cost with the length calculated at compile-time. |
| 80 > However, this only works for literals - in all other cases you must use `cx_str()` or `cx_strn`. |
|
| 81 |
80 |
| 82 ## Comparison |
81 ## Comparison |
| 83 |
82 |
| 84 ```C |
83 ```C |
| 85 #include <cx/string.h> |
84 #include <cx/string.h> |
| 292 #include <cx/string.h> |
291 #include <cx/string.h> |
| 293 |
292 |
| 294 cxstring str = cx_str("an,arbitrarily;||separated;string"); |
293 cxstring str = cx_str("an,arbitrarily;||separated;string"); |
| 295 |
294 |
| 296 // create the context |
295 // create the context |
| 297 CxStrtokCtx ctx = cx_strtok(str, CX_STR(","), 10); |
296 CxStrtokCtx ctx = cx_strtok(str, ",", 10); |
| 298 |
297 |
| 299 // add two more delimters |
298 // add two more delimters |
| 300 cxstring delim_more[2] = {CX_STR("||"), CX_STR(";")}; |
299 cxstring delim_more[2] = {cx_str("||"), cx_str(";")}; |
| 301 cx_strtok_delim(&ctx, delim_more, 2); |
300 cx_strtok_delim(&ctx, delim_more, 2); |
| 302 |
301 |
| 303 // iterate over the tokens |
302 // iterate over the tokens |
| 304 cxstring tok; |
303 cxstring tok; |
| 305 while(cx_strtok_next(&ctx, &tok)) { |
304 while(cx_strtok_next(&ctx, &tok)) { |