Skip to content

Commit 6eeff72

Browse files
committed
Merge pull request godotengine#95212 from Germenzi/regex-show-error
[RegEx] Add `show_error` parameter to control error printing on compilation fail
2 parents d1c238f + ebb5a5c commit 6eeff72

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

misc/extension_api_validation/4.3-stable.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ GH-93605
4848
Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/Semaphore/methods/post': arguments
4949

5050
Optional arguments added. Compatibility methods registered.
51+
52+
53+
GH-95212
54+
--------
55+
Validate extension JSON: Error: Field 'classes/RegEx/methods/compile/arguments': size changed value in new API, from 1 to 2.
56+
Validate extension JSON: Error: Field 'classes/RegEx/methods/create_from_string/arguments': size changed value in new API, from 1 to 2.
57+
58+
Add optional argument to control error printing on compilation fail. Compatibility methods registered.

modules/regex/doc_classes/RegEx.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@
5858
<method name="compile">
5959
<return type="int" enum="Error" />
6060
<param index="0" name="pattern" type="String" />
61+
<param index="1" name="show_error" type="bool" default="true" />
6162
<description>
62-
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.
63+
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.
6364
</description>
6465
</method>
6566
<method name="create_from_string" qualifiers="static">
6667
<return type="RegEx" />
6768
<param index="0" name="pattern" type="String" />
69+
<param index="1" name="show_error" type="bool" default="true" />
6870
<description>
69-
Creates and compiles a new [RegEx] object.
71+
Creates and compiles a new [RegEx] object. See also [method compile].
7072
</description>
7173
</method>
7274
<method name="get_group_count" qualifiers="const">

modules/regex/regex.compat.inc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**************************************************************************/
2+
/* regex.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
Ref<RegEx> RegEx::_create_from_string_bind_compat_95212(const String &p_pattern) {
34+
return create_from_string(p_pattern, true);
35+
}
36+
37+
Error RegEx::_compile_bind_compat_95212(const String &p_pattern) {
38+
return compile(p_pattern, true);
39+
}
40+
41+
void RegEx::_bind_compatibility_methods() {
42+
ClassDB::bind_compatibility_static_method("RegEx", D_METHOD("create_from_string", "pattern"), &RegEx::_create_from_string_bind_compat_95212);
43+
ClassDB::bind_compatibility_method(D_METHOD("compile", "pattern"), &RegEx::_compile_bind_compat_95212);
44+
}
45+
46+
#endif

modules/regex/regex.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "regex.h"
32+
#include "regex.compat.inc"
3233

3334
#include "core/os/memory.h"
3435

@@ -161,10 +162,10 @@ void RegEx::_pattern_info(uint32_t what, void *where) const {
161162
pcre2_pattern_info_32((pcre2_code_32 *)code, what, where);
162163
}
163164

164-
Ref<RegEx> RegEx::create_from_string(const String &p_pattern) {
165+
Ref<RegEx> RegEx::create_from_string(const String &p_pattern, bool p_show_error) {
165166
Ref<RegEx> ret;
166167
ret.instantiate();
167-
ret->compile(p_pattern);
168+
ret->compile(p_pattern, p_show_error);
168169
return ret;
169170
}
170171

@@ -175,7 +176,7 @@ void RegEx::clear() {
175176
}
176177
}
177178

178-
Error RegEx::compile(const String &p_pattern) {
179+
Error RegEx::compile(const String &p_pattern, bool p_show_error) {
179180
pattern = p_pattern;
180181
clear();
181182

@@ -192,10 +193,12 @@ Error RegEx::compile(const String &p_pattern) {
192193
pcre2_compile_context_free_32(cctx);
193194

194195
if (!code) {
195-
PCRE2_UCHAR32 buf[256];
196-
pcre2_get_error_message_32(err, buf, 256);
197-
String message = String::num(offset) + ": " + String((const char32_t *)buf);
198-
ERR_PRINT(message.utf8());
196+
if (p_show_error) {
197+
PCRE2_UCHAR32 buf[256];
198+
pcre2_get_error_message_32(err, buf, 256);
199+
String message = String::num(offset) + ": " + String((const char32_t *)buf);
200+
ERR_PRINT(message.utf8());
201+
}
199202
return FAILED;
200203
}
201204
return OK;
@@ -395,10 +398,10 @@ RegEx::~RegEx() {
395398
}
396399

397400
void RegEx::_bind_methods() {
398-
ClassDB::bind_static_method("RegEx", D_METHOD("create_from_string", "pattern"), &RegEx::create_from_string);
401+
ClassDB::bind_static_method("RegEx", D_METHOD("create_from_string", "pattern", "show_error"), &RegEx::create_from_string, DEFVAL(true));
399402

400403
ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear);
401-
ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile);
404+
ClassDB::bind_method(D_METHOD("compile", "pattern", "show_error"), &RegEx::compile, DEFVAL(true));
402405
ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1));
403406
ClassDB::bind_method(D_METHOD("search_all", "subject", "offset", "end"), &RegEx::search_all, DEFVAL(0), DEFVAL(-1));
404407
ClassDB::bind_method(D_METHOD("sub", "subject", "replacement", "all", "offset", "end"), &RegEx::sub, DEFVAL(false), DEFVAL(0), DEFVAL(-1));

modules/regex/regex.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ class RegEx : public RefCounted {
8181
protected:
8282
static void _bind_methods();
8383

84+
#ifndef DISABLE_DEPRECATED
85+
static Ref<RegEx> _create_from_string_bind_compat_95212(const String &p_pattern);
86+
Error _compile_bind_compat_95212(const String &p_pattern);
87+
static void _bind_compatibility_methods();
88+
#endif
89+
8490
public:
85-
static Ref<RegEx> create_from_string(const String &p_pattern);
91+
static Ref<RegEx> create_from_string(const String &p_pattern, bool p_show_error = true);
8692

8793
void clear();
88-
Error compile(const String &p_pattern);
94+
Error compile(const String &p_pattern, bool p_show_error = true);
8995

9096
Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const;
9197
TypedArray<RegExMatch> search_all(const String &p_subject, int p_offset = 0, int p_end = -1) const;

0 commit comments

Comments
 (0)