-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharset.c
175 lines (122 loc) · 3.36 KB
/
charset.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdlib.h>
#include <string.h>
#include "misc.h"
#include "array.h"
#include "charset.h"
/* Initialize an empty charset. */
void cs_init(struct charset *cs) {
memset(cs, 0, sizeof(*cs));
ARRAY_ALLOC(cs->chars, 2);
}
/* Deinitialize a charset. */
void cs_fini(struct charset *cs) {
size_t i;
for (i = 0; i < cs->chars_size; i++)
free(cs->chars[i]);
ARRAY_FREE(cs->chars);
memset(cs, 0, sizeof(*cs));
}
/* Add a list of chars to the charset. */
void cs_add(struct charset *cs, const char *str) {
char *cpy;
size_t len;
len = strlen(str);
cpy = strdup(str);
if (cs->chars_size == 0)
cs->length = len;
if (len != cs->length)
custom_error("cs_add: "
"All the charsets must have the same length");
ARRAY_APPEND(cs->chars, cpy);
}
/*
* Set stridx and pos to be the string index and the position of a character
* into the charset.
* Return 1 if the character is found, 0 otherwise.
*/
int cs_find_char(const struct charset *cs, char c,
size_t *stridx, size_t *pos) {
size_t i;
for (i = 0; i < cs->chars_size; i++) {
char *p = strchr(cs->chars[i], c);
/* Yay, found it! */
if (p != NULL) {
if (stridx)
*stridx = i;
if (pos)
*pos = p - cs->chars[i];
return 1;
}
}
return 0;
}
/*
* Return the number of the character in the charset. Equivalent to ord() in
* most languages.
* Return -1 if the character is not found.
*/
int cs_ord(const struct charset *cs, char c) {
size_t pos;
if (!cs_find_char(cs, c, NULL, &pos))
return -1;
return pos;
}
/*
* Return one of the characters at the given position. Equivalent to chr() in
* most languages.
* Return '\0' if the position does not exists.
*/
char cs_chr(const struct charset *cs, size_t pos) {
if (pos >= cs->length || cs->chars_size == 0)
return '\0';
return cs->chars[0][pos];
}
/* Ask whether a character belong to the charset. Return 1 if yes, 0 if no. */
int cs_belong(const struct charset *cs, char c) {
return cs_find_char(cs, c, NULL, NULL);
}
/* Ask whether two characters are equivalents. */
int cs_equiv(const struct charset *cs, char c1, char c2) {
size_t c1_pos;
size_t c2_pos;
/* If you can't even find c1, it's not equivalent to c2! */
if (!cs_find_char(cs, c1, NULL, &c1_pos))
return 0;
/* If you can't even find c2, it's not equivalent to c1! */
if (!cs_find_char(cs, c2, NULL, &c2_pos))
return 0;
/* They're equivalent only if they are at the same position. */
return c1_pos == c2_pos;
}
/*
* Retrive the normalized version of a character. That may be any character
* give, it is guaranteed to be always the same. Return c if the character do
* not belong to the charset.
*/
char cs_norm(const struct charset *cs, char c) {
size_t pos;
if (!cs_find_char(cs, c, NULL, &pos))
return c;
return cs->chars[0][pos];
}
/*
* Equivalent to strpbrk with a struct charset as set of accepted characters.
* /!\ The arguments are inverted w.r.t strpbrk to keep consistent with the
* other functions of this module.
*/
char *cs_strpbrk(const struct charset *cs, const char *str) {
char *min_pos = NULL;
size_t i;
/*
* Basically, we search the first character that corresponds to one of
* the charset strings.
*/
for (i = 0; i < cs->chars_size; i++) {
char *pos = strpbrk(str, cs->chars[i]);
if (pos == NULL)
continue;
if (min_pos == NULL || pos < min_pos)
min_pos = pos;
}
return min_pos;
}