Integral sets

Overview

Generic functions to operate on integral sets. More…

// typedefs

typedef struct te_intset_ops te_intset_ops;
typedef struct te_charset te_charset;

// structs

struct te_charset;
struct te_intset_ops;

// global variables

const te_intset_ops te_bits_intset;
const te_intset_ops te_charset_intset;
const te_intset_ops te_fdset_intset;

// global functions

te_errno te_intset_generic_parse(const te_intset_ops* ops, int minval, int maxval, const char* str, void* val);
char* te_intset_generic2string(const te_intset_ops* ops, int minval, int maxval, const void* val);
bool te_intset_generic_is_subset(const te_intset_ops* ops, int minval, int maxval, const void* subset, const void* superset);
void te_intset_generic_add_range(const te_intset_ops* ops, void* val, int first, int last);
void te_intset_generic_remove_range(const te_intset_ops* ops, void* val, int first, int last);
static te_errno te_bits_parse(const char* str, uint64_t* val);
static char* te_bits2string(uint64_t val);
static void te_charset_clear(te_charset* cset);
static void te_charset_add_range(te_charset* cset, uint8_t minbyte, uint8_t maxbyte);
static void te_charset_remove_range(te_charset* cset, uint8_t minbyte, uint8_t maxbyte);
static bool te_charset_check(const te_charset* cset, uint8_t byte);
void te_charset_add_from_string(te_charset* cset, const char* str);
void te_charset_get_bytes(const te_charset* cset, uint8_t buf[static UINT8_MAX+1]);
bool te_charset_check_bytes(const te_charset* cset, size_t len, const uint8_t* bytes);
static te_errno te_fdset_parse(const char* str, fd_set* fdset);
static char* te_fdset2string(int nfds, const fd_set* fdset);
static bool te_fdset_is_subset(int nfds, const fd_set* sub, const fd_set* super);

// macros

#define TE_CHARSET_INIT

Detailed Documentation

Generic functions to operate on integral sets.

The functions in this module use dynamic dispatching, so they may not be suitable for use in really performance-critical cases.

Typedefs

typedef struct te_intset_ops te_intset_ops

A description of a specific intset type.

typedef struct te_charset te_charset

Character set or class.

Global Variables

const te_intset_ops te_bits_intset

A description of 64-bit integer as a bit set

const te_intset_ops te_charset_intset

Description of a te_charset as an intset.

const te_intset_ops te_fdset_intset

Description of POSIX fd_set as an integral set.

Global Functions

te_errno te_intset_generic_parse(const te_intset_ops* ops, int minval, int maxval, const char* str, void* val)

Convert a string to an integral set.

The string str is a comma-separated list of single numbers and ranges, e.g. 1,2-10,100.

The set val will be cleared before parsing.

Parameters:

ops

intset type description

minval

minimum possible value in a set

maxval

maximum possible value in a set

str

string to parse

val

target value

Returns:

status code

char* te_intset_generic2string(const te_intset_ops* ops, int minval, int maxval, const void* val)

Convert an integral set to a string.

The resulting string may be passed to te_intset_generic_parse() yielding the original set. Sequences of consecutive numbers are represented as ranges N-M.

maxval should be less than INT_MAX and in general the function is not very efficient if maxval is too high.

Parameters:

ops

intset type description

minval

minimum possible value in a set

maxval

maximum possible value in a set

val

intset

Returns:

a string representation or NULL in case of an error (it should be free()’d)

bool te_intset_generic_is_subset(const te_intset_ops* ops, int minval, int maxval, const void* subset, const void* superset)

Tests whether subset is really a subset of superset.

Parameters:

ops

intset type description

minval

minimum possible value in a set

maxval

maximum possible value in a set

subset

a subset to test

superset

its superset

Returns:

true iff subset is a subset of superset

void te_intset_generic_add_range(const te_intset_ops* ops, void* val, int first, int last)

Add a contiguous range of integers to the set val.

Parameters:

ops

intset type description

val

intset

first

start of the range

last

end of the range

void te_intset_generic_remove_range(const te_intset_ops* ops, void* val, int first, int last)

Remove a contiguous range of integers from the set val.

Parameters:

ops

intset type description

val

intset

first

start of the range

last

end of the range

static te_errno te_bits_parse(const char* str, uint64_t* val)

Convert a string to a 64-bit integer treated as a bit set.

The string str is a comma-separated list of single numbers and ranges, e.g. 1,2-10,63.

Parameters:

str

input string

val

resulting value

Returns:

status code

See also:

te_intset_generic_parse

static char* te_bits2string(uint64_t val)

Convert a 64-bit integer treated as a bit set to a string.

Sequences of consecutive bits are represented as ranges N-M.

Parameters:

val

input integer

Returns:

a string representation or NULL in case of an error (it should be free()’d)

See also:

te_intset_generic2string

static void te_charset_clear(te_charset* cset)

Clear the character set cset.

static void te_charset_add_range(te_charset* cset, uint8_t minbyte, uint8_t maxbyte)

Add a contiguous range of characters to a charset.

Parameters:

cset

character set

minbyte

starting character

maxbyte

end character

static void te_charset_remove_range(te_charset* cset, uint8_t minbyte, uint8_t maxbyte)

Remove a contiguous range of characters from a charset.

Parameters:

cset

character set

minbyte

starting character

maxbyte

end character

static bool te_charset_check(const te_charset* cset, uint8_t byte)

Check whether a byte is in a charset cset.

Parameters:

cset

character set

byte

a byte to check

Returns:

true iff byte is in cset

void te_charset_add_from_string(te_charset* cset, const char* str)

Add all characters from str to a charset cset.

The terminating zero character is not added.

Parameters:

cset

character set

str

characters to add

void te_charset_get_bytes(const te_charset* cset, uint8_t buf[static UINT8_MAX+1])

Get all bytes set in cset as a contiguous array.

The number of stores bytes is equal to te_charset::n_items. Essentially, it is an inverse of te_charset_add_from_string(), however, it does not produce a zero-terminated string.

Parameters:

cset

character set

buf

buffer for resulting bytes (should be at least 256 bytes long)

bool te_charset_check_bytes(const te_charset* cset, size_t len, const uint8_t* bytes)

Check that all bytes belong to a charset cset.

Parameters:

cset

character set

len

length of bytes

bytes

data to check

Returns:

true if all bytes are in cset.

static te_errno te_fdset_parse(const char* str, fd_set* fdset)

Convert a string to a fd_set.

The string str is a comma-separated list of single numbers and ranges, e.g. 1,2-10,511.

Parameters:

str

string to parse

fdset

resulting FD set

Returns:

status code

See also:

te_intset_generic_parse

static char* te_fdset2string(int nfds, const fd_set* fdset)

Convert an FD set to a string.

Sequences of consecutive FD numbers are represented as ranges N-M.

Parameters:

nfds

highest FD number in fdset plus 1 (the same as with POSIX select())

fdset

FD set

Returns:

a string representation or NULL in case of an error. (it should be free()’d)

See also:

te_intset_generic2string

static bool te_fdset_is_subset(int nfds, const fd_set* sub, const fd_set* super)

Check whether a FD set sub is a subset of super.

Parameters:

nfds

highest FD number in fdset plus 1 (the same as with POSIX select())

sub

FD set to test

super

FD superset

Returns:

true iff sub is a subset of super

Macros

#define TE_CHARSET_INIT

Static initialiser for te_charset.