ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
helpers.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef DS_HELPERS_H
4#define DS_HELPERS_H 1
5
6//! @file ds/helpers.h
7//! @author Avinash Maddikonda (svasssakavi@gmail.com)
8//! @brief Declaration of helper constants and functions.
9//! @since 0.2.0
10//! @date 2023-07-25
11
12#include <stdbool.h>
13
14#ifdef __cplusplus
15extern "C"
16{
17#endif /* __cplusplus */
18
19 /// @brief Returns `true` if @p ptr is `NULL`.
20 /// @param ptr The pointer to check.
21 /// @return `true` if @p ptr is `NULL`, and `false` otherwise.
22 ///
23 /// ### Examples
24 ///
25 /// Basic usage:
26 ///
27 /// ```c
28 /// assert (ds_helpers_is_null (NULL));
29 /// ```
30 extern bool ds_helpers_is_null (void const *ptr);
31
32 /// @brief Returns `true` if @p ptr is a valid non-`NULL` pointer.
33 /// @param ptr The pointer to check.
34 /// @return `true` if @p ptr is a valid non-`NULL` pointer. `false`
35 /// otherwise.
36 ///
37 /// ### Examples
38 ///
39 /// Basic usage:
40 ///
41 /// ```c
42 /// int x = 0;
43 /// assert (ds_helpers_is_instance (&x));
44 /// ```
45 extern bool ds_helpers_is_instance (void const *ptr);
46
47/// @brief Compares and returns the minimum of two values.
48/// @param self The first value to compare.
49/// @param other The second value to compare.
50/// @return The minimum of @p self and @p other.
51/// @return @p self if the comparison determines them to be equal.
52///
53/// ### Examples
54///
55/// Basic usage:
56///
57/// ```c
58/// assert (DS_HELPERS_MIN (1, 2) == 1);
59/// assert (DS_HELPERS_MIN (2, 2) == 2);
60/// ```
61#define DS_HELPERS_MIN(self, other) (((self) <= (other)) ? (self) : (other))
62
63/// @brief Compares and returns the maximum of two values.
64/// @param self The first value to compare.
65/// @param other The second value to compare.
66/// @return The maximum of @p self and @p other.
67/// @return @p other if the comparison determines them to be equal.
68///
69/// ### Examples
70///
71/// Basic usage:
72///
73/// ```c
74/// assert (DS_HELPERS_MAX (1, 2) == 2);
75/// assert (DS_HELPERS_MAX (2, 2) == 2);
76/// ```
77#define DS_HELPERS_MAX(self, other) (((self) > (other)) ? (self) : (other))
78
79/// @brief Restrict a value to a certain interval.
80/// @param self The value to restrict.
81/// @param min The inclusive lower bound of the interval.
82/// @param max The inclusive upper bound of the interval.
83/// @return @p max if @p self is greater than @p max, and @p min if @p
84/// self is less than @p min. Otherwise this returns @p self.
85///
86/// @attention Always ensure that @p min is less than or equal to @p max.
87///
88/// ### Examples
89///
90/// Basic usage:
91///
92/// ```c
93/// assert (DS_HELPERS_CLAMP (-3, -2, 1), -2);
94/// assert (DS_HELPERS_CLAMP (0, -2, 1), 0);
95/// assert (DS_HELPERS_CLAMP (2, -2, 1), 1);
96/// ```
97#define DS_HELPERS_CLAMP(self, min, max) \
98 (DS_HELPERS_MAX ((min), (DS_HELPERS_MIN ((self), (max)))))
99
100/// @brief Checks if @p self lies in the right-exclusive interval between @p
101/// start and @p end.
102/// @param self The value to check.
103/// @param start The inclusive start of the interval.
104/// @param end The exclusive end of the interval.
105/// @return `true` if @p self lies in the right-exclusive interval between @p
106/// start and @p end, and `false` otherwise.
107///
108/// ### Examples
109///
110/// Basic usage:
111///
112/// ```c
113/// assert (DS_HELPERS_IN_RANGE (0, 0, 10));
114/// assert (!DS_HELPERS_IN_RANGE (10, 0, 10));
115/// ```
116#define DS_HELPERS_IN_RANGE(self, start, end) \
117 (((self) >= (start)) && ((self) < (end)))
118
119/// @brief Checks if @p self lies in the inclusive interval between @p start
120/// and @p end.
121/// @param self The value to check.
122/// @param start The inclusive start of the interval.
123/// @param end The inclusive end of the interval.
124/// @return `true` if @p self lies in the inclusive interval between @p start
125/// and @p end, and `false` otherwise.
126///
127/// ### Examples
128///
129/// Basic usage:
130///
131/// ```c
132/// assert (DS_HELPERS_IN_RANGE_INCLUSIVE (0, 0, 10));
133/// assert (!DS_HELPERS_IN_RANGE_INCLUSIVE (11, 0, 10));
134/// ```
135#define DS_HELPERS_IN_RANGE_INCLUSIVE(self, start, end) \
136 (((self) >= (start)) && ((self) <= (end)))
137
138#ifdef __cplusplus
139}
140#endif /* __cplusplus */
141
142#endif /* DS_HELPERS_H */
bool ds_helpers_is_null(void const *ptr)
Returns true if ptr is NULL.
Definition: helpers.c:13
bool ds_helpers_is_instance(void const *ptr)
Returns true if ptr is a valid non-NULL pointer.
Definition: helpers.c:19