File size: 10,996 Bytes
d6df5f0 | 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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | /*
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* snapkitty-regex
* Copyright (C) 2026 SnapKitty Collective
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* snapkitty-regex core: Thompson NFA construction + parallel simulation.
*
* Follows Russ Cox's "Regular Expression Matching Can Be Simple And Fast"
* (https://swtch.com/~rsc/regexp/regexp1.html).
*
* re2post : infix -> postfix (explicit '.' concatenation operator)
* post2nfa: postfix -> NFA via Thompson's construction
* match : parallel NFA simulation over the input
*
* Worst-case matching time is O(m*n) for pattern length m and text
* length n. There are no pathological inputs.
*/
#include "regex.h"
#include <stdlib.h>
#include <string.h>
enum {
Split = 256, /* epsilon choice state */
Match = 257 /* accepting state */
};
typedef struct State State;
typedef struct Frag Frag;
typedef struct Ptrlist Ptrlist;
typedef struct List List;
struct State {
int c; /* character, Split, or Match */
State *out;
State *out1;
int lastlist; /* dedup generation, see addstate */
};
struct Frag {
State *start;
Ptrlist *out; /* dangling arrows */
};
struct Ptrlist {
Ptrlist *next;
State **s; /* pointer to a State* field to patch */
};
struct List {
State **s;
int n;
};
/* Opaque compiled pattern. */
struct Regex {
State *start;
State *matchstate;
State **states; /* every allocated state, for teardown */
int nstates;
int listid; /* dedup generation counter */
};
/* Compile-time context: tracks allocations so failures unwind cleanly. */
typedef struct Compiler {
State **states;
int nstates;
int capstates;
} Compiler;
static State *
state(Compiler *cp, int c, State *out, State *out1)
{
State *s;
State **ns;
int ncap;
if (cp->nstates == cp->capstates) {
ncap = cp->capstates ? cp->capstates * 2 : 64;
ns = realloc(cp->states, (size_t)ncap * sizeof *ns);
if (ns == NULL)
return NULL;
cp->states = ns;
cp->capstates = ncap;
}
s = malloc(sizeof *s);
if (s == NULL)
return NULL;
s->c = c;
s->out = out;
s->out1 = out1;
s->lastlist = 0;
cp->states[cp->nstates++] = s;
return s;
}
static Ptrlist *
list1(State **outp)
{
Ptrlist *l = malloc(sizeof *l);
if (l == NULL)
return NULL;
l->next = NULL;
l->s = outp;
return l;
}
static Ptrlist *
append(Ptrlist *l1, Ptrlist *l2)
{
Ptrlist *oldl = l1;
if (l1 == NULL)
return l2;
while (l1->next)
l1 = l1->next;
l1->next = l2;
return oldl;
}
/* Connect every dangling arrow in l to s, freeing the list nodes. */
static void
patch(Ptrlist *l, State *s)
{
Ptrlist *next;
for (; l; l = next) {
next = l->next;
*l->s = s;
free(l);
}
}
static void
ptrlist_free(Ptrlist *l)
{
Ptrlist *next;
for (; l; l = next) {
next = l->next;
free(l);
}
}
static Frag
frag(State *start, Ptrlist *out)
{
Frag f;
f.start = start;
f.out = out;
return f;
}
/*
* Rewrite an infix pattern to postfix with '.' as the explicit
* concatenation operator. Backslash escapes the next character into a
* literal (emitted as '\' + char in the postfix stream).
* Returns a malloc'd string, or NULL on malformed pattern / OOM.
*/
static char *
re2post(const char *re)
{
size_t len = strlen(re);
char *buf = malloc(6 * len + 8);
char *dst;
int nalt, natom;
struct { int nalt; int natom; } *paren, *p;
if (buf == NULL)
return NULL;
paren = malloc((len + 1) * sizeof *paren);
if (paren == NULL) {
free(buf);
return NULL;
}
dst = buf;
p = paren;
nalt = 0;
natom = 0;
for (; *re; re++) {
switch (*re) {
case '(':
if (natom > 1) {
--natom;
*dst++ = '.';
}
p->nalt = nalt;
p->natom = natom;
p++;
nalt = 0;
natom = 0;
break;
case '|':
if (natom == 0)
goto fail;
while (--natom > 0)
*dst++ = '.';
nalt++;
break;
case ')':
if (p == paren || natom == 0)
goto fail;
while (--natom > 0)
*dst++ = '.';
for (; nalt > 0; nalt--)
*dst++ = '|';
--p;
nalt = p->nalt;
natom = p->natom;
natom++;
break;
case '*':
case '+':
case '?':
if (natom == 0)
goto fail;
*dst++ = *re;
break;
case '\\':
if (re[1] == '\0')
goto fail;
if (natom > 1) {
--natom;
*dst++ = '.';
}
*dst++ = '\\';
*dst++ = *++re;
natom++;
break;
default:
if (natom > 1) {
--natom;
*dst++ = '.';
}
/* '.' is the postfix concatenation operator: a literal
* dot must go through as an escaped literal. */
if (*re == '.')
*dst++ = '\\';
*dst++ = *re;
natom++;
break;
}
}
if (p != paren)
goto fail;
while (--natom > 0)
*dst++ = '.';
for (; nalt > 0; nalt--)
*dst++ = '|';
*dst = '\0';
free(paren);
return buf;
fail:
free(paren);
free(buf);
return NULL;
}
/*
* Thompson's construction: build an NFA fragment per postfix token on a
* fragment stack. Returns the start state, or NULL on failure.
*/
static State *
post2nfa(Compiler *cp, const char *postfix, State *matchstate)
{
Frag *stack, *stackp, e1, e2, e;
State *s;
Ptrlist *l;
size_t n = strlen(postfix);
const char *p;
#define push(f) (*stackp++ = (f))
#define pop() (*--stackp)
if (n == 0)
return NULL;
stack = malloc((n + 1) * sizeof *stack);
if (stack == NULL)
return NULL;
stackp = stack;
for (p = postfix; *p; p++) {
switch (*p) {
default: /* literal character */
s = state(cp, (unsigned char)*p, NULL, NULL);
if (s == NULL)
goto fail;
l = list1(&s->out);
if (l == NULL)
goto fail;
push(frag(s, l));
break;
case '\\': /* escaped literal */
if (p[1] == '\0')
goto fail;
s = state(cp, (unsigned char)*++p, NULL, NULL);
if (s == NULL)
goto fail;
l = list1(&s->out);
if (l == NULL)
goto fail;
push(frag(s, l));
break;
case '.': /* concatenation */
if (stackp - stack < 2)
goto fail;
e2 = pop();
e1 = pop();
patch(e1.out, e2.start);
push(frag(e1.start, e2.out));
break;
case '|': /* alternation */
if (stackp - stack < 2)
goto fail;
e2 = pop();
e1 = pop();
s = state(cp, Split, e1.start, e2.start);
if (s == NULL)
goto fail;
push(frag(s, append(e1.out, e2.out)));
break;
case '?': /* zero or one */
if (stackp - stack < 1)
goto fail;
e = pop();
s = state(cp, Split, e.start, NULL);
if (s == NULL)
goto fail;
l = list1(&s->out1);
if (l == NULL)
goto fail;
push(frag(s, append(e.out, l)));
break;
case '*': /* zero or more */
if (stackp - stack < 1)
goto fail;
e = pop();
s = state(cp, Split, e.start, NULL);
if (s == NULL)
goto fail;
patch(e.out, s);
l = list1(&s->out1);
if (l == NULL)
goto fail;
push(frag(s, l));
break;
case '+': /* one or more */
if (stackp - stack < 1)
goto fail;
e = pop();
s = state(cp, Split, e.start, NULL);
if (s == NULL)
goto fail;
patch(e.out, s);
l = list1(&s->out1);
if (l == NULL)
goto fail;
push(frag(e.start, l));
break;
}
}
if (stackp != stack + 1)
goto fail;
e = pop();
patch(e.out, matchstate);
free(stack);
return e.start;
fail:
while (stackp > stack) {
e = pop();
ptrlist_free(e.out);
}
free(stack);
return NULL;
#undef push
#undef pop
}
/*
* Add s to list l, following unlabeled (Split) arrows. The per-Regex
* listid generation counter dedups in O(1) without clearing the list.
*/
static void
addstate(Regex *re, List *l, State *s)
{
if (s == NULL || s->lastlist == re->listid)
return;
s->lastlist = re->listid;
if (s->c == Split) {
addstate(re, l, s->out);
addstate(re, l, s->out1);
return;
}
l->s[l->n++] = s;
}
static List *
startlist(Regex *re, State *s, List *l)
{
re->listid++;
l->n = 0;
addstate(re, l, s);
return l;
}
/* Advance the NFA past one input character. */
static void
step(Regex *re, List *clist, int c, List *nlist)
{
int i;
State *s;
re->listid++;
nlist->n = 0;
for (i = 0; i < clist->n; i++) {
s = clist->s[i];
if (s->c == c)
addstate(re, nlist, s->out);
}
}
static int
ismatch(Regex *re, List *l)
{
int i;
for (i = 0; i < l->n; i++)
if (l->s[i] == re->matchstate)
return 1;
return 0;
}
/* Parallel NFA simulation over the input. With prefix != 0, report a
* match as soon as the accept state is reachable (pattern matches a
* prefix of s); otherwise require it after the last character (full
* match). */
static int
pump(Regex *re, const char *s, int prefix)
{
State **buf1 = malloc((size_t)re->nstates * sizeof *buf1);
State **buf2 = malloc((size_t)re->nstates * sizeof *buf2);
List l1, l2, *clist, *nlist, *t;
int m;
if (buf1 == NULL || buf2 == NULL) {
free(buf1);
free(buf2);
return 0;
}
l1.s = buf1;
l1.n = 0;
l2.s = buf2;
l2.n = 0;
clist = startlist(re, re->start, &l1);
if (prefix && ismatch(re, clist)) {
m = 1;
goto done;
}
nlist = &l2;
for (; *s; s++) {
step(re, clist, (unsigned char)*s, nlist);
t = clist;
clist = nlist;
nlist = t;
if (prefix && ismatch(re, clist)) {
m = 1;
goto done;
}
}
m = ismatch(re, clist);
done:
free(buf1);
free(buf2);
return m;
}
static int
match(Regex *re, const char *s)
{
return pump(re, s, 0);
}
Regex *
re_compile(const char *pattern)
{
char *post;
Compiler cp = { NULL, 0, 0 };
Regex *re;
State *start;
int i;
if (pattern == NULL || *pattern == '\0')
return NULL;
post = re2post(pattern);
if (post == NULL)
return NULL;
re = calloc(1, sizeof *re);
if (re == NULL) {
free(post);
return NULL;
}
re->matchstate = state(&cp, Match, NULL, NULL);
start = re->matchstate ? post2nfa(&cp, post, re->matchstate) : NULL;
free(post);
if (start == NULL) {
for (i = 0; i < cp.nstates; i++)
free(cp.states[i]);
free(cp.states);
free(re);
return NULL;
}
re->start = start;
re->states = cp.states;
re->nstates = cp.nstates;
re->listid = 0;
return re;
}
int
re_fullmatch(Regex *re, const char *s)
{
if (re == NULL || s == NULL)
return 0;
return match(re, s);
}
int
re_match(Regex *re, const char *s)
{
if (re == NULL || s == NULL)
return 0;
return pump(re, s, 1);
}
int
re_search(Regex *re, const char *s)
{
const char *p;
if (re == NULL || s == NULL)
return 0;
for (p = s; ; p++) {
if (re_match(re, p))
return 1;
if (*p == '\0')
break;
}
return 0;
}
void
re_free(Regex *re)
{
int i;
if (re == NULL)
return;
for (i = 0; i < re->nstates; i++)
free(re->states[i]);
free(re->states);
free(re);
}
|