diff options
Diffstat (limited to 'modules/regex')
-rw-r--r-- | modules/regex/doc_classes/RegEx.xml | 35 | ||||
-rw-r--r-- | modules/regex/doc_classes/RegExMatch.xml | 6 | ||||
-rw-r--r-- | modules/regex/regex.cpp | 29 | ||||
-rw-r--r-- | modules/regex/regex.h | 11 | ||||
-rw-r--r-- | modules/regex/register_types.cpp | 11 | ||||
-rw-r--r-- | modules/regex/register_types.h | 6 | ||||
-rw-r--r-- | modules/regex/tests/test_regex.h | 12 |
7 files changed, 66 insertions, 44 deletions
diff --git a/modules/regex/doc_classes/RegEx.xml b/modules/regex/doc_classes/RegEx.xml index deabc5ccd3..56404f796c 100644 --- a/modules/regex/doc_classes/RegEx.xml +++ b/modules/regex/doc_classes/RegEx.xml @@ -57,11 +57,18 @@ </method> <method name="compile"> <return type="int" enum="Error" /> - <argument index="0" name="pattern" type="String" /> + <param index="0" name="pattern" type="String" /> <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. </description> </method> + <method name="create_from_string" qualifiers="static"> + <return type="RegEx" /> + <param index="0" name="pattern" type="String" /> + <description> + Creates and compiles a new [RegEx] object. + </description> + </method> <method name="get_group_count" qualifiers="const"> <return type="int" /> <description> @@ -69,7 +76,7 @@ </description> </method> <method name="get_names" qualifiers="const"> - <return type="Array" /> + <return type="PackedStringArray" /> <description> Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance. </description> @@ -88,29 +95,29 @@ </method> <method name="search" qualifiers="const"> <return type="RegExMatch" /> - <argument index="0" name="subject" type="String" /> - <argument index="1" name="offset" type="int" default="0" /> - <argument index="2" name="end" type="int" default="-1" /> + <param index="0" name="subject" type="String" /> + <param index="1" name="offset" type="int" default="0" /> + <param index="2" name="end" type="int" default="-1" /> <description> Searches the text for the compiled pattern. Returns a [RegExMatch] container of the first matching result if found, otherwise [code]null[/code]. The region to search within can be specified without modifying where the start and end anchor would be. </description> </method> <method name="search_all" qualifiers="const"> - <return type="Array" /> - <argument index="0" name="subject" type="String" /> - <argument index="1" name="offset" type="int" default="0" /> - <argument index="2" name="end" type="int" default="-1" /> + <return type="RegExMatch[]" /> + <param index="0" name="subject" type="String" /> + <param index="1" name="offset" type="int" default="0" /> + <param index="2" name="end" type="int" default="-1" /> <description> Searches the text for the compiled pattern. Returns an array of [RegExMatch] containers for each non-overlapping result. If no results were found, an empty array is returned instead. The region to search within can be specified without modifying where the start and end anchor would be. </description> </method> <method name="sub" qualifiers="const"> <return type="String" /> - <argument index="0" name="subject" type="String" /> - <argument index="1" name="replacement" type="String" /> - <argument index="2" name="all" type="bool" default="false" /> - <argument index="3" name="offset" type="int" default="0" /> - <argument index="4" name="end" type="int" default="-1" /> + <param index="0" name="subject" type="String" /> + <param index="1" name="replacement" type="String" /> + <param index="2" name="all" type="bool" default="false" /> + <param index="3" name="offset" type="int" default="0" /> + <param index="4" name="end" type="int" default="-1" /> <description> Searches the text for the compiled pattern and replaces it with the specified string. Escapes and backreferences such as [code]$1[/code] and [code]$name[/code] are expanded and resolved. By default, only the first instance is replaced, but it can be changed for all instances (global replacement). The region to search within can be specified without modifying where the start and end anchor would be. </description> diff --git a/modules/regex/doc_classes/RegExMatch.xml b/modules/regex/doc_classes/RegExMatch.xml index 530a541ae8..5bcf070e82 100644 --- a/modules/regex/doc_classes/RegExMatch.xml +++ b/modules/regex/doc_classes/RegExMatch.xml @@ -11,7 +11,7 @@ <methods> <method name="get_end" qualifiers="const"> <return type="int" /> - <argument index="0" name="name" type="Variant" default="0" /> + <param index="0" name="name" type="Variant" default="0" /> <description> Returns the end position of the match within the source string. The end position of capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern. Returns -1 if the group did not match or doesn't exist. @@ -25,7 +25,7 @@ </method> <method name="get_start" qualifiers="const"> <return type="int" /> - <argument index="0" name="name" type="Variant" default="0" /> + <param index="0" name="name" type="Variant" default="0" /> <description> Returns the starting position of the match within the source string. The starting position of capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern. Returns -1 if the group did not match or doesn't exist. @@ -33,7 +33,7 @@ </method> <method name="get_string" qualifiers="const"> <return type="String" /> - <argument index="0" name="name" type="Variant" default="0" /> + <param index="0" name="name" type="Variant" default="0" /> <description> Returns the substring of the match from the source string. Capturing groups can be retrieved by providing its group number as an integer or its string name (if it's a named group). The default value of 0 refers to the whole pattern. Returns an empty string if the group did not match or doesn't exist. diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index ee1137b71f..b2e6ea1004 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -52,9 +52,9 @@ int RegExMatch::_find(const Variant &p_name) const { return i; } else if (p_name.get_type() == Variant::STRING) { - const Map<String, int>::Element *found = names.find((String)p_name); + HashMap<String, int>::ConstIterator found = names.find((String)p_name); if (found) { - return found->value(); + return found->value; } } @@ -75,8 +75,8 @@ int RegExMatch::get_group_count() const { Dictionary RegExMatch::get_names() const { Dictionary result; - for (const Map<String, int>::Element *i = names.front(); i != nullptr; i = i->next()) { - result[i->key()] = i->value(); + for (const KeyValue<String, int> &E : names) { + result[E.key] = E.value; } return result; @@ -159,6 +159,13 @@ 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> ret; + ret.instantiate(); + ret->compile(p_pattern); + return ret; +} + void RegEx::clear() { if (code) { pcre2_code_free_32((pcre2_code_32 *)code); @@ -194,6 +201,7 @@ Error RegEx::compile(const String &p_pattern) { Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end) const { ERR_FAIL_COND_V(!is_valid(), nullptr); + ERR_FAIL_COND_V_MSG(p_offset < 0, nullptr, "RegEx search offset must be >= 0"); Ref<RegExMatch> result = memnew(RegExMatch); @@ -257,9 +265,11 @@ Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end) 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"); + int last_end = -1; - Array result; + TypedArray<RegExMatch> result; Ref<RegExMatch> match = search(p_subject, p_offset, p_end); while (match.is_valid()) { if (last_end == match->get_end(0)) { @@ -274,6 +284,7 @@ Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const 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 @@ -340,8 +351,8 @@ int RegEx::get_group_count() const { return count; } -Array RegEx::get_names() const { - Array result; +PackedStringArray RegEx::get_names() const { + PackedStringArray result; ERR_FAIL_COND_V(!is_valid(), result); @@ -380,6 +391,8 @@ RegEx::~RegEx() { } 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("compile", "pattern"), &RegEx::compile); ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1)); diff --git a/modules/regex/regex.h b/modules/regex/regex.h index e7221f4070..6920d2634d 100644 --- a/modules/regex/regex.h +++ b/modules/regex/regex.h @@ -33,10 +33,11 @@ #include "core/object/ref_counted.h" #include "core/string/ustring.h" -#include "core/templates/map.h" +#include "core/templates/rb_map.h" #include "core/templates/vector.h" #include "core/variant/array.h" #include "core/variant/dictionary.h" +#include "core/variant/typed_array.h" class RegExMatch : public RefCounted { GDCLASS(RegExMatch, RefCounted); @@ -48,7 +49,7 @@ class RegExMatch : public RefCounted { String subject; Vector<Range> data; - Map<String, int> names; + HashMap<String, int> names; friend class RegEx; @@ -81,17 +82,19 @@ protected: static void _bind_methods(); public: + static Ref<RegEx> create_from_string(const String &p_pattern); + void clear(); Error compile(const String &p_pattern); 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; bool is_valid() const; String get_pattern() const; int get_group_count() const; - Array get_names() const; + PackedStringArray get_names() const; RegEx(); RegEx(const String &p_pattern); diff --git a/modules/regex/register_types.cpp b/modules/regex/register_types.cpp index 9289a724d8..2103c57f77 100644 --- a/modules/regex/register_types.cpp +++ b/modules/regex/register_types.cpp @@ -32,10 +32,17 @@ #include "core/object/class_db.h" #include "regex.h" -void register_regex_types() { +void initialize_regex_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + GDREGISTER_CLASS(RegExMatch); GDREGISTER_CLASS(RegEx); } -void unregister_regex_types() { +void uninitialize_regex_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } } diff --git a/modules/regex/register_types.h b/modules/regex/register_types.h index b2d5009ef4..c3edf23562 100644 --- a/modules/regex/register_types.h +++ b/modules/regex/register_types.h @@ -31,7 +31,9 @@ #ifndef REGEX_REGISTER_TYPES_H #define REGEX_REGISTER_TYPES_H -void register_regex_types(); -void unregister_regex_types(); +#include "modules/register_module_types.h" + +void initialize_regex_module(ModuleInitializationLevel p_level); +void uninitialize_regex_module(ModuleInitializationLevel p_level); #endif // REGEX_REGISTER_TYPES_H diff --git a/modules/regex/tests/test_regex.h b/modules/regex/tests/test_regex.h index 3f500e7b2f..c81094d3ae 100644 --- a/modules/regex/tests/test_regex.h +++ b/modules/regex/tests/test_regex.h @@ -46,7 +46,7 @@ TEST_CASE("[RegEx] Initialization") { CHECK(re1.get_pattern() == pattern); CHECK(re1.get_group_count() == 1); - Array names = re1.get_names(); + PackedStringArray names = re1.get_names(); CHECK(names.size() == 1); CHECK(names[0] == "vowel"); @@ -130,16 +130,6 @@ TEST_CASE("[RegEx] Empty Pattern") { CHECK(re.is_valid()); } -TEST_CASE("[RegEx] Invalid offset") { - const String s = "Godot"; - - RegEx re("o"); - REQUIRE(re.is_valid()); - CHECK(re.search(s, -1) == nullptr); - CHECK(re.search_all(s, -1).size() == 0); - CHECK(re.sub(s, "", true, -1) == ""); -} - TEST_CASE("[RegEx] Invalid end position") { const String s = "Godot"; |