-
Notifications
You must be signed in to change notification settings - Fork 1
/
range.c
59 lines (50 loc) · 1.18 KB
/
range.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* range.c
*
* Let's implementing the range struct.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "range.h"
/* range
*
* Definition of range struct. Consists of an array and an int which
* represents the size of the array. Eventually maybe add pointers to
* more arrays which could represent recursive group captures.
*/
struct _range {
group_t* groups;
int size;
};
group_t* range_group(range_t* ra, int index) {
assert(ra);
if (index < 0 || index >= ra->size)
return NULL;
return ra->groups + index;
}
int range_size(range_t* ra) {
assert(ra);
return ra->size;
}
range_t* range_copy(range_t* that) {
assert(that);
range_t* ra = range_new(that->size);
memcpy(ra->groups, that->groups, that->size * sizeof(group_t));
return ra;
}
range_t* range_new(int size) {
assert(size >= 1);
range_t* new = malloc(sizeof(range_t));
assert(new);
new->groups = calloc(size, sizeof(group_t));
assert(new->groups);
new->size = size;
return new;
}
void range_free(range_t* range) {
if (range) {
free(range->groups);
free(range);
}
}
/********************************************************************/