summaryrefslogtreecommitdiffstats
path: root/modules/regex
diff options
context:
space:
mode:
Diffstat (limited to 'modules/regex')
-rw-r--r--modules/regex/doc_classes/RegEx.xml6
-rw-r--r--modules/regex/icons/RegEx.svg2
-rw-r--r--modules/regex/icons/RegExMatch.svg2
-rw-r--r--modules/regex/regex.compat.inc46
-rw-r--r--modules/regex/regex.cpp25
-rw-r--r--modules/regex/regex.h10
-rw-r--r--modules/regex/tests/test_regex.h50
7 files changed, 99 insertions, 42 deletions
diff --git a/modules/regex/doc_classes/RegEx.xml b/modules/regex/doc_classes/RegEx.xml
index ab74fce3a9..e12dc43b6f 100644
--- a/modules/regex/doc_classes/RegEx.xml
+++ b/modules/regex/doc_classes/RegEx.xml
@@ -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">
diff --git a/modules/regex/icons/RegEx.svg b/modules/regex/icons/RegEx.svg
index 4df26f41c0..ba232f6f0a 100644
--- a/modules/regex/icons/RegEx.svg
+++ b/modules/regex/icons/RegEx.svg
@@ -1 +1 @@
-<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M2 14h3v-3H2zM6.561 2.855a21 21 0 0 1 2.82 1.185A21 21 0 0 1 9.137 1h1.77a21 21 0 0 1-.28 3.027 21 21 0 0 1 2.88-1.171l.562 1.733a21 21 0 0 1-3.04.684 21 21 0 0 1 2.1 2.307l-1.465 1.037a21 21 0 0 1-1.672-2.624 21 21 0 0 1-1.587 2.624L6.965 7.58a21 21 0 0 1 2.026-2.308A21 21 0 0 1 6 4.59z" fill="#e0e0e0"/></svg>
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#e0e0e0" d="M2 14h3v-3H2zM6.561 2.855a21 21 0 0 1 2.82 1.185A21 21 0 0 1 9.137 1h1.77a21 21 0 0 1-.28 3.027 21 21 0 0 1 2.88-1.171l.562 1.733a21 21 0 0 1-3.04.684 21 21 0 0 1 2.1 2.307l-1.465 1.037a21 21 0 0 1-1.672-2.624 21 21 0 0 1-1.587 2.624L6.965 7.58a21 21 0 0 1 2.026-2.308A21 21 0 0 1 6 4.59z"/></svg> \ No newline at end of file
diff --git a/modules/regex/icons/RegExMatch.svg b/modules/regex/icons/RegExMatch.svg
index 889cf6cc8a..626ff36691 100644
--- a/modules/regex/icons/RegExMatch.svg
+++ b/modules/regex/icons/RegExMatch.svg
@@ -1 +1 @@
-<svg height="16" viewBox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="M5 13h2v-2H5zm2.5-8a14 14 0 0 1 1.88.79 14 14 0 0 1-.163-2.027h1.18a14 14 0 0 1-.186 2.018 14 14 0 0 1 1.92-.78l.374 1.155a14 14 0 0 1-2.026.456 14 14 0 0 1 1.4 1.538l-.977.691a14 14 0 0 1-1.115-1.75 14 14 0 0 1-1.058 1.75l-.96-.691A14 14 0 0 1 9.12 6.61a14 14 0 0 1-1.993-.454zM1.67 2C0 5 0 11 1.67 14h2C2 11 2 5 3.67 2zm10.66 0c1.67 3 1.67 9 0 12h2c1.67-3 1.67-9 0-12z" fill="#e0e0e0"/></svg>
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#e0e0e0" d="M5 13h2v-2H5zm2.5-8a14 14 0 0 1 1.88.79 14 14 0 0 1-.163-2.027h1.18a14 14 0 0 1-.186 2.018 14 14 0 0 1 1.92-.78l.374 1.155a14 14 0 0 1-2.026.456 14 14 0 0 1 1.4 1.538l-.977.691a14 14 0 0 1-1.115-1.75 14 14 0 0 1-1.058 1.75l-.96-.691A14 14 0 0 1 9.12 6.61a14 14 0 0 1-1.993-.454zM1.67 2C0 5 0 11 1.67 14h2C2 11 2 5 3.67 2zm10.66 0c1.67 3 1.67 9 0 12h2c1.67-3 1.67-9 0-12z"/></svg> \ No newline at end of file
diff --git a/modules/regex/regex.compat.inc b/modules/regex/regex.compat.inc
new file mode 100644
index 0000000000..0c380655a4
--- /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> 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 9f34a6ca6a..9c366408a0 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"
@@ -53,8 +54,8 @@ int RegExMatch::_find(const Variant &p_name) const {
return -1;
}
return i;
- } else if (p_name.get_type() == Variant::STRING || p_name.get_type() == Variant::STRING_NAME) {
- HashMap<String, int>::ConstIterator found = names.find((String)p_name);
+ } else if (p_name.is_string()) {
+ HashMap<String, int>::ConstIterator found = names.find(p_name);
if (found) {
return found->value;
}
@@ -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));
diff --git a/modules/regex/regex.h b/modules/regex/regex.h
index 13476d69de..cb8b0459ad 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<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;
diff --git a/modules/regex/tests/test_regex.h b/modules/regex/tests/test_regex.h
index af58e2487b..7e8e456341 100644
--- a/modules/regex/tests/test_regex.h
+++ b/modules/regex/tests/test_regex.h
@@ -80,32 +80,32 @@ TEST_CASE("[RegEx] Searching") {
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "ea");
match = re.search(s, 1, 2);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "e");
match = re.search(s, 2, 4);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "a");
match = re.search(s, 3, 5);
- CHECK(match == nullptr);
+ CHECK(match.is_null());
match = re.search(s, 6, 2);
- CHECK(match == nullptr);
+ CHECK(match.is_null());
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 2);
match = all_results[0];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "ea");
match = all_results[1];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "i");
CHECK(re.compile(numerics) == OK);
CHECK(re.is_valid());
- CHECK(re.search(s) == nullptr);
+ CHECK(re.search(s).is_null());
CHECK(re.search_all(s).size() == 0);
}
@@ -168,7 +168,7 @@ TEST_CASE("[RegEx] Uninitialized use") {
RegEx re;
ERR_PRINT_OFF;
- CHECK(re.search(s) == nullptr);
+ CHECK(re.search(s).is_null());
CHECK(re.search_all(s).size() == 0);
CHECK(re.sub(s, "") == "");
CHECK(re.get_group_count() == 0);
@@ -237,10 +237,10 @@ TEST_CASE("[RegEx] Invalid end position") {
const Array all_results = re.search_all(s, 0, 10);
CHECK(all_results.size() == 2);
match = all_results[0];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String("o"));
match = all_results[1];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String("o"));
CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
@@ -251,7 +251,7 @@ TEST_CASE("[RegEx] Get match string list") {
RegEx re("(Go)(dot)");
Ref<RegExMatch> match = re.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
PackedStringArray result;
result.append("Godot");
result.append("Go");
@@ -265,14 +265,14 @@ TEST_CASE("[RegEx] Match start and end positions") {
RegEx re1("pattern");
REQUIRE(re1.is_valid());
Ref<RegExMatch> match = re1.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 6);
CHECK(match->get_end(0) == 13);
RegEx re2("(?<vowel>[aeiou])");
REQUIRE(re2.is_valid());
match = re2.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start("vowel") == 2);
CHECK(match->get_end("vowel") == 3);
}
@@ -307,7 +307,7 @@ TEST_CASE("[RegEx] Simple lookahead") {
RegEx re("o(?=t)");
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
}
@@ -325,12 +325,12 @@ TEST_CASE("[RegEx] Lookahead groups empty matches") {
CHECK(all_results.size() == 2);
match = all_results[0];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("12"));
match = all_results[1];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("2"));
}
@@ -341,7 +341,7 @@ TEST_CASE("[RegEx] Simple lookbehind") {
RegEx re("(?<=d)o");
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
}
@@ -355,22 +355,22 @@ TEST_CASE("[RegEx] Simple lookbehind search all") {
CHECK(all_results.size() == 4);
Ref<RegExMatch> match = all_results[0];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 1);
CHECK(match->get_end(0) == 2);
match = all_results[1];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
match = all_results[2];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 7);
CHECK(match->get_end(0) == 8);
match = all_results[3];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 9);
CHECK(match->get_end(0) == 10);
}
@@ -386,7 +386,7 @@ TEST_CASE("[RegEx] Lookbehind groups empty matches") {
CHECK(all_results.size() == 3);
match = all_results[0];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 2);
CHECK(match->get_end(0) == 2);
CHECK(match->get_start(1) == 1);
@@ -395,7 +395,7 @@ TEST_CASE("[RegEx] Lookbehind groups empty matches") {
CHECK(match->get_string(1) == String("b"));
match = all_results[1];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 6);
CHECK(match->get_end(0) == 6);
CHECK(match->get_start(1) == 5);
@@ -404,7 +404,7 @@ TEST_CASE("[RegEx] Lookbehind groups empty matches") {
CHECK(match->get_string(1) == String("b"));
match = all_results[2];
- REQUIRE(match != nullptr);
+ REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 8);
CHECK(match->get_end(0) == 8);
CHECK(match->get_start(1) == 7);