Skip to content

Commit

Permalink
Add unit test for rz_regex_get_match_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Feb 3, 2024
1 parent 11665f2 commit db694c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions librz/util/regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ RZ_API void rz_regex_error_msg(RzRegexStatus errcode, RZ_OUT char *errbuf, RzReg
pcre2_get_error_message(errcode, (PCRE2_UCHAR *)errbuf, errbuf_size);
}

RZ_API const ut8 *rz_regex_get_match_name(RZ_NONNULL const RzRegex *regex, ut32 name_idx) {
/**
* \brief Returns the name of a group.
*
* \param regex The regex expression with named groups.
* \param group_idx The index of the group to get the name for.
*
* \return The name of the group or NULL in case of failure or non is was set.
*/
RZ_API const ut8 *rz_regex_get_match_name(RZ_NONNULL const RzRegex *regex, ut32 group_idx) {
rz_return_val_if_fail(regex, NULL);

ut32 namecount;
Expand All @@ -151,7 +159,7 @@ RZ_API const ut8 *rz_regex_get_match_name(RZ_NONNULL const RzRegex *regex, ut32

for (size_t i = 0; i < namecount; i++) {
int n = (nametable_ptr[0] << 8) | nametable_ptr[1];
if (n == name_idx) {
if (n == group_idx) {
return nametable_ptr + 2;
}
nametable_ptr += name_entry_size;
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_regex.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,24 @@ bool test_rz_regex_capture(void) {
mu_end;
}

bool test_rz_regex_named_matches(void) {
RzRegex *reg = rz_regex_new("(?<proto>^\\w+)(:\\/\\/)(?<domain>\\w+)\\.(?<tdomain>\\w+)", RZ_REGEX_EXTENDED, 0);
mu_assert_notnull(reg, "Regex was NULL");
RzRegexMatch *match = NULL;
mu_assert_true(exec_regex(reg, "https://rizin.re", &match), "Regex match failed");
mu_assert_streq((char *)rz_regex_get_match_name(reg, 1), "proto", "proto was not matched.");
mu_assert_streq((char *)rz_regex_get_match_name(reg, 3), "domain", "domain was not matched.");
mu_assert_streq((char *)rz_regex_get_match_name(reg, 4), "tdomain", "tdomain was not matched.");

rz_regex_free(reg);
mu_end;
}

int main() {
mu_run_test(test_rz_regex_all_match);
mu_run_test(test_rz_regex_extend_space);
mu_run_test(test_rz_reg_exec);
mu_run_test(test_rz_regex_capture);
mu_run_test(test_rz_regex_all_to_str);
mu_run_test(test_rz_regex_named_matches);
}

0 comments on commit db694c6

Please sign in to comment.