CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
strbase.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cx/cx.h>
7
8CX_C_BEGIN
9
10typedef struct str_ref {
11 void* _is_string;
12} str_ref;
13
14// Layout of the static empty string. The real type has to be visible here rather
15// than declaring the object as a str_ref, or LTO builds see a type mismatch
16// between this declaration and the definition in strempty.c
17typedef struct str_empty_data {
18 unsigned char hdr; // STR_CX | STR_ASCII | STR_UTF8 | STR_LEN8
19 unsigned char magic; // 0xc1 cx string marker
20 unsigned char len; // length, always 0
21 unsigned char nul; // NUL terminator, so strC() has something valid to return
22} str_empty_data;
23
24extern str_empty_data _emptyStringData; // internal data for the empty string
25extern strref emptyString; // public constant for empty string
26
30
42typedef struct str_ref* _Nullable string;
43
58typedef const struct str_ref* _Nullable strref;
59
64typedef string* _Nonnull strhandle;
65
80#define strInit(o) *(o) = NULL
81
98void strReset(_Inout_ptr_opt_ strhandle o, uint32 sizehint);
99
128void strDup(_Inout_ strhandle o, _In_opt_ strref s);
129
147void strCopy(_Inout_ strhandle o, _In_opt_ strref s);
148
174bool strFromBytes(_Inout_ strhandle o, _In_reads_bytes_opt_(sz) const void* _Nullable buf,
175 uint32 sz);
176
192void strClear(_Inout_ strhandle ps);
193
210_When_(s == NULL, _Post_equal_to_(0)) _Pure uint32 strLen(_In_opt_ strref s);
211
227void strSetLen(_Inout_ strhandle ps, uint32 len);
228
243_When_(s == NULL, _Post_equal_to_(true)) _Pure bool strEmpty(_In_opt_ strref s);
244
262void strDestroy(_Inout_ strhandle ps);
263
284_Ret_valid_ const char* _Nonnull strC(_In_opt_ strref s);
285
307_Ret_valid_ const char* _Nonnull strPC(_Inout_ strhandle ps);
308
333_Ret_valid_ uint8* _Nonnull strBuffer(_Inout_ strhandle ps, uint32 minsz);
334
355uint32 strCopyOut(_In_opt_ strref s, uint32 off, _Out_writes_bytes_(bufsz) uint8* _Nonnull buf,
356 uint32 bufsz);
357
377uint32 strCopyRaw(_In_opt_ strref s, uint32 off, _Out_writes_bytes_(maxlen) uint8* _Nonnull buf,
378 uint32 maxlen);
379
380_Pure uint32 _strStackAllocSize(uint32 maxlen);
381void _strInitStack(_Inout_ _Deref_pre_valid_ _Deref_post_opt_valid_ strhandle ps, uint32 maxlen);
382
411#define strTemp(ps, maxlen) \
412 (*(ps)) = (string)stackAlloc(_strStackAllocSize(maxlen)); \
413 _strInitStack(ps, maxlen);
414
416
417#include "strliteral.h"
418
419CX_C_END
uint32 strLen(strref s)
bool strEmpty(strref s)
void strSetLen(strhandle ps, uint32 len)
string * strhandle
Pointer to a string variable.
Definition strbase.h:64
void strReset(strhandle o, uint32 sizehint)
struct str_ref * string
Opaque handle to a string object.
Definition strbase.h:42
const char * strC(strref s)
void strDestroy(strhandle ps)
uint32 strCopyOut(strref s, uint32 off, uint8 *buf, uint32 bufsz)
const struct str_ref * strref
Borrowed reference to a string.
Definition strbase.h:58
uint8 * strBuffer(strhandle ps, uint32 minsz)
void strDup(strhandle o, strref s)
void strClear(strhandle ps)
const char * strPC(strhandle ps)
uint32 strCopyRaw(strref s, uint32 off, uint8 *buf, uint32 maxlen)
void strCopy(strhandle o, strref s)
bool strFromBytes(strhandle o, _In_reads_bytes_opt_(sz) const void *buf, uint32 sz)
String literal prefix macros and compile-time constant declarations.