sdyna¶
Category: string
GitHub: cute_string.h
An empty macro used in the C API to markup dynamic strings.
Code Example¶
Showcase of basic dynamic string features.
// Create a new dynamic string.
sdyna char* s = smake("Hello world!");
printf("%s\n", s); // Prints: Hello world!
// Overwrite it with a new value.
sset(s, "Goodbye!");
printf("%s\n", s); // Prints: Goodbye!
// Append text to the string.
sappend(s, " See you later.");
printf("%s\n", s); // Prints: Goodbye! See you later.
// Printf-style formatting.
sfmt(s, "The answer is %d", 42);
printf("%s\n", s); // Prints: The answer is 42
// String comparison.
if (sequ(s, "The answer is 42")) {
printf("Correct!\n");
}
// Clean up.
sfree(s);
Remarks¶
This is an optional and completely empty macro. It's only purpose is to provide a bit of visual indication a type is a
dynamic string. One downside of the C-macro API is the opaque nature of the pointer type. Since the macros use polymorphism
on typed pointers (char*), there's no actual string struct type visible. sdyna helps visually indicate a pointer is a
dynamic string, not just a plain char*.
Dynamic strings are 100% compatible with normal C-strings -- pass them to printf, strcmp, etc.
They automatically grow on the heap as needed. Free with sfree when done.
To create a new string use smake or sfmake. To overwrite an existing string use sset or sfmt.
Note: sset/sfmt require an l-value (a variable), not a literal like NULL -- use smake/sfmake to create from scratch.
Related Pages¶
sequ
smake
sfmake
sset
sfmt
sappend
sfree
slen