Merge pull request #63830 from KoBeWi/regEX

Add static method for creating RegEx
This commit is contained in:
Rémi Verschelde 2022-08-02 18:16:04 +02:00 committed by GitHub
commit c67febb008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

View File

@ -2519,7 +2519,7 @@ Vector<String> ProjectConverter3To4::check_for_rename_enums(Vector<String> &file
int current_line = 1; int current_line = 1;
for (String &line : file_content) { for (String &line : file_content) {
Array reg_match = reg.search_all(line); TypedArray<RegExMatch> reg_match = reg.search_all(line);
if (reg_match.size() > 0) { if (reg_match.size() > 0) {
found_things.append(line_formatter(current_line, colors_renames[current_index][0], colors_renames[current_index][1], line)); found_things.append(line_formatter(current_line, colors_renames[current_index][0], colors_renames[current_index][1], line));
} }
@ -2596,7 +2596,7 @@ Vector<String> ProjectConverter3To4::check_for_rename_classes(Vector<String> &fi
line = reg_before.sub(line, "TEMP_RENAMED_CLASS.tscn", true); line = reg_before.sub(line, "TEMP_RENAMED_CLASS.tscn", true);
line = reg_before2.sub(line, "TEMP_RENAMED_CLASS.gd", true); line = reg_before2.sub(line, "TEMP_RENAMED_CLASS.gd", true);
Array reg_match = reg.search_all(line); TypedArray<RegExMatch> reg_match = reg.search_all(line);
if (reg_match.size() > 0) { if (reg_match.size() > 0) {
found_things.append(line_formatter(current_line, class_renames[current_index][0], class_renames[current_index][1], line)); found_things.append(line_formatter(current_line, class_renames[current_index][0], class_renames[current_index][1], line));
} }
@ -3810,7 +3810,7 @@ Vector<String> ProjectConverter3To4::check_for_custom_rename(Vector<String> &fil
int current_line = 1; int current_line = 1;
for (String &line : file_content) { for (String &line : file_content) {
Array reg_match = reg.search_all(line); TypedArray<RegExMatch> reg_match = reg.search_all(line);
if (reg_match.size() > 0) { if (reg_match.size() > 0) {
found_things.append(line_formatter(current_line, from.replace("\\.", "."), to, line)); // Without replacing it will print "\.shader" instead ".shader" found_things.append(line_formatter(current_line, from.replace("\\.", "."), to, line)); // Without replacing it will print "\.shader" instead ".shader"
} }
@ -3841,7 +3841,7 @@ Vector<String> ProjectConverter3To4::check_for_rename_common(const char *array[]
int current_line = 1; int current_line = 1;
for (String &line : file_content) { for (String &line : file_content) {
Array reg_match = reg.search_all(line); TypedArray<RegExMatch> reg_match = reg.search_all(line);
if (reg_match.size() > 0) { if (reg_match.size() > 0) {
found_things.append(line_formatter(current_line, array[current_index][0], array[current_index][1], line)); found_things.append(line_formatter(current_line, array[current_index][0], array[current_index][1], line));
} }

View File

@ -62,6 +62,13 @@
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 an error is encountered, details are printed to standard output and an error is returned.
</description> </description>
</method> </method>
<method name="create_from_string" qualifiers="static">
<return type="RegEx" />
<argument index="0" name="pattern" type="String" />
<description>
Creates and compiles a new [RegEx] object.
</description>
</method>
<method name="get_group_count" qualifiers="const"> <method name="get_group_count" qualifiers="const">
<return type="int" /> <return type="int" />
<description> <description>
@ -96,7 +103,7 @@
</description> </description>
</method> </method>
<method name="search_all" qualifiers="const"> <method name="search_all" qualifiers="const">
<return type="Array" /> <return type="RegExMatch[]" />
<argument index="0" name="subject" type="String" /> <argument index="0" name="subject" type="String" />
<argument index="1" name="offset" type="int" default="0" /> <argument index="1" name="offset" type="int" default="0" />
<argument index="2" name="end" type="int" default="-1" /> <argument index="2" name="end" type="int" default="-1" />

View File

@ -159,6 +159,13 @@ void RegEx::_pattern_info(uint32_t what, void *where) const {
pcre2_pattern_info_32((pcre2_code_32 *)code, what, where); pcre2_pattern_info_32((pcre2_code_32 *)code, what, where);
} }
Ref<RegEx> RegEx::create_from_string(const String &p_pattern) {
Ref<RegEx> ret;
ret.instantiate();
ret->compile(p_pattern);
return ret;
}
void RegEx::clear() { void RegEx::clear() {
if (code) { if (code) {
pcre2_code_free_32((pcre2_code_32 *)code); pcre2_code_free_32((pcre2_code_32 *)code);
@ -258,11 +265,11 @@ Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end)
return result; return result;
} }
Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const { TypedArray<RegExMatch> RegEx::search_all(const String &p_subject, int p_offset, int p_end) const {
ERR_FAIL_COND_V_MSG(p_offset < 0, Array(), "RegEx search offset must be >= 0"); ERR_FAIL_COND_V_MSG(p_offset < 0, Array(), "RegEx search offset must be >= 0");
int last_end = -1; int last_end = -1;
Array result; TypedArray<RegExMatch> result;
Ref<RegExMatch> match = search(p_subject, p_offset, p_end); Ref<RegExMatch> match = search(p_subject, p_offset, p_end);
while (match.is_valid()) { while (match.is_valid()) {
if (last_end == match->get_end(0)) { if (last_end == match->get_end(0)) {
@ -384,6 +391,8 @@ RegEx::~RegEx() {
} }
void RegEx::_bind_methods() { void RegEx::_bind_methods() {
ClassDB::bind_static_method("RegEx", D_METHOD("create_from_string", "pattern"), &RegEx::create_from_string);
ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear); ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear);
ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile); ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile);
ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1)); ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1));

View File

@ -37,6 +37,7 @@
#include "core/templates/vector.h" #include "core/templates/vector.h"
#include "core/variant/array.h" #include "core/variant/array.h"
#include "core/variant/dictionary.h" #include "core/variant/dictionary.h"
#include "core/variant/typed_array.h"
class RegExMatch : public RefCounted { class RegExMatch : public RefCounted {
GDCLASS(RegExMatch, RefCounted); GDCLASS(RegExMatch, RefCounted);
@ -81,11 +82,13 @@ protected:
static void _bind_methods(); static void _bind_methods();
public: public:
static Ref<RegEx> create_from_string(const String &p_pattern);
void clear(); void clear();
Error compile(const String &p_pattern); Error compile(const String &p_pattern);
Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const; Ref<RegExMatch> search(const String &p_subject, int p_offset = 0, int p_end = -1) const;
Array search_all(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;
String sub(const String &p_subject, const String &p_replacement, bool p_all = false, int p_offset = 0, int p_end = -1) const; String sub(const String &p_subject, const String &p_replacement, bool p_all = false, int p_offset = 0, int p_end = -1) const;
bool is_valid() const; bool is_valid() const;