From ebb5a5cc3d48e3beb59ba444068fe0e5cabefb9f Mon Sep 17 00:00:00 2001 From: "Nikita\\Nick" Date: Wed, 28 Aug 2024 15:35:42 +0300 Subject: [PATCH] [RegEx] Add show_error parameter to control error printing on compilation fail --- .../4.3-stable.expected | 8 ++++ modules/regex/doc_classes/RegEx.xml | 6 ++- modules/regex/regex.compat.inc | 46 +++++++++++++++++++ modules/regex/regex.cpp | 21 +++++---- modules/regex/regex.h | 10 +++- 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 modules/regex/regex.compat.inc diff --git a/misc/extension_api_validation/4.3-stable.expected b/misc/extension_api_validation/4.3-stable.expected index 80735f28e7c..39dd0640122 100644 --- a/misc/extension_api_validation/4.3-stable.expected +++ b/misc/extension_api_validation/4.3-stable.expected @@ -48,3 +48,11 @@ GH-93605 Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/Semaphore/methods/post': arguments Optional arguments added. Compatibility methods registered. + + +GH-95212 +-------- +Validate extension JSON: Error: Field 'classes/RegEx/methods/compile/arguments': size changed value in new API, from 1 to 2. +Validate extension JSON: Error: Field 'classes/RegEx/methods/create_from_string/arguments': size changed value in new API, from 1 to 2. + +Add optional argument to control error printing on compilation fail. Compatibility methods registered. diff --git a/modules/regex/doc_classes/RegEx.xml b/modules/regex/doc_classes/RegEx.xml index ab74fce3a93..e12dc43b6ff 100644 --- a/modules/regex/doc_classes/RegEx.xml +++ b/modules/regex/doc_classes/RegEx.xml @@ -58,15 +58,17 @@ + - Compiles and assign the search pattern to use. Returns [constant OK] if the compilation is successful. If an error is encountered, details are printed to standard output and an error is returned. + Compiles and assign the search pattern to use. Returns [constant OK] if the compilation is successful. If compilation fails, returns [constant FAILED] and when [param show_error] is [code]true[/code], details are printed to standard output. + - Creates and compiles a new [RegEx] object. + Creates and compiles a new [RegEx] object. See also [method compile]. diff --git a/modules/regex/regex.compat.inc b/modules/regex/regex.compat.inc new file mode 100644 index 00000000000..0c380655a45 --- /dev/null +++ b/modules/regex/regex.compat.inc @@ -0,0 +1,46 @@ +/**************************************************************************/ +/* regex.compat.inc */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef DISABLE_DEPRECATED + +Ref RegEx::_create_from_string_bind_compat_95212(const String &p_pattern) { + return create_from_string(p_pattern, true); +} + +Error RegEx::_compile_bind_compat_95212(const String &p_pattern) { + return compile(p_pattern, true); +} + +void RegEx::_bind_compatibility_methods() { + ClassDB::bind_compatibility_static_method("RegEx", D_METHOD("create_from_string", "pattern"), &RegEx::_create_from_string_bind_compat_95212); + ClassDB::bind_compatibility_method(D_METHOD("compile", "pattern"), &RegEx::_compile_bind_compat_95212); +} + +#endif diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index 9f34a6ca6a8..85c0b9ecad2 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::create_from_string(const String &p_pattern) { +Ref RegEx::create_from_string(const String &p_pattern, bool p_show_error) { Ref 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)); diff --git a/modules/regex/regex.h b/modules/regex/regex.h index 13476d69de6..cb8b0459ade 100644 --- a/modules/regex/regex.h +++ b/modules/regex/regex.h @@ -81,11 +81,17 @@ class RegEx : public RefCounted { protected: static void _bind_methods(); +#ifndef DISABLE_DEPRECATED + static Ref _create_from_string_bind_compat_95212(const String &p_pattern); + Error _compile_bind_compat_95212(const String &p_pattern); + static void _bind_compatibility_methods(); +#endif + public: - static Ref create_from_string(const String &p_pattern); + static Ref create_from_string(const String &p_pattern, bool p_show_error = true); void clear(); - Error compile(const String &p_pattern); + Error compile(const String &p_pattern, bool p_show_error = true); Ref search(const String &p_subject, int p_offset = 0, int p_end = -1) const; TypedArray search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const;