Lely core libraries 2.3.4
cmp.h
Go to the documentation of this file.
1
22#ifndef LELY_UTIL_CMP_H_
23#define LELY_UTIL_CMP_H_
24
25#include <lely/libc/strings.h>
26#include <lely/libc/uchar.h>
27#include <lely/util/util.h>
28
29#include <stddef.h>
30#include <stdint.h>
31#include <string.h>
32#include <wchar.h>
33
34#ifndef LELY_UTIL_CMP_INLINE
35#define LELY_UTIL_CMP_INLINE static inline
36#endif
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
50typedef int cmp_t(const void *p1, const void *p2);
51
52#define LELY_UTIL_DEFINE_TYPE(name, type) \
53 LELY_UTIL_CMP_INLINE int name##_cmp(const void *p1, const void *p2);
54#include <lely/util/def/type.def>
55LELY_UTIL_DEFINE_TYPE(ptr, )
56LELY_UTIL_DEFINE_TYPE(str, )
57LELY_UTIL_DEFINE_TYPE(str_case, )
58#undef LELY_UTIL_DEFINE_TYPE
59
60#define LELY_UTIL_DEFINE_TYPE(name, type) \
61 inline int name##_cmp(const void *p1, const void *p2) \
62 { \
63 if (p1 == p2) \
64 return 0; \
65\
66 if (!p1) \
67 return -1; \
68 if (!p2) \
69 return 1; \
70\
71 type v1 = *(const type *)p1; \
72 type v2 = *(const type *)p2; \
73 return (v2 < v1) - (v1 < v2); \
74 }
75#include <lely/util/def/type.def>
76#undef LELY_UTIL_DEFINE_TYPE
77
78LELY_UTIL_CMP_INLINE int
79ptr_cmp(const void *p1, const void *p2)
80{
81 uintptr_t v1 = (uintptr_t)p1;
82 uintptr_t v2 = (uintptr_t)p2;
83 return (v2 < v1) - (v1 < v2);
84}
85
86LELY_UTIL_CMP_INLINE int
87str_cmp(const void *p1, const void *p2)
88{
89 if (p1 == p2)
90 return 0;
91
92 if (!p1)
93 return -1;
94 if (!p2)
95 return 1;
96
97 return strcmp((const char *)p1, (const char *)p2);
98}
99
100LELY_UTIL_CMP_INLINE int
101str_case_cmp(const void *p1, const void *p2)
102{
103 if (p1 == p2)
104 return 0;
105
106 if (!p1)
107 return -1;
108 if (!p2)
109 return 1;
110
111 return strcasecmp((const char *)p1, (const char *)p2);
112}
113
114#ifdef __cplusplus
115}
116#endif
117
118#endif // !LELY_UTIL_CMP_H_
int cmp_t(const void *p1, const void *p2)
The type of a generic comparison function.
Definition cmp.h:50
This is the public header file of the utilities library.
This header file is part of the C11 and POSIX compatibility library; it includes <stddef....
This header file is part of the C11 and POSIX compatibility library; it includes <stdint....
This header file is part of the C11 and POSIX compatibility library; it includes <string....
This header file is part of the C11 and POSIX compatibility library; it includes <strings....
int strcasecmp(const char *s1, const char *s2)
Compares the string at s1 to the string at s2, ignoring differences in case.
Definition strings.c:72
This header file is part of the C11 and POSIX compatibility library; it includes <uchar....