int RS_compile(Regis * r, int issuffix, const char *str) { int i, len = strlen(str); int state = RS_IN_WAIT; RegisNode *ptr = NULL; memset(r, 0, sizeof(Regis)); r->issuffix = (issuffix) ? 1 : 0; for (i = 0; i < len; i++) { unsigned char c = *(((unsigned char *) str) + i); if (state == RS_IN_WAIT) { if (isalpha(c)) { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); ptr->data[0] = c; ptr->type = RSF_ONEOF; ptr->len = 1; } else if (c == '[') { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); ptr->type = RSF_ONEOF; state = RS_IN_ONEOF; } else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else if (state == RS_IN_ONEOF) { if (c == '^') { ptr->type = RSF_NONEOF; state = RS_IN_NONEOF; } else if (isalpha(c)) { ptr->data[0] = c; ptr->len = 1; state = RS_IN_ONEOF_IN; } else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else if (state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF) { if (isalpha(c)) { ptr->data[ptr->len] = c; ptr->len++; } else if (c == ']') state = RS_IN_WAIT; else ts_error(ERROR, "Error in regis: %s at pos %d\n", str, i + 1); } else ts_error(ERROR, "Internal error in RS_compile: %d\n", state); } ptr = r->node; while (ptr) { r->nchar++; ptr = ptr->next; } return 0; }
void RS_compile(Regis *r, bool issuffix, const char *str) { int len = strlen(str); int state = RS_IN_WAIT; const char *c = str; RegisNode *ptr = NULL; memset(r, 0, sizeof(Regis)); r->issuffix = (issuffix) ? 1 : 0; while (*c) { if (state == RS_IN_WAIT) { if (t_isalpha(c)) { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); COPYCHAR(ptr->data, c); ptr->type = RSF_ONEOF; ptr->len = pg_mblen(c); } else if (t_iseq(c, '[')) { if (ptr) ptr = newRegisNode(ptr, len); else ptr = r->node = newRegisNode(NULL, len); ptr->type = RSF_ONEOF; state = RS_IN_ONEOF; } else /* shouldn't get here */ elog(ERROR, "invalid regis pattern: \"%s\"", str); } else if (state == RS_IN_ONEOF) { if (t_iseq(c, '^')) { ptr->type = RSF_NONEOF; state = RS_IN_NONEOF; } else if (t_isalpha(c)) { COPYCHAR(ptr->data, c); ptr->len = pg_mblen(c); state = RS_IN_ONEOF_IN; } else /* shouldn't get here */ elog(ERROR, "invalid regis pattern: \"%s\"", str); } else if (state == RS_IN_ONEOF_IN || state == RS_IN_NONEOF) { if (t_isalpha(c)) { COPYCHAR(ptr->data + ptr->len, c); ptr->len += pg_mblen(c); } else if (t_iseq(c, ']')) state = RS_IN_WAIT; else /* shouldn't get here */ elog(ERROR, "invalid regis pattern: \"%s\"", str); } else elog(ERROR, "internal error in RS_compile: state %d", state); c += pg_mblen(c); } if (state != RS_IN_WAIT) /* shouldn't get here */ elog(ERROR, "invalid regis pattern: \"%s\"", str); ptr = r->node; while (ptr) { r->nchar++; ptr = ptr->next; } }