feat: updated engine version to 4.4-rc1

This commit is contained in:
Sara 2025-02-23 14:38:14 +01:00
parent ee00efde1f
commit 21ba8e33af
5459 changed files with 1128836 additions and 198305 deletions

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")

View file

@ -34,14 +34,14 @@
print(result.get_string("digit"))
# Would print 01 03 0 3f 42
[/codeblock]
[b]Example of splitting a string using a RegEx:[/b]
[b]Example:[/b] Split a string using a RegEx:
[codeblock]
var regex = RegEx.new()
regex.compile("\\S+") # Negated whitespace character class.
var results = []
for result in regex.search_all("One Two \n\tThree"):
results.push_back(result.get_string())
# The `results` array now contains "One", "Two", "Three".
# The `results` array now contains "One", "Two", and "Three".
[/codeblock]
[b]Note:[/b] Godot's regex implementation is based on the [url=https://www.pcre.org/]PCRE2[/url] library. You can view the full pattern reference [url=https://www.pcre.org/current/doc/html/pcre2pattern.html]here[/url].
[b]Tip:[/b] You can use [url=https://regexr.com/]Regexr[/url] to test regular expressions online.
@ -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"
@ -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_int64(offset) + ": " + String((const char32_t *)buf);
ERR_PRINT(message.utf8());
}
return FAILED;
}
return OK;
@ -286,25 +289,17 @@ TypedArray<RegExMatch> RegEx::search_all(const String &p_subject, int p_offset,
return result;
}
String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_all, int p_offset, int p_end) const {
ERR_FAIL_COND_V(!is_valid(), String());
ERR_FAIL_COND_V_MSG(p_offset < 0, String(), "RegEx sub offset must be >= 0");
// safety_zone is the number of chars we allocate in addition to the number of chars expected in order to
// guard against the PCRE API writing one additional \0 at the end. PCRE's API docs are unclear on whether
// PCRE understands outlength in pcre2_substitute() as counting an implicit additional terminating char or
// not. always allocating one char more than telling PCRE has us on the safe side.
int RegEx::_sub(const String &p_subject, const String &p_replacement, int p_offset, int p_end, uint32_t p_flags, String &r_output) const {
// `safety_zone` is the number of chars we allocate in addition to the number of chars expected in order to
// guard against the PCRE API writing one additional `\0` at the end. PCRE's API docs are unclear on whether
// PCRE understands outlength in `pcre2_substitute(`) as counting an implicit additional terminating char or
// not. Always allocating one char more than telling PCRE has us on the safe side.
const int safety_zone = 1;
PCRE2_SIZE olength = p_subject.length() + 1; // space for output string and one terminating \0 character
PCRE2_SIZE olength = p_subject.length() + 1; // Space for output string and one terminating `\0` character.
Vector<char32_t> output;
output.resize(olength + safety_zone);
uint32_t flags = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
if (p_all) {
flags |= PCRE2_SUBSTITUTE_GLOBAL;
}
PCRE2_SIZE length = p_subject.length();
if (p_end >= 0 && (uint32_t)p_end < length) {
length = p_end;
@ -319,22 +314,49 @@ String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_a
pcre2_match_data_32 *match = pcre2_match_data_create_from_pattern_32(c, gctx);
int res = pcre2_substitute_32(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
int res = pcre2_substitute_32(c, s, length, p_offset, p_flags, match, mctx, r, p_replacement.length(), o, &olength);
if (res == PCRE2_ERROR_NOMEMORY) {
output.resize(olength + safety_zone);
o = (PCRE2_UCHAR32 *)output.ptrw();
res = pcre2_substitute_32(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
res = pcre2_substitute_32(c, s, length, p_offset, p_flags, match, mctx, r, p_replacement.length(), o, &olength);
}
pcre2_match_data_free_32(match);
pcre2_match_context_free_32(mctx);
if (res < 0) {
return String();
if (res >= 0) {
r_output = String(output.ptr(), olength) + p_subject.substr(length);
}
return String(output.ptr(), olength) + p_subject.substr(length);
return res;
}
String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_all, int p_offset, int p_end) const {
ERR_FAIL_COND_V(!is_valid(), String());
ERR_FAIL_COND_V_MSG(p_offset < 0, String(), "RegEx sub offset must be >= 0");
uint32_t flags = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH | PCRE2_SUBSTITUTE_UNSET_EMPTY;
if (p_all) {
flags |= PCRE2_SUBSTITUTE_GLOBAL;
}
String output;
const int res = _sub(p_subject, p_replacement, p_offset, p_end, flags, output);
if (res < 0) {
PCRE2_UCHAR32 buf[256];
pcre2_get_error_message_32(res, buf, 256);
String message = "PCRE2 Error: " + String((const char32_t *)buf);
ERR_PRINT(message.utf8());
if (res == PCRE2_ERROR_NOSUBSTRING) {
flags |= PCRE2_SUBSTITUTE_UNKNOWN_UNSET;
_sub(p_subject, p_replacement, p_offset, p_end, flags, output);
}
}
return output;
}
bool RegEx::is_valid() const {
@ -395,10 +417,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

@ -78,14 +78,22 @@ class RegEx : public RefCounted {
void _pattern_info(uint32_t what, void *where) const;
int _sub(const String &p_subject, const String &p_replacement, int p_offset, int p_end, uint32_t p_flags, String &r_output) const;
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;

View file

@ -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);
}
@ -145,6 +145,15 @@ TEST_CASE("[RegEx] Substitution") {
CHECK(re5.sub(s5, "cc", true, 0, 2) == "ccccaa");
CHECK(re5.sub(s5, "cc", true, 1, 3) == "acccca");
CHECK(re5.sub(s5, "", true, 0, 2) == "aa");
const String s6 = "property get_property set_property";
RegEx re6("(get_|set_)?property");
REQUIRE(re6.is_valid());
CHECK(re6.sub(s6, "$1new_property", true) == "new_property get_new_property set_new_property");
ERR_PRINT_OFF;
CHECK(re6.sub(s6, "$5new_property", true) == "new_property new_property new_property");
ERR_PRINT_ON;
}
TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {
@ -168,7 +177,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 +246,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 +260,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 +274,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 +316,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 +334,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 +350,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 +364,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 +395,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 +404,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 +413,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);