mongory 0.7.6 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +0 -14
  3. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +122 -0
  4. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +161 -0
  5. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +79 -0
  6. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +95 -0
  7. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +127 -0
  8. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +175 -0
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +76 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core.h +12 -0
  11. data/ext/mongory_ext/mongory-core/src/foundations/array.c +287 -0
  12. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +19 -0
  13. data/ext/mongory_ext/mongory-core/src/foundations/config.c +270 -0
  14. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +48 -0
  15. data/ext/mongory_ext/mongory-core/src/foundations/error.c +38 -0
  16. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +298 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +65 -0
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +49 -0
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +498 -0
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +210 -0
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +70 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +500 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +164 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h +47 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +122 -0
  26. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +100 -0
  27. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +217 -0
  28. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +83 -0
  29. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +573 -0
  30. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +125 -0
  31. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +147 -0
  32. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h +48 -0
  33. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +124 -0
  34. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +46 -0
  35. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +126 -0
  36. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +46 -0
  37. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +314 -0
  38. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h +97 -0
  39. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +252 -0
  40. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +79 -0
  41. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +23 -0
  42. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +60 -0
  43. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +23 -0
  44. data/lib/mongory/version.rb +1 -1
  45. metadata +43 -2
@@ -0,0 +1,210 @@
1
+ #ifndef MONGORY_UTILS_C
2
+ #define MONGORY_UTILS_C
3
+
4
+ #include "utils.h"
5
+ #include <errno.h>
6
+ #include <limits.h>
7
+ #include <stdlib.h>
8
+ #include <stdbool.h>
9
+ #include <stdarg.h>
10
+ #include <string.h>
11
+ #include <stdio.h>
12
+ #include <math.h>
13
+
14
+ /**
15
+ * @brief Attempts to parse a string `key` into an integer `out`.
16
+ *
17
+ * Uses `strtol` for conversion and checks for common parsing errors:
18
+ * - Input string is NULL or empty.
19
+ * - The string contains non-numeric characters after the number.
20
+ * - The parsed number is out of the range of `int` (`INT_MIN`, `INT_MAX`).
21
+ *
22
+ * @param key The null-terminated string to parse.
23
+ * @param out Pointer to an integer where the result is stored.
24
+ * @return `true` if parsing is successful and the value fits in an `int`.
25
+ * `false` otherwise. `errno` may be set by `strtol`.
26
+ */
27
+ bool mongory_try_parse_int(const char *key, int *out) {
28
+ if (key == NULL || *key == '\0') {
29
+ return false; // Invalid input string.
30
+ }
31
+ if (out == NULL) {
32
+ return false; // Output pointer must be valid.
33
+ }
34
+
35
+ char *endptr = NULL;
36
+ errno = 0; // Clear errno before calling strtol.
37
+ long val = strtol(key, &endptr, 10); // Base 10 conversion.
38
+
39
+ // Check for parsing errors reported by strtol.
40
+ if (endptr == key) {
41
+ return false; // No digits were found.
42
+ }
43
+ if (*endptr != '\0') {
44
+ return false; // Additional characters after the number.
45
+ }
46
+ if (errno == ERANGE || val < INT_MIN || val > INT_MAX) {
47
+ // Value out of range for int. ERANGE is set by strtol for overflow/underflow.
48
+ return false;
49
+ }
50
+
51
+ *out = (int)val; // Successfully parsed and within int range.
52
+ return true;
53
+ }
54
+
55
+ /**
56
+ * @brief Creates a copy of a string using the specified memory pool.
57
+ * @param pool The memory pool to use for allocation.
58
+ * @param str The null-terminated string to copy.
59
+ * @return A pointer to the newly allocated copy, or NULL if str is NULL or
60
+ * allocation fails.
61
+ */
62
+ char *mongory_string_cpy(mongory_memory_pool *pool, char *str) {
63
+ if (str == NULL) {
64
+ return NULL;
65
+ }
66
+
67
+ size_t len = strlen(str);
68
+ char *new_str = (char *)MG_ALLOC(pool, len + 1); // +1 for null terminator.
69
+ if (new_str == NULL) {
70
+ pool->error = &MONGORY_ALLOC_ERROR;
71
+ return NULL;
72
+ }
73
+
74
+ strcpy(new_str, str);
75
+ return new_str;
76
+ }
77
+
78
+ char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...) {
79
+ va_list args;
80
+ va_start(args, format);
81
+ int len = vsnprintf(NULL, 0, format, args);
82
+ va_end(args);
83
+ char *new_str = (char *)MG_ALLOC(pool, len + 1);
84
+ if (new_str == NULL) {
85
+ pool->error = &MONGORY_ALLOC_ERROR;
86
+ return NULL;
87
+ }
88
+ va_start(args, format);
89
+ vsnprintf(new_str, len + 1, format, args);
90
+ va_end(args);
91
+ new_str[len] = '\0';
92
+ return new_str;
93
+ }
94
+
95
+ double mongory_log(double x, double base) {
96
+ return log(x) / log(base);
97
+ }
98
+
99
+ bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line) {
100
+ if (pool->error != NULL) {
101
+ return false;
102
+ }
103
+ if (ptr == NULL) {
104
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
105
+ error->type = MONGORY_ERROR_INVALID_ARGUMENT;
106
+ error->message = mongory_string_cpyf(pool, "Null pointer: %s (at %s:%d)", name, file, line);
107
+ pool->error = error;
108
+ return false;
109
+ }
110
+ return true;
111
+ }
112
+
113
+ static char *error_format =
114
+ "[Mongory Core Error]\n"
115
+ "%s needs %s, got %s\n"
116
+ "(%s:%d)\n";
117
+
118
+ bool mongory_validate_table(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
119
+ if (pool->error != NULL) {
120
+ return false;
121
+ }
122
+ if (!mongory_validate_ptr(pool, name, value, file, line)) {
123
+ return false;
124
+ }
125
+ if (value->type != MONGORY_TYPE_TABLE) {
126
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
127
+ error->type = MONGORY_ERROR_INVALID_ARGUMENT;
128
+ error->message = mongory_string_cpyf(pool, error_format,
129
+ name,
130
+ "Table",
131
+ mongory_type_to_string(value),
132
+ file,
133
+ line
134
+ );
135
+ pool->error = error;
136
+ return false;
137
+ }
138
+ return true;
139
+ }
140
+
141
+ bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
142
+ if (pool->error != NULL) {
143
+ return false;
144
+ }
145
+ if (!mongory_validate_ptr(pool, name, value, file, line)) {
146
+ return false;
147
+ }
148
+ if (value->type != MONGORY_TYPE_ARRAY) {
149
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
150
+ error->type = MONGORY_ERROR_INVALID_ARGUMENT;
151
+ error->message = mongory_string_cpyf(pool, error_format,
152
+ name,
153
+ "Array",
154
+ mongory_type_to_string(value),
155
+ file,
156
+ line
157
+ );
158
+ pool->error = error;
159
+ return false;
160
+ }
161
+ return true;
162
+ }
163
+
164
+ bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
165
+ if (pool->error != NULL) {
166
+ return false;
167
+ }
168
+ if (!mongory_validate_ptr(pool, name, value, file, line)) {
169
+ return false;
170
+ }
171
+ if (value->type != MONGORY_TYPE_STRING) {
172
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
173
+ error->type = MONGORY_ERROR_INVALID_ARGUMENT;
174
+ error->message = mongory_string_cpyf(pool, error_format,
175
+ name,
176
+ "String",
177
+ mongory_type_to_string(value),
178
+ file,
179
+ line
180
+ );
181
+ pool->error = error;
182
+ return false;
183
+ }
184
+ return true;
185
+ }
186
+
187
+ bool mongory_validate_number(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
188
+ if (pool->error != NULL) {
189
+ return false;
190
+ }
191
+ if (!mongory_validate_ptr(pool, name, value, file, line)) {
192
+ return false;
193
+ }
194
+ if (value->type != MONGORY_TYPE_INT && value->type != MONGORY_TYPE_DOUBLE) {
195
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
196
+ error->type = MONGORY_ERROR_INVALID_ARGUMENT;
197
+ error->message = mongory_string_cpyf(pool, error_format,
198
+ name,
199
+ "Number",
200
+ mongory_type_to_string(value),
201
+ file,
202
+ line
203
+ );
204
+ pool->error = error;
205
+ return false;
206
+ }
207
+ return true;
208
+ }
209
+
210
+ #endif // MONGORY_UTILS_C
@@ -0,0 +1,70 @@
1
+ #ifndef MONGORY_UTILS_H
2
+ #define MONGORY_UTILS_H
3
+
4
+ #include "mongory-core/foundations/memory_pool.h"
5
+ #include "mongory-core/foundations/value.h"
6
+ #include <stdbool.h>
7
+
8
+ /**
9
+ * @brief Attempts to parse an integer from a string.
10
+ *
11
+ * This utility function is useful for matchers that might operate on array
12
+ * indices or numeric string fields. It checks for valid integer formats and
13
+ * range (`INT_MIN`, `INT_MAX`).
14
+ *
15
+ * @param key The null-terminated string to parse. Must not be NULL or empty.
16
+ * @param out A pointer to an integer where the parsed value will be stored if
17
+ * successful. Must not be NULL.
18
+ * @return bool True if the string was successfully parsed as an integer and is
19
+ * within `int` range, false otherwise. `errno` might be set by `strtol` on
20
+ * failure (e.g. `ERANGE`).
21
+ */
22
+ bool mongory_try_parse_int(const char *key, int *out);
23
+
24
+ /**
25
+ * @brief Copies a string using the Mongory library's memory pool.
26
+ *
27
+ * Allocates memory from the given pool and copies the source string into it.
28
+ * The returned string is null-terminated.
29
+ *
30
+ * @param pool The memory pool to use for allocating the new string.
31
+ * @param str The source string to copy. If NULL, returns NULL.
32
+ * @return char* A pointer to the newly allocated and copied string, or NULL
33
+ * if allocation fails or str is NULL. The caller does not own this memory
34
+ * directly; it will be freed when the pool is freed.
35
+ */
36
+ char *mongory_string_cpy(mongory_memory_pool *pool, char *str);
37
+
38
+ /**
39
+ * @brief Copies a formatted string using the Mongory library's memory pool.
40
+ *
41
+ * Allocates memory from the given pool and copies the formatted string into it.
42
+ * The returned string is null-terminated.
43
+ *
44
+ * @param pool The memory pool to use for allocating the new string.
45
+ * @param format The format string to copy.
46
+ * @param ... The arguments to be formatted into the string.
47
+ * @return char* A pointer to the newly allocated and copied string, or NULL
48
+ * if allocation fails or format is NULL. The caller does not own this memory
49
+ * directly; it will be freed when the pool is freed.
50
+ */
51
+ char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...);
52
+
53
+ double mongory_log(double x, double base);
54
+
55
+ bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line);
56
+ #define MONGORY_VALIDATE_PTR(pool, ptr) mongory_validate_ptr(pool, #ptr, ptr, __FILE__, __LINE__)
57
+
58
+ bool mongory_validate_table(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
59
+ #define MONGORY_VALIDATE_TABLE(pool, value) mongory_validate_table(pool, #value, value, __FILE__, __LINE__)
60
+
61
+ bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
62
+ #define MONGORY_VALIDATE_ARRAY(pool, value) mongory_validate_array(pool, #value, value, __FILE__, __LINE__)
63
+
64
+ bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
65
+ #define MONGORY_VALIDATE_STRING(pool, value) mongory_validate_string(pool, #value, value, __FILE__, __LINE__)
66
+
67
+ bool mongory_validate_number(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line);
68
+ #define MONGORY_VALIDATE_NUMBER(pool, value) mongory_validate_number(pool, #value, value, __FILE__, __LINE__)
69
+
70
+ #endif // MONGORY_UTILS_H