ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
helpers.h File Reference

Declaration of helper constants and functions. More...

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

Go to the source code of this file.

Macros

#define DS_HELPERS_MIN(self, other)   (((self) <= (other)) ? (self) : (other))
 Compares and returns the minimum of two values. More...
 
#define DS_HELPERS_MAX(self, other)   (((self) > (other)) ? (self) : (other))
 Compares and returns the maximum of two values. More...
 
#define DS_HELPERS_CLAMP(self, min, max)    (DS_HELPERS_MAX ((min), (DS_HELPERS_MIN ((self), (max)))))
 Restrict a value to a certain interval. More...
 
#define DS_HELPERS_IN_RANGE(self, start, end)    (((self) >= (start)) && ((self) < (end)))
 Checks if self lies in the right-exclusive interval between start and end. More...
 
#define DS_HELPERS_IN_RANGE_INCLUSIVE(self, start, end)    (((self) >= (start)) && ((self) <= (end)))
 Checks if self lies in the inclusive interval between start and end. More...
 

Functions

bool ds_helpers_is_null (void const *ptr)
 Returns true if ptr is NULL. More...
 
bool ds_helpers_is_instance (void const *ptr)
 Returns true if ptr is a valid non-NULL pointer. More...
 

Detailed Description

Declaration of helper constants and functions.

Author
Avinash Maddikonda (svass.nosp@m.saka.nosp@m.vi@gm.nosp@m.ail..nosp@m.com)
Since
0.2.0
Date
2023-07-25

Definition in file helpers.h.

Macro Definition Documentation

◆ DS_HELPERS_CLAMP

#define DS_HELPERS_CLAMP (   self,
  min,
  max 
)     (DS_HELPERS_MAX ((min), (DS_HELPERS_MIN ((self), (max)))))

Restrict a value to a certain interval.

Parameters
selfThe value to restrict.
minThe inclusive lower bound of the interval.
maxThe inclusive upper bound of the interval.
Returns
max if self is greater than max, and min if self is less than min. Otherwise this returns self.
Attention
Always ensure that min is less than or equal to max.

Examples

Basic usage:

assert (DS_HELPERS_CLAMP (-3, -2, 1), -2);
assert (DS_HELPERS_CLAMP (0, -2, 1), 0);
assert (DS_HELPERS_CLAMP (2, -2, 1), 1);
#define DS_HELPERS_CLAMP(self, min, max)
Restrict a value to a certain interval.
Definition: helpers.h:97

Definition at line 97 of file helpers.h.

◆ DS_HELPERS_IN_RANGE

#define DS_HELPERS_IN_RANGE (   self,
  start,
  end 
)     (((self) >= (start)) && ((self) < (end)))

Checks if self lies in the right-exclusive interval between start and end.

Parameters
selfThe value to check.
startThe inclusive start of the interval.
endThe exclusive end of the interval.
Returns
true if self lies in the right-exclusive interval between start and end, and false otherwise.

Examples

Basic usage:

assert (DS_HELPERS_IN_RANGE (0, 0, 10));
assert (!DS_HELPERS_IN_RANGE (10, 0, 10));
#define DS_HELPERS_IN_RANGE(self, start, end)
Checks if self lies in the right-exclusive interval between start and end.
Definition: helpers.h:116

Definition at line 116 of file helpers.h.

◆ DS_HELPERS_IN_RANGE_INCLUSIVE

#define DS_HELPERS_IN_RANGE_INCLUSIVE (   self,
  start,
  end 
)     (((self) >= (start)) && ((self) <= (end)))

Checks if self lies in the inclusive interval between start and end.

Parameters
selfThe value to check.
startThe inclusive start of the interval.
endThe inclusive end of the interval.
Returns
true if self lies in the inclusive interval between start and end, and false otherwise.

Examples

Basic usage:

assert (DS_HELPERS_IN_RANGE_INCLUSIVE (0, 0, 10));
assert (!DS_HELPERS_IN_RANGE_INCLUSIVE (11, 0, 10));
#define DS_HELPERS_IN_RANGE_INCLUSIVE(self, start, end)
Checks if self lies in the inclusive interval between start and end.
Definition: helpers.h:135

Definition at line 135 of file helpers.h.

◆ DS_HELPERS_MAX

#define DS_HELPERS_MAX (   self,
  other 
)    (((self) > (other)) ? (self) : (other))

Compares and returns the maximum of two values.

Parameters
selfThe first value to compare.
otherThe second value to compare.
Returns
The maximum of self and other.
other if the comparison determines them to be equal.

Examples

Basic usage:

assert (DS_HELPERS_MAX (1, 2) == 2);
assert (DS_HELPERS_MAX (2, 2) == 2);
#define DS_HELPERS_MAX(self, other)
Compares and returns the maximum of two values.
Definition: helpers.h:77

Definition at line 77 of file helpers.h.

◆ DS_HELPERS_MIN

#define DS_HELPERS_MIN (   self,
  other 
)    (((self) <= (other)) ? (self) : (other))

Compares and returns the minimum of two values.

Parameters
selfThe first value to compare.
otherThe second value to compare.
Returns
The minimum of self and other.
self if the comparison determines them to be equal.

Examples

Basic usage:

assert (DS_HELPERS_MIN (1, 2) == 1);
assert (DS_HELPERS_MIN (2, 2) == 2);
#define DS_HELPERS_MIN(self, other)
Compares and returns the minimum of two values.
Definition: helpers.h:61

Definition at line 61 of file helpers.h.

Function Documentation

◆ ds_helpers_is_instance()

bool ds_helpers_is_instance ( void const *  ptr)

Returns true if ptr is a valid non-NULL pointer.

Parameters
ptrThe pointer to check.
Returns
true if ptr is a valid non-NULL pointer. false otherwise.

Examples

Basic usage:

int x = 0;
assert (ds_helpers_is_instance (&x));
bool ds_helpers_is_instance(void const *ptr)
Returns true if ptr is a valid non-NULL pointer.
Definition: helpers.c:19

Definition at line 19 of file helpers.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds_helpers_is_null()

bool ds_helpers_is_null ( void const *  ptr)

Returns true if ptr is NULL.

Parameters
ptrThe pointer to check.
Returns
true if ptr is NULL, and false otherwise.

Examples

Basic usage:

assert (ds_helpers_is_null (NULL));
bool ds_helpers_is_null(void const *ptr)
Returns true if ptr is NULL.
Definition: helpers.c:13

Definition at line 13 of file helpers.c.

Here is the caller graph for this function: