Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
strings.c
Go to the documentation of this file.
1
13
14#include <banjo/log.h>
15#include <banjo/main.h>
16#include <banjo/string.h>
17
18int main(int argc, char* argv[]) {
19 (void)argc; (void)argv;
20
21 // A view can be created on a literal using BJ_STR.
22 // This operation doesn't allocate anything and happens at compile time.
23 const bj_str title = BJ_STR("Hello, UTF-8!");
24 bj_info("The view spans %zu bytes", title.len);
25
26 // A slice is a view that cuts another one at byte level.
27 const bj_str word = bj_str_slice(title, 7, 12);
28 bj_info("The slice at 7..12 spans %zu bytes", word.len);
29
30 // A string view operated at byte level.
31 // As such, two different encoding ot the same string will be
32 // different.
33 const bj_str precomposed = bj_str_from_cstr("caf\xC3\xA9");
34 const bj_str decomposed = bj_str_from_cstr("cafe\xCC\x81");
35 bj_info("The two spellings of cafe: %zu and %zu bytes, equal: %s",
36 precomposed.len, decomposed.len,
37 bj_str_eq(precomposed, decomposed) ? "yes" : "no");
38
39 // bj_string is an owning string.
40 // It allocats and owns content.
41 // Besides, it validates the UTF-8 encoding and replaces every fail with the
42 // standard U+FFFD code point.
43 // bj_string guarantees a valid UTF-8 of its content, meaning that
44 // downstream code can trust it on this contract.
45 // The view below holds a defect: E0 80 cannot sit where it is.
46 static const char bytes[] = "\xC3\xA9\xE0\x80\x41";
47 const bj_str damaged = { bytes, 5 };
48 bj_string* text = bj_create_string(damaged);
49 if (text == NULL) {
50 bj_err("Out of memory building the string");
51 return 1;
52 }
53 bj_info("The owning string, after replacement: \"%s\"", bj_string_cstr(text));
54
55 // Appending goes through the same replacement policy: the tail is
56 // validated too, the existing bytes untouched.
57 bj_append_string(text, BJ_STR(" (valid again)"));
58 bj_info("After appending: \"%s\"", bj_string_cstr(text));
59
60 // The hidden NUL: the string's view spans its own bytes only, and a
61 // NUL sits right after them, which is why cstr costs nothing.
62 const bj_str view = bj_string_view(text);
63 bj_info("The string holds %zu bytes, and cstr reads \"%s\"",
64 view.len, bj_string_cstr(text));
65
67 return 0;
68}
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
size_t len
Number of bytes in the view.
Definition string.h:59
#define BJ_STR(literal)
Create a view of a string literal.
Definition string.h:75
const char * bj_string_cstr(const struct bj_string *string)
Get a NUL-terminated pointer to an owning string.
bj_bool bj_str_eq(struct bj_str left, struct bj_str right)
Compare two views byte by byte.
struct bj_string * bj_create_string(struct bj_str initial)
Create an owning string from a byte view.
struct bj_str bj_string_view(const struct bj_string *string)
Get a borrowed view of an owning string.
struct bj_str bj_str_slice(struct bj_str view, size_t from, size_t to)
Create a view of a byte range within another view.
struct bj_str bj_str_from_cstr(const char *c_string)
Create a view from a NUL-terminated C string.
void bj_destroy_string(struct bj_string *string)
Destroy an owning string and release its memory.
void bj_append_string(struct bj_string *string, struct bj_str tail)
Append bytes to an owning string.
A borrowed view of a sequence of bytes.
Definition string.h:57
An owning string containing valid UTF-8.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
UTF-8 string views and owning strings.
int main(void)