CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
parse.h
Go to the documentation of this file.
1#pragma once
2
4#include <cx/cx.h>
5#include <cx/string.h>
6#include <cx/stype/stvar.h>
7#include <cx/utils/lazyinit.h>
8
11
12CX_C_BEGIN
13
209
215
219typedef struct StrPattern StrPattern;
220
221// Internal - use the strPatternCreate() macro
222_Ret_opt_valid_ StrPattern* _strPatternCreate(_In_opt_ strref pat, flags_t flags);
223
238#define strPatternCreate(pat, ...) _strPatternCreate(pat, opt_flags(__VA_ARGS__))
239
243void strPatternDestroy(_Inout_ StrPattern* _Nullable* _Nonnull pat);
244
245// Internal - use the strPatternMatch() macro
246bool _strPatternMatch(_In_ StrPattern* pat, _In_opt_ strref s, int n, _In_ stvp* dests);
247
264#define strPatternMatch(pat, s, ...) \
265 _strPatternMatch(pat, s, count_macro_args(__VA_ARGS__), (stvp[]) { __VA_ARGS__ })
266
267// Internal - use the strPatternMatchAt() macro
268bool _strPatternMatchAt(_Inout_ int32* io_pos, _In_ StrPattern* pat, _In_opt_ strref s, int n,
269 _In_ stvp* dests);
270
285#define strPatternMatchAt(io_pos, pat, s, ...) \
286 _strPatternMatchAt(io_pos, pat, s, count_macro_args(__VA_ARGS__), (stvp[]) { __VA_ARGS__ })
287
288// Internal - use the strParse() macro
289bool _strParse(_In_opt_ strref s, _In_opt_ strref pat, int n, _In_ stvp* dests);
290
309#define strParse(s, pat, ...) \
310 _strParse(s, pat, count_macro_args(__VA_ARGS__), (stvp[]) { __VA_ARGS__ })
311
312// strFormat learned the hard way that sizeof((stvar[]){...})/sizeof(stvar) makes MSVC
313// allocate double the stack space and push the argument list twice. Same reason for
314// count_macro_args here.
315
327
328// Internal - use the strPat() macro
329_Ret_opt_valid_ StrPattern* _strPatGet(_Inout_ StrPatternDecl* decl);
330
331#define _STR_PATTERN_DECL(name, cname, patstr, patflags) \
332 STR_CONSTRL(cname, patstr); \
333 static StrPatternDecl name = { _SR(cname), (patflags) }
334
356#define STR_PATTERN(name, patstr, ...) \
357 _STR_PATTERN_DECL(name, _strpat_##name, patstr, opt_flags(__VA_ARGS__))
358
366#define strPat(name) _strPatGet(&name)
367
375
383
385typedef struct ParseVar {
386 sa_string opts;
387 string def;
388 flags_t flags;
390
392
394
395CX_C_END
PARSEVarFlags
Parse variable flags for Parsable interface implementations.
Definition parse.h:377
@ PARSEVar_CaseInsensitive
the pattern was compiled case-insensitive
Definition parse.h:378
@ PARSEVar_Trim
the trim option was given
Definition parse.h:379
@ PARSEVar_Lower
the lower option was given
Definition parse.h:381
@ PARSEVar_Upper
the upper option was given
Definition parse.h:380
struct StrPattern StrPattern
Definition parse.h:219
STRPAT_FLAGS
Flags controlling how a pattern is compiled and matched.
Definition parse.h:211
void strPatternDestroy(StrPattern **pat)
@ STRPAT_CaseI
Literal text matches without regard to case.
Definition parse.h:213
@ STRPAT_ExactWS
Whitespace in the pattern matches exactly, not a run.
Definition parse.h:212
Thread-safe lazy initialization.
Dynamic arrays with type-safe generic programming.
Copy-on-write strings with automatic memory management and rope optimization.
State tracker for lazy initialization.
Definition lazyinit.h:40
Parse variable descriptor for Parsable interface implementations.
Definition parse.h:385
string def
Default text, if the placeholder had one.
Definition parse.h:387
flags_t flags
PARSEVarFlags.
Definition parse.h:388
sa_string opts
Parse options the pattern gave that the object must interpret.
Definition parse.h:386
LazyInitState state
Guards the one-time compile.
Definition parse.h:323
StrPattern * compiled
The compiled pattern, once there is one.
Definition parse.h:324
struct StrPatternDecl * next
Links every compiled pattern off a global root.
Definition parse.h:325
strref pat
Pattern text.
Definition parse.h:321
flags_t flags
STRPAT_FLAGS.
Definition parse.h:322
Definition stvar.h:293
Variant type containers and type-safe variadic argument support.