diff --git a/regcomp.c b/regcomp.c index 0b29b27034..aaf5dc9991 100644 --- a/regcomp.c +++ b/regcomp.c @@ -5729,7 +5729,7 @@ onig_reg_copy(regex_t** nreg, regex_t* oreg) goto err_repeat_range; } if (IS_NOT_NULL(reg->name_table)) { - if (IS_NULL(reg->name_table = st_copy(reg->name_table))) + if (onig_names_copy(reg, oreg)) goto err_name_table; } if (IS_NOT_NULL(reg->chain)) { @@ -5740,7 +5740,7 @@ onig_reg_copy(regex_t** nreg, regex_t* oreg) # undef COPY_FAILED err_chain: - onig_st_free_table(reg->name_table); + onig_names_free(reg); err_name_table: xfree(reg->repeat_range); err_repeat_range: diff --git a/regparse.c b/regparse.c index a8a7a0975a..33480a66b8 100644 --- a/regparse.c +++ b/regparse.c @@ -251,7 +251,7 @@ bitset_copy(BitSetRef dest, BitSetRef bs) #if defined(USE_NAMED_GROUP) && !defined(USE_ST_LIBRARY) extern int -onig_strncmp(const UChar* s1, const UChar* s2, int n) +onig_strncmp(const UChar* s1, const UChar* s2, size_t n) { int x; @@ -551,6 +551,17 @@ onig_names_free(regex_t* reg) return 0; } +extern int +onig_names_copy(regex_t* reg, regex_t* oreg) +{ + NameTable* table = oreg->name_table; + if (table) { + NameTable* t = st_copy(table); + reg->name_table = t; + } + return 0; +} + static NameEntry* name_find(regex_t* reg, const UChar* name, const UChar* name_end) { @@ -736,10 +747,52 @@ onig_names_free(regex_t* reg) return 0; } +extern int +onig_names_copy(regex_t* reg, regex_t* oreg) +{ + NameTable* ot = oreg->name_table; + if (ot) { + OnigEncoding enc = oreg->enc; + int i, num = ot->num; + NameTable* t = xmalloc(sizeof(*t)); + CHECK_NULL_RETURN_MEMERR(t); + *t = *ot; + t->e = xmalloc(t->alloc * sizeof(t->e[0])); + if (IS_NULL(t->e)) { + xfree(t); + return ONIGERR_MEMORY; + } + t->num = 0; + reg->name_table = t; + for (i = 0; i < num; t->num = ++i) { + NameEntry* oe = &(ot->e[i]); + NameEntry* e = &(t->e[i]); + *e = *oe; + e->name = NULL; + e->back_refs = NULL; + e->name = strdup_with_null(enc, oe->name, oe->name + e->name_len); + if (IS_NULL(e->name)) { + onig_names_free(reg); + return ONIGERR_MEMORY; + } + e->back_refs = xmalloc(e->back_alloc * sizeof(e->back_refs[0])); + if (IS_NULL(e->back_refs)) { + xfree(e->name); + onig_names_free(reg); + return ONIGERR_MEMORY; + } + memcpy(e->back_refs, oe->back_refs, e->back_num * sizeof(e->back_refs[0])); + e->back_ref1 = e->back_refs[0]; + } + } + return 0; +} + static NameEntry* name_find(regex_t* reg, const UChar* name, const UChar* name_end) { - int i, len; + int i; + size_t len; NameEntry* e; NameTable* t = (NameTable* )reg->name_table; @@ -774,6 +827,30 @@ onig_foreach_name(regex_t* reg, return 0; } +extern int +onig_renumber_name_table(regex_t* reg, GroupNumRemap* map) +{ + int i, j; + NameEntry* e; + NameTable* t = (NameTable* )reg->name_table; + + if (IS_NOT_NULL(t)) { + for (i = 0; i < t->num; i++) { + e = &(t->e[i]); + + if (e->back_num > 1) { + for (j = 0; j < e->back_num; j++) { + e->back_refs[j] = map[e->back_refs[j]].new_val; + } + } + else if (e->back_num == 1) { + e->back_ref1 = map[e->back_ref1].new_val; + } + } + } + return 0; +} + extern int onig_number_of_names(const regex_t* reg) { @@ -976,6 +1053,12 @@ onig_number_of_names(const regex_t* reg) { return 0; } + +extern int +onig_names_copy(regex_t* reg, regex_t* oreg) +{ + return 0; +} #endif /* else USE_NAMED_GROUP */ extern int diff --git a/regparse.h b/regparse.h index acdd3e2f5c..de980d0ac8 100644 --- a/regparse.h +++ b/regparse.h @@ -339,7 +339,7 @@ typedef struct { extern int onig_renumber_name_table(regex_t* reg, GroupNumRemap* map); #endif -extern int onig_strncmp(const UChar* s1, const UChar* s2, int n); +extern int onig_strncmp(const UChar* s1, const UChar* s2, size_t n); extern void onig_strcpy(UChar* dest, const UChar* src, const UChar* end); extern void onig_scan_env_set_error_string(ScanEnv* env, int ecode, UChar* arg, UChar* arg_end); extern int onig_scan_unsigned_number(UChar** src, const UChar* end, OnigEncoding enc); @@ -356,6 +356,7 @@ extern Node* onig_node_list_add(Node* list, Node* x); extern Node* onig_node_new_alt(Node* left, Node* right); extern void onig_node_str_clear(Node* node); extern int onig_names_free(regex_t* reg); +extern int onig_names_copy(regex_t* reg, regex_t* oreg); extern int onig_parse_make_tree(Node** root, const UChar* pattern, const UChar* end, regex_t* reg, ScanEnv* env); extern int onig_free_shared_cclass_table(void);