moddef-c
Loading...
Searching...
No Matches
str.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3/* Length-delimited strings. Protobuf strings are not NUL-terminated, and
4 * document views point straight into the serialized buffer, so every
5 * identifier in the API is an (pointer, length) pair. */
6#ifndef MODDEF_STR_H
7#define MODDEF_STR_H
8
9#include <stdbool.h>
10#include <stdint.h>
11#include <string.h>
12
13typedef struct md_str {
14 const char *p;
15 uint16_t len;
17
18/* String literal → md_str_t (compile-time length). */
19#define MD_STR(lit) ((md_str_t){(lit), (uint16_t)(sizeof(lit) - 1)})
20
21static inline md_str_t md_str_cstr(const char *s)
22{
23 md_str_t out = {s, (uint16_t)(s ? strlen(s) : 0)};
24 return out;
25}
26
27static inline bool md_str_eq(md_str_t a, md_str_t b)
28{
29 return a.len == b.len && (a.len == 0 || memcmp(a.p, b.p, a.len) == 0);
30}
31
32/* Raw byte range inside a serialized document. */
33typedef struct md_bytes {
34 const uint8_t *p;
35 uint32_t len;
37
38#endif /* MODDEF_STR_H */
struct md_str md_str_t
struct md_bytes md_bytes_t
Definition str.h:33
uint32_t len
Definition str.h:35
const uint8_t * p
Definition str.h:34
Definition str.h:13
uint16_t len
Definition str.h:15
const char * p
Definition str.h:14