Sat, 21 Dec 2024 21:03:28 +0100
add some more overflow treatment and make sure to set errno properly
resolves #469
601 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | /** | |
29 | * \file compare.h | |
30 | * \brief A collection of simple compare functions. | |
31 | * \author Mike Becker | |
32 | * \author Olaf Wintermann | |
33 | * \copyright 2-Clause BSD License | |
34 | */ | |
35 | ||
36 | #ifndef UCX_COMPARE_H | |
37 | #define UCX_COMPARE_H | |
38 | ||
650
77021e06b1a8
fix code not compiling under windows+mingw
Mike Becker <universe@uap-core.de>
parents:
631
diff
changeset
|
39 | #include "common.h" |
77021e06b1a8
fix code not compiling under windows+mingw
Mike Becker <universe@uap-core.de>
parents:
631
diff
changeset
|
40 | |
601 | 41 | #ifdef __cplusplus |
42 | extern "C" { | |
43 | #endif | |
44 | ||
786
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
45 | /** |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
46 | * A comparator function comparing two collection elements. |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
47 | */ |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
48 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
49 | cx_attr_nodiscard |
786
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
50 | typedef int(*cx_compare_func)( |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
51 | const void *left, |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
52 | const void *right |
786
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
53 | ); |
b0ebb3d88407
declare cx_compare_func in compare.h - fixes #344
Mike Becker <universe@uap-core.de>
parents:
762
diff
changeset
|
54 | |
601 | 55 | /** |
56 | * Compares two integers of type int. | |
57 | * | |
58 | * @param i1 pointer to integer one | |
59 | * @param i2 pointer to integer two | |
60 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
61 | * 1 if *i1 is greater than *i2 | |
62 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
63 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
64 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
65 | int cx_cmp_int(const void *i1, const void *i2); |
601 | 66 | |
67 | /** | |
68 | * Compares two integers of type long int. | |
69 | * | |
70 | * @param i1 pointer to long integer one | |
71 | * @param i2 pointer to long integer two | |
72 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
73 | * 1 if *i1 is greater than *i2 | |
74 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
75 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
76 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
77 | int cx_cmp_longint(const void *i1, const void *i2); |
601 | 78 | |
79 | /** | |
80 | * Compares two integers of type long long. | |
81 | * | |
82 | * @param i1 pointer to long long one | |
83 | * @param i2 pointer to long long two | |
84 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
85 | * 1 if *i1 is greater than *i2 | |
86 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
87 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
88 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
89 | int cx_cmp_longlong(const void *i1, const void *i2); |
601 | 90 | |
91 | /** | |
92 | * Compares two integers of type int16_t. | |
93 | * | |
94 | * @param i1 pointer to int16_t one | |
95 | * @param i2 pointer to int16_t two | |
96 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
97 | * 1 if *i1 is greater than *i2 | |
98 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
99 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
100 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
101 | int cx_cmp_int16(const void *i1, const void *i2); |
601 | 102 | |
103 | /** | |
104 | * Compares two integers of type int32_t. | |
105 | * | |
106 | * @param i1 pointer to int32_t one | |
107 | * @param i2 pointer to int32_t two | |
108 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
109 | * 1 if *i1 is greater than *i2 | |
110 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
111 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
112 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
113 | int cx_cmp_int32(const void *i1, const void *i2); |
601 | 114 | |
115 | /** | |
116 | * Compares two integers of type int64_t. | |
117 | * | |
118 | * @param i1 pointer to int64_t one | |
119 | * @param i2 pointer to int64_t two | |
120 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
121 | * 1 if *i1 is greater than *i2 | |
122 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
123 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
124 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
125 | int cx_cmp_int64(const void *i1, const void *i2); |
601 | 126 | |
127 | /** | |
128 | * Compares two integers of type unsigned int. | |
129 | * | |
130 | * @param i1 pointer to unsigned integer one | |
131 | * @param i2 pointer to unsigned integer two | |
132 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
133 | * 1 if *i1 is greater than *i2 | |
134 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
135 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
136 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
137 | int cx_cmp_uint(const void *i1, const void *i2); |
601 | 138 | |
139 | /** | |
140 | * Compares two integers of type unsigned long int. | |
141 | * | |
142 | * @param i1 pointer to unsigned long integer one | |
143 | * @param i2 pointer to unsigned long integer two | |
144 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
145 | * 1 if *i1 is greater than *i2 | |
146 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
147 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
148 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
149 | int cx_cmp_ulongint(const void *i1, const void *i2); |
601 | 150 | |
151 | /** | |
152 | * Compares two integers of type unsigned long long. | |
153 | * | |
154 | * @param i1 pointer to unsigned long long one | |
155 | * @param i2 pointer to unsigned long long two | |
156 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
157 | * 1 if *i1 is greater than *i2 | |
158 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
159 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
160 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
161 | int cx_cmp_ulonglong(const void *i1, const void *i2); |
601 | 162 | |
163 | /** | |
164 | * Compares two integers of type uint16_t. | |
165 | * | |
166 | * @param i1 pointer to uint16_t one | |
167 | * @param i2 pointer to uint16_t two | |
168 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
169 | * 1 if *i1 is greater than *i2 | |
170 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
171 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
172 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
173 | int cx_cmp_uint16(const void *i1, const void *i2); |
601 | 174 | |
175 | /** | |
176 | * Compares two integers of type uint32_t. | |
177 | * | |
178 | * @param i1 pointer to uint32_t one | |
179 | * @param i2 pointer to uint32_t two | |
180 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
181 | * 1 if *i1 is greater than *i2 | |
182 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
183 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
184 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
185 | int cx_cmp_uint32(const void *i1, const void *i2); |
601 | 186 | |
187 | /** | |
188 | * Compares two integers of type uint64_t. | |
189 | * | |
190 | * @param i1 pointer to uint64_t one | |
191 | * @param i2 pointer to uint64_t two | |
192 | * @return -1, if *i1 is less than *i2, 0 if both are equal, | |
193 | * 1 if *i1 is greater than *i2 | |
194 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
195 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
196 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
197 | int cx_cmp_uint64(const void *i1, const void *i2); |
601 | 198 | |
199 | /** | |
200 | * Compares two real numbers of type float with precision 1e-6f. | |
201 | * | |
202 | * @param f1 pointer to float one | |
203 | * @param f2 pointer to float two | |
204 | * @return -1, if *f1 is less than *f2, 0 if both are equal, | |
205 | * 1 if *f1 is greater than *f2 | |
206 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
207 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
208 | cx_attr_nodiscard |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
209 | int cx_cmp_float(const void *f1, const void *f2); |
601 | 210 | |
211 | /** | |
212 | * Compares two real numbers of type double with precision 1e-14. | |
213 | * | |
214 | * @param d1 pointer to double one | |
215 | * @param d2 pointer to double two | |
216 | * @return -1, if *d1 is less than *d2, 0 if both are equal, | |
217 | * 1 if *d1 is greater than *d2 | |
218 | */ | |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
219 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
220 | cx_attr_nodiscard |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
221 | int cx_cmp_double(const void *d1, const void *d2); |
601 | 222 | |
223 | /** | |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
224 | * Compares the integer representation of two pointers. |
601 | 225 | * |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
226 | * @param ptr1 pointer to pointer one (const intptr_t*) |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
227 | * @param ptr2 pointer to pointer two (const intptr_t*) |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
228 | * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
229 | * 1 if *ptr1 is greater than *ptr2 |
601 | 230 | */ |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
231 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
232 | cx_attr_nodiscard |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
233 | int cx_cmp_intptr(const void *ptr1, const void *ptr2); |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
234 | |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
235 | /** |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
236 | * Compares the unsigned integer representation of two pointers. |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
237 | * |
890
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
238 | * @param ptr1 pointer to pointer one (const uintptr_t*) |
54565fd74e74
move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents:
786
diff
changeset
|
239 | * @param ptr2 pointer to pointer two (const uintptr_t*) |
631
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
240 | * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
241 | * 1 if *ptr1 is greater than *ptr2 |
406376e64fd8
tests for compare functions
Mike Becker <universe@uap-core.de>
parents:
605
diff
changeset
|
242 | */ |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
243 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
244 | cx_attr_nodiscard |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
245 | int cx_cmp_uintptr(const void *ptr1, const void *ptr2); |
601 | 246 | |
762
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
247 | /** |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
248 | * Compares the pointers specified in the arguments without de-referencing. |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
249 | * |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
250 | * @param ptr1 pointer one |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
251 | * @param ptr2 pointer two |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
252 | * @return -1 if ptr1 is less than ptr2, 0 if both are equal, |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
253 | * 1 if ptr1 is greater than ptr2 |
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
254 | */ |
985
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
255 | cx_attr_nonnull |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
256 | cx_attr_nodiscard |
68754c7de906
major refactoring of attributes
Mike Becker <universe@uap-core.de>
parents:
890
diff
changeset
|
257 | int cx_cmp_ptr(const void *ptr1, const void *ptr2); |
762
4523f6d42512
add cx_cmp_ptr() - fix #340
Mike Becker <universe@uap-core.de>
parents:
759
diff
changeset
|
258 | |
601 | 259 | #ifdef __cplusplus |
260 | } // extern "C" | |
261 | #endif | |
262 | ||
263 | #endif //UCX_COMPARE_H |