diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2024-08-28 15:46:49 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2024-08-28 15:46:49 +0200 |
commit | 6eeff72e87cad1adb5d0ce017a8a5e011d7a9be1 (patch) | |
tree | a6de4a825e3a7fe975e192b8468c9a67a37c9aa9 /modules/regex/regex.cpp | |
parent | d1c238f43cca5560a7a0b98a674bb1273de92c6a (diff) | |
parent | ebb5a5cc3d48e3beb59ba444068fe0e5cabefb9f (diff) | |
download | redot-engine-6eeff72e87cad1adb5d0ce017a8a5e011d7a9be1.tar.gz |
Merge pull request #95212 from Germenzi/regex-show-error
[RegEx] Add `show_error` parameter to control error printing on compilation fail
Diffstat (limited to 'modules/regex/regex.cpp')
-rw-r--r-- | modules/regex/regex.cpp | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index 9f34a6ca6a..85c0b9ecad 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -29,6 +29,7 @@ /**************************************************************************/ #include "regex.h" +#include "regex.compat.inc" #include "core/os/memory.h" @@ -161,10 +162,10 @@ void RegEx::_pattern_info(uint32_t what, void *where) const { pcre2_pattern_info_32((pcre2_code_32 *)code, what, where); } -Ref<RegEx> RegEx::create_from_string(const String &p_pattern) { +Ref<RegEx> RegEx::create_from_string(const String &p_pattern, bool p_show_error) { Ref<RegEx> ret; ret.instantiate(); - ret->compile(p_pattern); + ret->compile(p_pattern, p_show_error); return ret; } @@ -175,7 +176,7 @@ void RegEx::clear() { } } -Error RegEx::compile(const String &p_pattern) { +Error RegEx::compile(const String &p_pattern, bool p_show_error) { pattern = p_pattern; clear(); @@ -192,10 +193,12 @@ Error RegEx::compile(const String &p_pattern) { pcre2_compile_context_free_32(cctx); if (!code) { - PCRE2_UCHAR32 buf[256]; - pcre2_get_error_message_32(err, buf, 256); - String message = String::num(offset) + ": " + String((const char32_t *)buf); - ERR_PRINT(message.utf8()); + if (p_show_error) { + PCRE2_UCHAR32 buf[256]; + pcre2_get_error_message_32(err, buf, 256); + String message = String::num(offset) + ": " + String((const char32_t *)buf); + ERR_PRINT(message.utf8()); + } return FAILED; } return OK; @@ -395,10 +398,10 @@ RegEx::~RegEx() { } void RegEx::_bind_methods() { - ClassDB::bind_static_method("RegEx", D_METHOD("create_from_string", "pattern"), &RegEx::create_from_string); + ClassDB::bind_static_method("RegEx", D_METHOD("create_from_string", "pattern", "show_error"), &RegEx::create_from_string, DEFVAL(true)); ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear); - ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile); + ClassDB::bind_method(D_METHOD("compile", "pattern", "show_error"), &RegEx::compile, DEFVAL(true)); ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1)); ClassDB::bind_method(D_METHOD("search_all", "subject", "offset", "end"), &RegEx::search_all, DEFVAL(0), DEFVAL(-1)); ClassDB::bind_method(D_METHOD("sub", "subject", "replacement", "all", "offset", "end"), &RegEx::sub, DEFVAL(false), DEFVAL(0), DEFVAL(-1)); |