37 compiled_regex::compiled_regex () : regex (
NULL), pattern (
NULL)
40 compiled_regex::~compiled_regex ()
42 clear (regex, pattern);
48 std::string error_message;
49 using namespace std::regex_constants;
53 error_message.assign (
"regex_error(error_collate): the expression contains an invalid collating element name");
56 error_message.assign (
"regex_error(error_ctype): the expression contains an invalid character class name");
59 error_message.assign (
"regex_error(error_escape): the expression contains an invalid escaped character or a trailing escape");
62 error_message.assign (
"regex_error(error_backref): the expression contains an invalid back reference");
65 error_message.assign (
"regex_error(error_brack): the expression contains mismatched square brackets ('[' and ']')");
68 error_message.assign (
"regex_error(error_paren): the expression contains mismatched parentheses ('(' and ')')");
71 error_message.assign (
"regex_error(error_brace): the expression contains mismatched curly braces ('{' and '}')");
74 error_message.assign (
"regex_error(error_badbrace): the expression contains an invalid range in a {} expression");
77 error_message.assign (
"regex_error(error_range): the expression contains an invalid character range (e.g. [b-a])");
80 error_message.assign (
"regex_error(error_space): there was not enough memory to convert the expression into a finite state machine");
83 error_message.assign (
"regex_error(error_badrepeat): one of *?+{ was not preceded by a valid regular expression");
85 case error_complexity:
86 error_message.assign (
"regex_error(error_complexity): the complexity of an attempted match exceeded a predefined level");
89 error_message.assign (
"regex_error(error_stack): there was not enough memory to perform a match");
92 error_message.assign (
"regex_error(error_unknown)");
99 parse_match_type (std::regex_constants::syntax_option_type ®_flags, std::string &opt_str)
103 auto mt_iter = opt_str.begin ();
104 while ((mt_iter != opt_str.end ()) && (error_status ==
NO_ERROR))
110 reg_flags &= ~std::regex_constants::icase;
113 reg_flags |= std::regex_constants::icase;
126 clear (cub_regex_object *®ex,
char *&pattern)
141 const std::string &pattern,
142 const std::regex_constants::syntax_option_type reg_flags)
145 if (compiled_regex ==
NULL || reg_flags != compiled_regex->flags ())
151 if (compiled_pattern ==
NULL || pattern.size () !=
strlen (compiled_pattern)
152 || pattern.compare (compiled_pattern) != 0)
160 int compile (cub_regex_object *&compiled_regex,
const char *pattern,
161 const std::regex_constants::syntax_option_type reg_flags,
const LANG_COLLATION *collation)
165 std::wstring pattern_wstring;
179 wchar_t *collate_elem_pattern = L
"[[.";
180 int found = pattern_wstring.find ( std::wstring (collate_elem_pattern));
181 if (found != std::wstring::npos)
183 throw std::regex_error (std::regex_constants::error_collate);
188 if (compiled_regex !=
NULL)
190 delete compiled_regex;
193 compiled_regex =
new cub_regex_object ();
194 if (compiled_regex ==
NULL)
201 compiled_regex->imbue (loc);
202 compiled_regex->assign (pattern_wstring, reg_flags);
205 catch (std::regex_error &e)
216 int search (
int &result,
const cub_regex_object ®,
const std::string &src,
const INTL_CODESET codeset)
219 bool is_matched =
false;
221 std::wstring src_wstring;
236 if (reg.flags() & std::regex_constants::icase)
238 std::wstring src_lower;
239 src_lower.resize (src_wstring.size ());
240 std::transform (src_wstring.begin(), src_wstring.end(), src_lower.begin(), ::towlower);
241 is_matched = std::regex_search (src_lower, reg);
245 is_matched = std::regex_search (src_wstring, reg);
248 is_matched = std::regex_search (src_wstring, reg);
251 catch (std::regex_error &e)
264 int count (
int &result,
const cub_regex_object ®,
const std::string &src,
const int position,
271 std::wstring src_wstring;
279 std::wstring target (
280 src_wstring.substr (position, src_wstring.size () - position)
288 std::wstring target_lower;
289 if (reg.flags() & std::regex_constants::icase)
291 target_lower.resize (target.size ());
292 std::transform (target.begin(), target.end(), target_lower.begin(), ::towlower);
296 target_lower = target;
304 auto reg_iter = cub_regex_iterator (target_lower.begin (), target_lower.end (), reg);
306 auto reg_iter = cub_regex_iterator (target.begin (), target.end (), reg);
308 auto reg_end = cub_regex_iterator ();
309 count = std::distance (reg_iter, reg_end);
311 catch (std::regex_error &e)
324 int instr (
int &result,
const cub_regex_object ®,
const std::string &src,
325 const int position,
const int occurrence,
const int return_opt,
const INTL_CODESET codeset)
332 std::wstring src_wstring;
340 std::wstring target (
341 src_wstring.substr (position, src_wstring.size () - position)
349 std::wstring target_lower;
350 if (reg.flags() & std::regex_constants::icase)
352 target_lower.resize (target.size ());
353 std::transform (target.begin(), target.end(), target_lower.begin(), ::towlower);
357 target_lower = target;
365 auto reg_iter = cub_regex_iterator (target_lower.begin (), target_lower.end (), reg);
367 auto reg_iter = cub_regex_iterator (target.begin (), target.end (), reg);
369 auto reg_end = cub_regex_iterator ();
372 while (reg_iter != reg_end)
377 cub_regex_results match_result = *reg_iter;
378 match_idx = match_result.position ();
381 match_idx += reg_iter->length ();
391 result = position + match_idx + 1;
398 catch (std::regex_error &e)
415 int replace (std::string &result,
const cub_regex_object ®,
const std::string &src,
416 const std::string &repl,
const int position,
424 std::wstring src_wstring;
431 std::wstring repl_wstring;
439 std::wstring result_wstring (src_wstring.substr (0, position));
440 std::wstring target (
441 src_wstring.substr (position, src_wstring.size () - position)
444 std::wstring target_lowercase;
445 if (reg.flags() & std::regex_constants::icase)
447 target_lowercase.resize (target.size ());
448 std::transform (target.begin(), target.end(), target_lowercase.begin(), ::towlower);
452 target_lowercase = target;
457 auto reg_iter = cub_regex_iterator (target_lowercase.begin (), target_lowercase.end (), reg);
458 auto reg_end = cub_regex_iterator ();
464 auto out = std::back_inserter (result_wstring);
466 cub_regex_results match_result;
467 while (reg_iter != reg_end)
469 match_result = *reg_iter;
472 match_pos = match_result.position ();
473 match_length = match_result.length ();
474 std::wstring match_prefix = target.substr (last_pos, match_pos - last_pos);
475 out = std::copy (match_prefix.begin (), match_prefix.end (), out);
478 if (n == occurrence || occurrence == 0)
480 out = match_result.format (out, repl_wstring);
484 std::wstring match_str = target.substr (match_pos, match_length);
485 out = std::copy (match_str.begin (), match_str.end (), out);
491 last_pos = match_pos + match_length;
492 if (((occurrence != 0) && (n == occurrence)) || reg_iter == reg_end)
494 std::wstring match_suffix = target.substr (match_pos + match_length, std::string::npos);
495 out = std::copy (match_suffix.begin (), match_suffix.end (), out);
496 if (occurrence != 0 && n == occurrence)
505 if (match_pos == -1 && reg_iter == reg_end)
507 out = std::copy (target.begin (), target.end (), out);
516 catch (std::regex_error &e)
528 int replace (std::string &result,
const cub_regex_object ®,
const std::string &src,
529 const std::string &repl,
const int position,
537 std::wstring src_wstring;
544 std::wstring repl_wstring;
552 std::wstring result_wstring (src_wstring.substr (0, position));
553 std::wstring target (
554 src_wstring.substr (position, src_wstring.size () - position)
558 size_t match_length = 0;
563 result_wstring.append (
564 std::regex_replace (target, reg, repl_wstring)
569 auto reg_iter = cub_regex_iterator (target.begin (), target.end (), reg);
570 auto reg_end = cub_regex_iterator ();
573 auto out = std::back_inserter (result_wstring);
575 while (reg_iter != reg_end)
577 const cub_regex_results match_result = *reg_iter;
579 std::wstring match_prefix = match_result.prefix ().str ();
580 out = std::copy (match_prefix.begin (), match_prefix.end (), out);
583 match_pos = match_result.position ();
584 match_length = match_result.length ();
587 out = match_result.format (out, repl_wstring);
591 std::wstring match_str = match_result.str ();
592 out = std::copy (match_str.begin (), match_str.end (), out);
598 if (n == occurrence || reg_iter == reg_end)
601 std::wstring match_suffix = match_result.suffix (). str ();
602 out = std::copy (match_suffix.begin (), match_suffix.end (), out);
609 if (match_pos == -1 && reg_iter == reg_end)
611 out = std::copy (target.begin (), target.end (), out);
622 catch (std::regex_error &e)
635 int substr (std::string &result,
bool &is_matched,
const cub_regex_object ®,
const std::string &src,
636 const int position,
const int occurrence,
const INTL_CODESET codeset)
644 std::wstring src_wstring;
652 std::wstring result_wstring;
653 std::wstring target (
654 src_wstring.substr (position, src_wstring.size () - position)
662 std::wstring target_lower;
663 if (reg.flags() & std::regex_constants::icase)
665 target_lower.resize (target.size ());
666 std::transform (target.begin(), target.end(), target_lower.begin(), ::towlower);
670 target_lower = target;
675 size_t match_length = 0;
679 auto reg_iter = cub_regex_iterator (target_lower.begin (), target_lower.end (), reg);
681 auto reg_iter = cub_regex_iterator (target.begin (), target.end (), reg);
683 auto reg_end = cub_regex_iterator ();
684 auto out = std::back_inserter (result_wstring);
687 while (reg_iter != reg_end)
689 cub_regex_results match_result = *reg_iter;
692 match_pos = match_result.position ();
693 match_length = match_result.length ();
696 std::wstring match_str = target.substr (match_pos, match_length);
697 out = std::copy (match_str.begin (), match_str.end (), out);
712 catch (std::regex_error &e)
#define ER_REGEX_COMPILE_ERROR
std::string get_lang_name(const LANG_COLLATION *lang_coll)
#define ER_QSTR_BAD_SRC_CODESET
int instr(int &result, const cub_regex_object ®, const std::string &src, const int position, const int occurrence, const int return_opt, const INTL_CODESET codeset)
int compile(cub_regex_object *&compiled_regex, const char *pattern, const std::regex_constants::syntax_option_type reg_flags, const LANG_COLLATION *collation)
void er_set(int severity, const char *file_name, const int line_no, int err_id, int num_args,...)
#define ER_QPROC_INVALID_PARAMETER
std::locale get_locale(const std::string &charset, const std::string &lang)
#define ER_OUT_OF_VIRTUAL_MEMORY
int substr(std::string &result, bool &is_matched, const cub_regex_object ®, const std::string &src, const int position, const int occurrence, const INTL_CODESET codeset)
#define db_private_free_and_init(thrd, ptr)
int count(int &result, const cub_regex_object ®, const std::string &src, const int position, const INTL_CODESET codeset)
int parse_match_type(std::regex_constants::syntax_option_type ®_flags, std::string &opt_str)
bool convert_to_string(std::string &out, const std::wstring &in, const INTL_CODESET codeset)
#define ER_REGEX_EXEC_ERROR
int replace(std::string &result, const cub_regex_object ®, const std::string &src, const std::string &repl, const int position, const int occurrence, const INTL_CODESET codeset)
enum intl_codeset INTL_CODESET
std::string parse_regex_exception(std::regex_error &e)
bool check_should_recompile(const cub_regex_object *compiled_regex, const char *compiled_pattern, const std::string &pattern, const std::regex_constants::syntax_option_type reg_flags)
bool convert_to_wstring(std::wstring &out, const std::string &in, const INTL_CODESET codeset)
int search(int &result, const cub_regex_object ®, const std::string &src, const INTL_CODESET codeset)
void clear(cub_regex_object *®ex, char *&pattern)