tb_char_t const* tb_regex_replace_done(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_char_t const* replace_cstr, tb_size_t replace_size, tb_size_t* plength) { // clear length first if (plength) *plength = 0; // init regex tb_char_t* result = tb_null; tb_regex_ref_t regex = tb_regex_init(pattern, mode); if (regex) { // replace regex tb_size_t result_size = 0; tb_char_t const* result_cstr = tb_regex_replace(regex, cstr, size, start, replace_cstr, replace_size, &result_size); if (result_cstr && result_size) { // save result result = tb_strndup(result_cstr, result_size); if (result) { // save length if (plength) *plength = result_size; } } // exit regex tb_regex_exit(regex); } // ok? return result; }
tb_long_t tb_regex_match_done(tb_char_t const* pattern, tb_size_t mode, tb_char_t const* cstr, tb_size_t size, tb_size_t start, tb_size_t* plength, tb_vector_ref_t* presults) { // clear results first if (presults) *presults = tb_null; // init regex tb_long_t ok = -1; tb_regex_ref_t regex = tb_regex_init(pattern, mode); if (regex) { // init results tb_vector_ref_t results = tb_vector_init(16, tb_element_mem(sizeof(tb_regex_match_t), tb_regex_match_exit, tb_null)); if (results) { // match regex ok = tb_regex_match(regex, cstr, size, start, plength, &results); // ok? if (ok >= 0) { // save results if (presults) { *presults = results; results = tb_null; } } // exit results if (results) tb_vector_exit(results); results = tb_null; } // exit regex tb_regex_exit(regex); } // ok? return ok; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_regex_ref_t tb_regex_init(tb_char_t const* pattern, tb_size_t mode) { // check tb_assert_and_check_return_val(pattern, tb_null); // done tb_bool_t ok = tb_false; tb_regex_t* regex = tb_null; do { // make regex regex = (tb_regex_t*)tb_malloc0_type(tb_regex_t); tb_assert_and_check_break(regex); // init options tb_uint32_t options = PCRE2_UTF; if (mode & TB_REGEX_MODE_CASELESS) options |= PCRE2_CASELESS; if (mode & TB_REGEX_MODE_MULTILINE) options |= PCRE2_MULTILINE; #ifndef __tb_debug__ options |= PCRE2_NO_UTF_CHECK; #endif // init code tb_int_t errornumber; PCRE2_SIZE erroroffset; regex->code = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED, options, &errornumber, &erroroffset, tb_null); if (!regex->code) { #if defined(__tb_debug__) && !defined(TB_CONFIG_OS_WINDOWS) // FIXME: _sprintf undefined link error for vs2015 on windows // get error info PCRE2_UCHAR info[256]; pcre2_get_error_message(errornumber, info, sizeof(info)); // trace tb_trace_d("compile failed at offset %ld: %s\n", (tb_long_t)erroroffset, info); #endif // end break; } // init match data regex->match_data = pcre2_match_data_create_from_pattern(regex->code, tb_null); tb_assert_and_check_break(regex->match_data); // save mode regex->mode = mode; // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (regex) tb_regex_exit((tb_regex_ref_t)regex); regex = tb_null; } // ok? return (tb_regex_ref_t)regex; }