diff options
Diffstat (limited to 'modules/regex')
-rw-r--r-- | modules/regex/SCsub | 18 | ||||
-rw-r--r-- | modules/regex/doc_classes/RegEx.xml | 4 | ||||
-rw-r--r-- | modules/regex/regex.cpp | 6 | ||||
-rw-r--r-- | modules/regex/regex.h | 11 | ||||
-rw-r--r-- | modules/regex/register_types.cpp | 4 | ||||
-rw-r--r-- | modules/regex/register_types.h | 4 | ||||
-rw-r--r-- | modules/regex/tests/test_regex.h | 164 |
7 files changed, 193 insertions, 18 deletions
diff --git a/modules/regex/SCsub b/modules/regex/SCsub index 2afacc1d9c..deb9db7591 100644 --- a/modules/regex/SCsub +++ b/modules/regex/SCsub @@ -5,6 +5,10 @@ Import("env_modules") env_regex = env_modules.Clone() +# Thirdparty source files + +thirdparty_obj = [] + if env["builtin_pcre2"]: thirdparty_dir = "#thirdparty/pcre2/src/" thirdparty_flags = ["PCRE2_STATIC", "HAVE_CONFIG_H", "SUPPORT_UNICODE"] @@ -52,11 +56,21 @@ if env["builtin_pcre2"]: env_pcre2 = env_regex.Clone() env_pcre2.disable_warnings() env_pcre2["OBJSUFFIX"] = "_" + width + env_pcre2["OBJSUFFIX"] - env_pcre2.add_source_files(env.modules_sources, thirdparty_sources) env_pcre2.Append(CPPDEFINES=[("PCRE2_CODE_UNIT_WIDTH", width)]) + env_pcre2.add_source_files(thirdparty_obj, thirdparty_sources) + env.modules_sources += thirdparty_obj pcre2_builtin("16") pcre2_builtin("32") + +# Godot source files + +module_obj = [] + env_regex.Append(CPPDEFINES=[("PCRE2_CODE_UNIT_WIDTH", 0)]) -env_regex.add_source_files(env.modules_sources, "*.cpp") +env_regex.add_source_files(module_obj, "*.cpp") +env.modules_sources += module_obj + +# Needed to force rebuilding the module files when the thirdparty library is updated. +env.Depends(module_obj, thirdparty_obj) diff --git a/modules/regex/doc_classes/RegEx.xml b/modules/regex/doc_classes/RegEx.xml index 312275842a..b21f5d1e7a 100644 --- a/modules/regex/doc_classes/RegEx.xml +++ b/modules/regex/doc_classes/RegEx.xml @@ -39,8 +39,8 @@ var regex = RegEx.new() regex.compile("\\S+") # Negated whitespace character class. var results = [] - for match in regex.search_all("One Two \n\tThree"): - results.push_back(match.get_string()) + for result in regex.search_all("One Two \n\tThree"): + results.push_back(result.get_string()) # The `results` array now contains "One", "Two", "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]. diff --git a/modules/regex/regex.cpp b/modules/regex/regex.cpp index c10a276eae..6bae12e7e6 100644 --- a/modules/regex/regex.cpp +++ b/modules/regex/regex.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -365,12 +365,10 @@ Array RegEx::get_names() const { RegEx::RegEx() { general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr); - code = nullptr; } RegEx::RegEx(const String &p_pattern) { general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr); - code = nullptr; compile(p_pattern); } diff --git a/modules/regex/regex.h b/modules/regex/regex.h index 8cf19e1ca6..f5773042fb 100644 --- a/modules/regex/regex.h +++ b/modules/regex/regex.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -42,8 +42,8 @@ class RegExMatch : public Reference { GDCLASS(RegExMatch, Reference); struct Range { - int start; - int end; + int start = 0; + int end = 0; }; String subject; @@ -72,7 +72,7 @@ class RegEx : public Reference { GDCLASS(RegEx, Reference); void *general_ctx; - void *code; + void *code = nullptr; String pattern; void _pattern_info(uint32_t what, void *where) const; @@ -83,7 +83,6 @@ protected: public: void clear(); Error compile(const String &p_pattern); - void _init(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; diff --git a/modules/regex/register_types.cpp b/modules/regex/register_types.cpp index d470fcdaeb..82f3eaf707 100644 --- a/modules/regex/register_types.cpp +++ b/modules/regex/register_types.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/regex/register_types.h b/modules/regex/register_types.h index cf377cdf5f..fe94cde954 100644 --- a/modules/regex/register_types.h +++ b/modules/regex/register_types.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ diff --git a/modules/regex/tests/test_regex.h b/modules/regex/tests/test_regex.h new file mode 100644 index 0000000000..c2d303b435 --- /dev/null +++ b/modules/regex/tests/test_regex.h @@ -0,0 +1,164 @@ +/*************************************************************************/ +/* test_regex.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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 TEST_REGEX_H +#define TEST_REGEX_H + +#include "core/string/ustring.h" +#include "modules/regex/regex.h" + +#include "tests/test_macros.h" + +namespace TestRegEx { + +TEST_CASE("[RegEx] Initialization") { + const String pattern = "(?<vowel>[aeiou])"; + + RegEx re1(pattern); + CHECK(re1.is_valid()); + CHECK(re1.get_pattern() == pattern); + CHECK(re1.get_group_count() == 1); + + Array names = re1.get_names(); + CHECK(names.size() == 1); + CHECK(names[0] == "vowel"); + + RegEx re2; + CHECK(re2.is_valid() == false); + CHECK(re2.compile(pattern) == OK); + CHECK(re2.is_valid()); + + CHECK(re1.get_pattern() == re2.get_pattern()); + CHECK(re1.get_group_count() == re2.get_group_count()); + + names = re2.get_names(); + CHECK(names.size() == 1); + CHECK(names[0] == "vowel"); +} + +TEST_CASE("[RegEx] Clearing") { + RegEx re("Godot"); + REQUIRE(re.is_valid()); + re.clear(); + CHECK(re.is_valid() == false); +} + +TEST_CASE("[RegEx] Searching") { + const String s = "Searching"; + const String vowels = "[aeiou]{1,2}"; + const String numerics = "\\d"; + + RegEx re(vowels); + REQUIRE(re.is_valid()); + + Ref<RegExMatch> match = re.search(s); + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == "ea"); + + match = re.search(s, 2, 4); + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == "a"); + + const Array all_results = re.search_all(s); + CHECK(all_results.size() == 2); + match = all_results[0]; + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == "ea"); + match = all_results[1]; + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == "i"); + + CHECK(re.compile(numerics) == OK); + CHECK(re.is_valid()); + CHECK(re.search(s) == nullptr); + CHECK(re.search_all(s).size() == 0); +} + +TEST_CASE("[RegEx] Substitution") { + String s = "Double all the vowels."; + + RegEx re("(?<vowel>[aeiou])"); + REQUIRE(re.is_valid()); + CHECK(re.sub(s, "$0$vowel", true) == "Doouublee aall thee vooweels."); +} + +TEST_CASE("[RegEx] Uninitialized use") { + const String s = "Godot"; + + RegEx re; + ERR_PRINT_OFF; + CHECK(re.search(s) == nullptr); + CHECK(re.search_all(s).size() == 0); + CHECK(re.sub(s, "") == ""); + CHECK(re.get_group_count() == 0); + CHECK(re.get_names().size() == 0); + ERR_PRINT_ON +} + +TEST_CASE("[RegEx] Empty Pattern") { + const String s = "Godot"; + + RegEx re; + CHECK(re.compile("") == OK); + 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"; + + RegEx re("o"); + REQUIRE(re.is_valid()); + Ref<RegExMatch> match = re.search(s, 0, 10); + CHECK(match->get_string(0) == "o"); + + const Array all_results = re.search_all(s, 0, 10); + CHECK(all_results.size() == 2); + match = all_results[0]; + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == String("o")); + match = all_results[1]; + REQUIRE(match != nullptr); + CHECK(match->get_string(0) == String("o")); + + CHECK(re.sub(s, "", true, 0, 10) == "Gdt"); +} +} // namespace TestRegEx + +#endif // TEST_REGEX_H |