-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathosc_match.c
393 lines (373 loc) · 9.6 KB
/
osc_match.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Written by John MacCallum, The Center for New Music and Audio Technologies,
University of California, Berkeley. Copyright (c) 2009, The Regents of
the University of California (Regents).
Permission to use, copy, modify, distribute, and distribute modified versions
of this software and its documentation without fee and without a signed
licensing agreement, is hereby granted, provided that the above copyright
notice, this paragraph and the following two paragraphs appear in all copies,
modifications, and distributions.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "osc_match.h"
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
//#define OSC_MATCH_LOGSTATE
#define OSC_MATCH_RANGE_CAN_BE_NEGATIVE
static int osc_match_range(const char *pattern, const char *address);
#define OSC_MATCH_RANGE_NOMATCH 1
static const char const *_osc_match_errstr[] =
{
"no error",
"unmatched left square bracket",
"unmatched right square bracket",
"unmatched left curly brace",
"unmatched right curly brace",
"pattern does not begin with a slash",
"address does not begin with a slash",
"invalid character range",
"backtrack limit exceeded"
};
const char const *osc_match_errstr(unsigned long e)
{
e >>= 8;
if(e < sizeof(_osc_match_errstr) / sizeof(char *)){
return _osc_match_errstr[e];
}
return NULL;
}
#ifdef OSC_MATCH_LOGSTATE
#define OSC_MATCH_PRINTSTATE(p, a, po, ao) osc_match_printState(p, a, po, ao);
static void osc_match_printState(const char *pattern, const char *address, int po, int ao)
{
for(int i = 0; i < strlen(pattern); i++){
if(i == po){
printf("v ");
}else{
printf(" ");
}
}
printf("\n");
for(int i = 0; i < strlen(pattern); i++){
printf("%c ", pattern[i]);
}
printf("\n");
for(int i = 0; i < strlen(address); i++){
printf("%c ", address[i]);
}
printf("\n");
for(int i = 0; i < strlen(address); i++){
if(i == ao){
printf("^ ");
}else{
printf(" ");
}
}
printf("\n");
}
#define OSC_MATCH_PUSH(__p, __a){ \
sp++; \
if((sp - stack) >= OSC_MATCH_BACKTRACK_LIMIT){ \
return OSC_MATCH_ERROR_BACKTRACK_LIMIT_EXCEEDED; \
} \
sp->p = __p; \
sp->a = __a; \
printf("PUSH{%c(%d); %c(%d);}\n", pattern[__p], __p, address[__a], __a); \
}
#define OSC_MATCH_RETURN_SUCCESS(__r){ \
switch(__r){ \
case 1: \
printf("return: ADDRESS complete\n"); \
break; \
case 2: \
printf("return: PATTERN complete\n"); \
break; \
case 3: \
printf("return: BOTH complete\n"); \
break; \
default: \
break; \
} \
return __r; \
}
#define OSC_MATCH_RETURN_FAILURE(__r){ \
*pattern_offset = 0; \
*address_offset = 0; \
if(__r >= 0x100){ \
printf("return: ERROR: %s\n", osc_match_errstr(__r)); \
}else{ \
printf("return: NO MATCH (%d)\n", __LINE__); \
} \
return __r; \
}
#else
#define OSC_MATCH_PRINTSTATE(p, a, po, ao) ;
#define OSC_MATCH_PUSH(__p, __a){ \
sp++; \
if((sp - stack) >= OSC_MATCH_BACKTRACK_LIMIT){ \
return OSC_MATCH_ERROR_BACKTRACK_LIMIT_EXCEEDED; \
} \
sp->p = __p; \
sp->a = __a; \
}
#define OSC_MATCH_RETURN_SUCCESS(__r) return __r;
#define OSC_MATCH_RETURN_FAILURE(__r) *pattern_offset = *address_offset = 0; return __r;
#endif
#define OSC_MATCH_POP() sp--;
int osc_match(const char *pattern, const char *address, int *pattern_offset, int *address_offset)
{
if(*pattern != '/'){
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_PATTERN_NO_LEADING_SLASH);
}
if(*address != '/'){
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_ADDRESS_NO_LEADING_SLASH);
}
/*
if(!strcmp(address, pattern)){
OSC_MATCH_RETURN_SUCCESS((OSC_MATCH_PATTERN_COMPLETE | OSC_MATCH_ADDRESS_COMPLETE));
}
*/
*pattern_offset = 0;
*address_offset = 0;
struct state {int p,a;} stack[OSC_MATCH_BACKTRACK_LIMIT];
struct state *sp = stack;
sp->p = sp->a = 0;
while(1){
#ifdef OSC_MATCH_LOGSTATE
printf("stackptr: %ld: ", (long)(sp - stack));
#endif
if(sp < stack){
#ifdef OSC_MATCH_LOGSTATE
printf("fail\n");
#endif
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_NOMATCH);
}
#ifdef OSC_MATCH_LOGSTATE
printf("sp{%c(%d); %c(%d);}\n", pattern[sp->p], sp->p, address[sp->a], sp->a);
#endif
char p = pattern[sp->p];
char a = address[sp->a];
*pattern_offset = sp->p;
*address_offset = sp->a;
OSC_MATCH_PRINTSTATE(pattern, address, sp->p, sp->a);
if(a == '\0'){
while(pattern[sp->p] == '*'){
sp->p++;
}
*pattern_offset = sp->p;
p = pattern[sp->p];
if(p == '\0'){
OSC_MATCH_RETURN_SUCCESS((OSC_MATCH_PATTERN_COMPLETE | OSC_MATCH_ADDRESS_COMPLETE));
}else if(p == '/'){
OSC_MATCH_RETURN_SUCCESS(OSC_MATCH_ADDRESS_COMPLETE);
}else{
OSC_MATCH_POP();
continue;
}
}
if(a == '/'){
if(p == '/'){
int pp = sp->p;
int aa = sp->a;
sp = stack;
sp->p = pp + 1;
sp->a = aa + 1;
continue;
}else if(p == '\0'){
OSC_MATCH_RETURN_SUCCESS(OSC_MATCH_PATTERN_COMPLETE);
}else if(p == '*'){
while(pattern[sp->p] == '*'){
sp->p++;
}
continue;
}else{
OSC_MATCH_POP();
continue;
}
}
#ifdef OSC_MATCH_ALLOW_STAR_IN_ADDRESS
if(a == '*' && sp->a > 0 && address[sp->a - 1] == '/' && (address[sp->a + 1] == '/' || address[sp->a + 1] == '\0')){
while(address[sp->a] != '/' && address[sp->a] != '\0'){
sp->a++;
}
while(pattern[sp->p] != '/' && pattern[sp->p] != '\0'){
sp->p++;
}
continue;
}
#endif
switch(p){
case '/':
// we already checked to see if a is a '/' or a '\0', so just pop and continue;
OSC_MATCH_POP();
break;
case '\0':
// we know a is not a '/' or a '\0', so just pop and continue;
OSC_MATCH_POP();
break;
case '?':
sp->p++;
sp->a++;
break;
case '[':
{
int ret;
switch((ret = osc_match_range(pattern + sp->p, address + sp->a))){
case 0:
OSC_MATCH_POP();
break;
case 1:
sp->a++;
// if the first char inside the opening square bracket is
// a closing square bracket, it's treated as a normal char,
// so skip over it
if(pattern[sp->p + 1] == ']'){
sp->p += 2;
}
while(1){
if(pattern[sp->p] == ']'){
break;
}else if(pattern[sp->p] == '/' || pattern[sp->p] == '\0'){
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_UNMATCHED_LEFT_SQUARE_BRACKET);
}
sp->p++;
}
sp->p++;
break;
default:
OSC_MATCH_RETURN_FAILURE(ret);
}
}
break;
case '{':
{
int rest = sp->p;
while(pattern[rest] != '}'){
if(pattern[rest] == '/' || pattern[rest] == '\0'){
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_UNMATCHED_LEFT_CURLY_BRACE);
}
rest++;
}
rest++;
int cont = 0;
sp->p++;
int p1 = sp->p, p2 = sp->p;
int aa = sp->a;
OSC_MATCH_POP();
while(pattern[p2] != '/' && pattern[p2] != '\0'){
if(pattern[p2] == '/'){
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_UNMATCHED_LEFT_CURLY_BRACE);
}else if(pattern[p2] == ',' || pattern[p2] == '}'){
if(!strncmp(pattern + p1, address + aa, p2 - p1)){
cont = 1;
OSC_MATCH_PUSH(rest, aa + (p2 - p1));
}
p1 = p2 + 1;
}
if(pattern[p2] == '}'){
break;
}
p2++;
}
}
break;
case '}':
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_UNMATCHED_RIGHT_CURLY_BRACE);
case ']':
OSC_MATCH_RETURN_FAILURE(OSC_MATCH_ERROR_UNMATCHED_RIGHT_SQUARE_BRACKET);
case '*':
{
while(pattern[sp->p + 1] == '*'){
// avoid all kinds of extra backtracking with multiple stars
sp->p++;
}
if(pattern[sp->p + 1] == '\0' || pattern[sp->p + 1] == '/'){
sp->p++;
while(address[sp->a] != '/' && address[sp->a] != '\0'){
sp->a++;
}
}else{
int pp = sp->p + 1;
int aa = sp->a;
OSC_MATCH_POP();
int here = aa;
while(address[aa] != '/' && address[aa] != '\0'){
aa++;
}
aa--;
while(aa != here){
OSC_MATCH_PUSH(pp, aa);
aa--;
}
OSC_MATCH_PUSH(pp, aa);
}
}
break;
default:
if(p == a){
sp->p++;
sp->a++;
}else{
OSC_MATCH_POP();
}
break;
}
}
// unreachable
}
static inline int osc_match_range(const char *pattern, const char *address)
{
const char *p = pattern;
p++;
int val = 1;
if(*p == '!'){
p++;
val = 0;
}
int matched = !val;
// we're on the first character inside the square brackets
// if it's a - or a ], it gets treated as a normal character
if(*p == ']'){
if(*p == *address){
return val;
}
}
while(*p != ']'){
char c = *p++, c2;
if(c == '\0' || c == '/'){
return OSC_MATCH_ERROR_UNMATCHED_LEFT_SQUARE_BRACKET;
}
if(*p == '-'){
if(p[1] != '\0' && p[1] != '/' && p[1] != ']'){
p++;
c2 = *p++;
#ifdef OSC_MATCH_RANGE_CAN_BE_NEGATIVE
if((c <= *address && *address <= c2) || (c >= *address && *address >= c2)){
#else
if(c2 < c){
return OSC_MATCH_ERROR_INVALID_CHARACTER_RANGE;
}
if(c <= *address && *address <= c2){
#endif
matched = val;
break;
}
continue;
}
}
if(c == *address){
matched = val;
break;
}
}
return matched;
}