vttp
An HTTP backed Virtual Table extension for SQLite
Loading...
Searching...
No Matches
Classes | Macros | Functions | Variables
pyc.h File Reference

A python-inspired API for general C scripts. More...

#include <stdbool.h>
#include <stdlib.h>
Include dependency graph for pyc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  str
 Buffer pointer + length. More...
 

Macros

#define STR(__s)   ((struct str) {.hd=__s, .length=sizeof(__s) - 1})
 Static string declaration convenience.
 
#define empty(T)
 Get the empty value for type T.
 
#define next(iter)
 Return the next value of ITER.
 
#define len(iter)
 Read length of ITER.
 
#define hd(iter)
 Get the underyling "value" of ITER.
 
#define done(iter)
 Free dynamically allocated ITER.
 
#define insert(iter, value)
 Return the next value of ITER.
 
#define filter(iter, f)
 Filter out all elements in ITER that don't pass F.
 
#define map(iter, f)
 F(x) for x in ITER. Memory semantics specific to each struct.
 
#define split(iter, match, f)
 Reduce INIT with each x in ITER in the callback F.
 

Functions

int min (int a, int b)
 Get min of A, B, ...
 
int max (int a, int b)
 max function
 
struct str str (const char *fmt,...)
 Malloc a char buffer from the format string FMT and wrap it in the returned str.
 
struct str strn (char *hd, size_t length)
 Move HD of length LENGTH into a struct str.
 
struct str __str_next (struct str s)
 Offset string S buffer pointer by 1.
 
size_t __str_len (struct str s)
 Read S string length.
 
char * __str_get (struct str s)
 Get the pointer to the backing byte buffer in S. This is optional for callers because you can just access s.val directly.
 
bool __str_done (struct str s)
 Cleanup dynamically allocated string S.
 
bool __str_insert (struct str s, char ch)
 Appends char CH directly to S's buffer if IS_MUTABLE. Otherwise, return a copy of S.
 
struct str __str_filter (struct str s, bool f(int c, uint i))
 For each char C at index I in S, include C in the returned S if F(C, I) = true.
 
struct str __str_map (struct str s, int f(int c))
 Map each char C in S to new char F(C).
 
struct str__str_split (struct str s, struct str m, size_t *n)
 Split S against matches of M into N elements.
 
struct str __str_slice (const struct str s, uint start, uint end)
 Get back S[START:END] (end is exclusive index)
 
char * __str_to_string (const struct str s)
 
struct list * __list_next (struct list *ls)
 Get the next node in the list in LS, if it exists.
 
size_t __list_len (struct list *ls)
 Compute number of element in list LS.
 
struct str __list_get (struct list *ls)
 Read underlying of node LS.
 
bool __list_done (struct list *ls)
 Cleanup dynamically allocated list LS. This does NOT free the str buffers from the nodes, so YOU are in charge of freeing those.
 
bool __list_insert (struct list *ls, struct str s)
 Appends S to the tail end of LS dynamically.
 
struct list * __list_filter (struct list **ls, bool f(struct str s, uint i))
 Returns the filtered list head for all nodes that pass F and writes out the head of the reject list to LS.
 
struct str __queue_next (struct queue *q)
 Pops the front string of Q.
 
size_t __queue_len (struct queue *q)
 Read size of Q;.
 
struct str __queue_get (struct queue *q)
 Peek the front string of Q.
 
bool __queue_done (struct queue *q)
 Cleanup dynamically allocated Q.
 
bool __queue_insert (struct queue *q, struct str s)
 Enqueue S in Q.
 
char * dsnprintf (size_t *n, const char *fmt,...)
 

Variables

const struct str STR_EMPTY
 

Detailed Description

A python-inspired API for general C scripts.

Macro Definition Documentation

◆ done

#define done (   iter)
Value:
_Generic((iter), \
struct str: __str_done, \
struct list *: __list_done, \
struct queue *: __queue_done \
)(iter)
bool __list_done(struct list *ls)
Cleanup dynamically allocated list LS. This does NOT free the str buffers from the nodes,...
Definition pyc.c:301
bool __str_done(struct str s)
Cleanup dynamically allocated string S.
Definition pyc.c:125
bool __queue_done(struct queue *q)
Cleanup dynamically allocated Q.
Definition pyc.c:422
Buffer pointer + length.
Definition pyc.h:24

Free dynamically allocated ITER.

◆ empty

#define empty (   T)
Value:
_Generic((T *)0, \
struct str *: STR_EMPTY, \
struct list **: NULL, \
struct queue **: NULL \
)
const struct str STR_EMPTY
Definition pyc.c:16

Get the empty value for type T.

◆ filter

#define filter (   iter,
 
)
Value:
_Generic((iter), \
struct str: __str_filter, \
struct list **: __list_filter \
)((iter), (f))
struct list * __list_filter(struct list **ls, bool f(struct str s, uint i))
Returns the filtered list head for all nodes that pass F and writes out the head of the reject list t...
Definition pyc.c:342
struct str __str_filter(struct str s, bool f(int c, uint i))
For each char C at index I in S, include C in the returned S if F(C, I) = true.
Definition pyc.c:135

Filter out all elements in ITER that don't pass F.

◆ hd

#define hd (   iter)
Value:
_Generic((iter), \
struct str: __str_get, \
struct list *: __list_get, \
struct queue *: __queue_get \
)(iter)
struct str __queue_get(struct queue *q)
Peek the front string of Q.
Definition pyc.c:414
char * __str_get(struct str s)
Get the pointer to the backing byte buffer in S. This is optional for callers because you can just ac...
Definition pyc.c:121
struct str __list_get(struct list *ls)
Read underlying of node LS.
Definition pyc.c:294

Get the underyling "value" of ITER.

◆ insert

#define insert (   iter,
  value 
)
Value:
_Generic((iter), \
struct str: __str_insert, \
struct list *: __list_insert, \
struct queue *: __queue_insert \
)((iter), (value))
bool __queue_insert(struct queue *q, struct str s)
Enqueue S in Q.
Definition pyc.c:438
bool __str_insert(struct str s, char ch)
Appends char CH directly to S's buffer if IS_MUTABLE. Otherwise, return a copy of S.
Definition pyc.c:150
bool __list_insert(struct list *ls, struct str s)
Appends S to the tail end of LS dynamically.
Definition pyc.c:326

Return the next value of ITER.

◆ len

#define len (   iter)
Value:
_Generic((iter), \
struct str: __str_len, \
struct list *: __list_len, \
struct queue *: __queue_len \
)(iter)
size_t __list_len(struct list *ls)
Compute number of element in list LS.
Definition pyc.c:274
size_t __str_len(struct str s)
Read S string length.
Definition pyc.c:117
size_t __queue_len(struct queue *q)
Read size of Q;.
Definition pyc.c:405

Read length of ITER.

◆ map

#define map (   iter,
 
)
Value:
_Generic((iter), \
struct str: __str_map \
)((iter), (f))
struct str __str_map(struct str s, int f(int c))
Map each char C in S to new char F(C).

F(x) for x in ITER. Memory semantics specific to each struct.

◆ next

#define next (   iter)
Value:
_Generic((iter), \
struct str: __str_next, \
struct list *: __list_next, \
struct queue *: __queue_next \
)(iter)
struct str __str_next(struct str s)
Offset string S buffer pointer by 1.
Definition pyc.c:103
struct str __queue_next(struct queue *q)
Pops the front string of Q.
Definition pyc.c:394
struct list * __list_next(struct list *ls)
Get the next node in the list in LS, if it exists.
Definition pyc.c:267

Return the next value of ITER.

◆ split

#define split (   iter,
  match,
 
)
Value:
_Generic((iter), \
struct str: __str_split \
)((iter), (match), (f))
struct str * __str_split(struct str s, struct str m, size_t *n)
Split S against matches of M into N elements.
Definition pyc.c:174

Reduce INIT with each x in ITER in the callback F.

Function Documentation

◆ __list_filter()

struct list * __list_filter ( struct list **  ls,
bool   fstruct str s, uint i 
)

Returns the filtered list head for all nodes that pass F and writes out the head of the reject list to LS.

We maintain a reject list so the caller can be in charge of the reject list's memory. So this is really a partition, but that's a memory-concern and not meant to be the interface (though you could certainly use it like so).

◆ __str_len()

size_t __str_len ( struct str  s)

Read S string length.

Length is defined as the "string" length, NOT the buffer size (which is +1 from the null terminator '\0').

Variable Documentation

◆ STR_EMPTY

const struct str STR_EMPTY
extern

Canonical empty string view. Safe to use everywhere.