-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharclass.c
More file actions
49 lines (39 loc) · 1.41 KB
/
Copy pathcharclass.c
File metadata and controls
49 lines (39 loc) · 1.41 KB
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
#include <stdlib.h>
#include <stdio.h>
#include "rxpriv.h"
/*
A character class can now be a chain of assertions to make on a
character. For example, this character class:
<punct + alpha - [a..fxyz] - [,]>
Means that a character must be punctuation, or alphabetical, but not in
the range "a" through "f" or characters "x", "y", "z", and not a comma.
In the code, it is represented backwards, because that is the order
matching occurs in. In the above example, if its a comma, you can
immediately tell the character is not in the class. If its not a comma,
you check if its in the range "a" through "f", if it is, its not in
the character class. Then if its an alphabetic, it is contained. If
punctiation, it is, otherwise it is not a part of the character class.
The above, once parsed, will be a flat array like this:
[CC_EXCLUDES, CC_CHAR, ',',
CC_EXCLUDES, CC_RANGE, 'a', 'f', CC_CHAR, 'x', CC_CHAR, 'y', CC_CHAR, 'z',
CC_INCLUDES, CC_FUNC, isalpha,
CC_INCLUDES, CC_FUNC, ispunct]
*/
CharClass *
char_class_new (Rx *rx, const char *str, int length) {
CharClass *cc = calloc(1, sizeof (CharClass));
cc->str = str;
cc->length = length;
rx->charclasses = list_push(rx->charclasses, cc);
return cc;
}
void
char_class_free (CharClass *cc) {
if (cc)
list_free(cc->actions, NULL);
free(cc);
}
void
char_class_print (CharClass *cc) {
printf("%.*s\n", cc->length, cc->str);
}