[RegEx] Add show_error parameter to control error printing on compilation fail

This commit is contained in:
Nikita\Nick 2024-08-28 15:35:42 +03:00
parent f648de1a83
commit ebb5a5cc3d
5 changed files with 78 additions and 13 deletions

View File

@ -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.

View File

@ -58,15 +58,17 @@
<method name="compile">
<return type="int" enum="Error" />
<param index="0" name="pattern" type="String" />
<param index="1" name="show_error" type="bool" default="true" />
<description>
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.
</description>
</method>
<method name="create_from_string" qualifiers="static">
<return type="RegEx" />
<param index="0" name="pattern" type="String" />
<param index="1" name="show_error" type="bool" default="true" />
<description>
Creates and compiles a new [RegEx] object.
Creates and compiles a new [RegEx] object. See also [method compile].
</description>
</method>
<method name="get_group_count" qualifiers="const">

View File

@ -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> 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

View File

@ -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));

View File

@ -81,11 +81,17 @@ class RegEx : public RefCounted {
protected:
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
static Ref<RegEx> _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<RegEx> create_from_string(const String &p_pattern);
static Ref<RegEx> 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<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const;
TypedArray<RegExMatch> search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const;