| 350 | |
| 351 | |
| 352 | static const char *matchbalance(MatchState *ms, const char *s, |
| 353 | const char *p) { |
| 354 | if (p >= ms->p_end - 1) |
| 355 | luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); |
| 356 | if (*s != *p) return nullptr; |
| 357 | else { |
| 358 | int b = *p; |
| 359 | int e = *(p + 1); |
| 360 | int cont = 1; |
| 361 | while (++s < ms->src_end) { |
| 362 | if (*s == e) { |
| 363 | if (--cont == 0) return s + 1; |
| 364 | } |
| 365 | else if (*s == b) cont++; |
| 366 | } |
| 367 | } |
| 368 | return nullptr; /* string ends out of balance */ |
| 369 | } |
| 370 | |
| 371 | |
| 372 | static const char *max_expand(MatchState *ms, const char *s, |