initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

80
modules/regex/SCsub Normal file
View File

@@ -0,0 +1,80 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
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"]
if env["builtin_pcre2_with_jit"]:
thirdparty_flags.append("SUPPORT_JIT")
thirdparty_sources = [
"pcre2_auto_possess.c",
"pcre2_chartables.c",
"pcre2_chkdint.c",
"pcre2_compile.c",
"pcre2_compile_class.c",
"pcre2_config.c",
"pcre2_context.c",
"pcre2_convert.c",
"pcre2_dfa_match.c",
"pcre2_error.c",
"pcre2_extuni.c",
"pcre2_find_bracket.c",
"pcre2_jit_compile.c",
# "pcre2_jit_match.c", "pcre2_jit_misc.c", # Included in `pcre2_jit_compile.c`.
"pcre2_maketables.c",
"pcre2_match.c",
"pcre2_match_data.c",
"pcre2_newline.c",
"pcre2_ord2utf.c",
"pcre2_pattern_info.c",
"pcre2_script_run.c",
"pcre2_serialize.c",
"pcre2_string_utils.c",
"pcre2_study.c",
"pcre2_substitute.c",
"pcre2_substring.c",
"pcre2_tables.c",
"pcre2_ucd.c",
# "pcre2_ucptables.c", # Included in `pcre2_tables.c`.
"pcre2_valid_utf.c",
"pcre2_xclass.c",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
env_regex.Prepend(CPPEXTPATH=[thirdparty_dir])
env_regex.Append(CPPDEFINES=thirdparty_flags)
def pcre2_builtin(width):
env_pcre2 = env_regex.Clone()
env_pcre2.disable_warnings()
env_pcre2["OBJSUFFIX"] = "_" + width + env_pcre2["OBJSUFFIX"]
env_pcre2.Append(CPPDEFINES=[("PCRE2_CODE_UNIT_WIDTH", width)])
env_pcre2.add_source_files(thirdparty_obj, thirdparty_sources)
pcre2_builtin("16")
pcre2_builtin("32")
env.modules_sources += thirdparty_obj
# Godot source files
module_obj = []
env_regex.Append(CPPDEFINES=[("PCRE2_CODE_UNIT_WIDTH", 0)])
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)

17
modules/regex/config.py Normal file
View File

@@ -0,0 +1,17 @@
def can_build(env, platform):
return True
def configure(env):
pass
def get_doc_classes():
return [
"RegEx",
"RegExMatch",
]
def get_doc_path():
return "doc_classes"

View File

@@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RegEx" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Class for searching text for patterns using regular expressions.
</brief_description>
<description>
A regular expression (or regex) is a compact language that can be used to recognize strings that follow a specific pattern, such as URLs, email addresses, complete sentences, etc. For example, a regex of [code]ab[0-9][/code] would find any string that is [code]ab[/code] followed by any number from [code]0[/code] to [code]9[/code]. For a more in-depth look, you can easily find various tutorials and detailed explanations on the Internet.
To begin, the RegEx object needs to be compiled with the search pattern using [method compile] before it can be used.
[codeblock]
var regex = RegEx.new()
regex.compile("\\w-(\\d+)")
[/codeblock]
The search pattern must be escaped first for GDScript before it is escaped for the expression. For example, [code]compile("\\d+")[/code] would be read by RegEx as [code]\d+[/code]. Similarly, [code]compile("\"(?:\\\\.|[^\"])*\"")[/code] would be read as [code]"(?:\\.|[^"])*"[/code]. In GDScript, you can also use raw string literals (r-strings). For example, [code]compile(r'"(?:\\.|[^"])*"')[/code] would be read the same.
Using [method search], you can find the pattern within the given text. If a pattern is found, [RegExMatch] is returned and you can retrieve details of the results using methods such as [method RegExMatch.get_string] and [method RegExMatch.get_start].
[codeblock]
var regex = RegEx.new()
regex.compile("\\w-(\\d+)")
var result = regex.search("abc n-0123")
if result:
print(result.get_string()) # Prints "n-0123"
[/codeblock]
The results of capturing groups [code]()[/code] can be retrieved by passing the group number to the various methods in [RegExMatch]. Group 0 is the default and will always refer to the entire pattern. In the above example, calling [code]result.get_string(1)[/code] would give you [code]0123[/code].
This version of RegEx also supports named capturing groups, and the names can be used to retrieve the results. If two or more groups have the same name, the name would only refer to the first one with a match.
[codeblock]
var regex = RegEx.new()
regex.compile("d(?&lt;digit&gt;[0-9]+)|x(?&lt;digit&gt;[0-9a-f]+)")
var result = regex.search("the number is x2f")
if result:
print(result.get_string("digit")) # Prints "2f"
[/codeblock]
If you need to process multiple results, [method search_all] generates a list of all non-overlapping results. This can be combined with a [code]for[/code] loop for convenience.
[codeblock]
# Prints "01 03 0 3f 42"
for result in regex.search_all("d01, d03, d0c, x3f and x42"):
print(result.get_string("digit"))
[/codeblock]
[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())
print(results) # Prints ["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].
[b]Tip:[/b] You can use [url=https://regexr.com/]Regexr[/url] to test regular expressions online.
</description>
<tutorials>
</tutorials>
<methods>
<method name="clear">
<return type="void" />
<description>
This method resets the state of the object, as if it was freshly created. Namely, it unassigns the regular expression of this object.
</description>
</method>
<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 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. See also [method compile].
</description>
</method>
<method name="get_group_count" qualifiers="const">
<return type="int" />
<description>
Returns the number of capturing groups in compiled pattern.
</description>
</method>
<method name="get_names" qualifiers="const">
<return type="PackedStringArray" />
<description>
Returns an array of names of named capturing groups in the compiled pattern. They are ordered by appearance.
</description>
</method>
<method name="get_pattern" qualifiers="const">
<return type="String" />
<description>
Returns the original search pattern that was compiled.
</description>
</method>
<method name="is_valid" qualifiers="const">
<return type="bool" />
<description>
Returns whether this object has a valid search pattern assigned.
</description>
</method>
<method name="search" qualifiers="const">
<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 a [RegExMatch] container of the first matching result if found, otherwise [code]null[/code].
The region to search within can be specified with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
</description>
</method>
<method name="search_all" qualifiers="const">
<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 with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
</description>
</method>
<method name="sub" qualifiers="const">
<return type="String" />
<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 with [param offset] and [param end]. This is useful when searching for another match in the same [param subject] by calling this method again after a previous success. Note that setting these parameters differs from passing over a shortened string. For example, the start anchor [code]^[/code] is not affected by [param offset], and the character before [param offset] will be checked for the word boundary [code]\b[/code].
</description>
</method>
</methods>
</class>

View File

@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RegExMatch" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Contains the results of a [RegEx] search.
</brief_description>
<description>
Contains the results of a single [RegEx] match returned by [method RegEx.search] and [method RegEx.search_all]. It can be used to find the position and range of the match and its capturing groups, and it can extract its substring for you.
</description>
<tutorials>
</tutorials>
<methods>
<method name="get_end" qualifiers="const">
<return type="int" />
<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.
</description>
</method>
<method name="get_group_count" qualifiers="const">
<return type="int" />
<description>
Returns the number of capturing groups.
</description>
</method>
<method name="get_start" qualifiers="const">
<return type="int" />
<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.
</description>
</method>
<method name="get_string" qualifiers="const">
<return type="String" />
<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.
</description>
</method>
</methods>
<members>
<member name="names" type="Dictionary" setter="" getter="get_names" default="{}">
A dictionary of named groups and its corresponding group number. Only groups that were matched are included. If multiple groups have the same name, that name would refer to the first matching one.
</member>
<member name="strings" type="PackedStringArray" setter="" getter="get_strings" default="PackedStringArray()">
An [Array] of the match and its capturing groups.
</member>
<member name="subject" type="String" setter="" getter="get_subject" default="&quot;&quot;">
The source string used with the search pattern to find this matching result.
</member>
</members>
</class>

View File

@@ -0,0 +1 @@
<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>

After

Width:  |  Height:  |  Size: 385 B

View File

@@ -0,0 +1 @@
<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>

After

Width:  |  Height:  |  Size: 467 B

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

431
modules/regex/regex.cpp Normal file
View File

@@ -0,0 +1,431 @@
/**************************************************************************/
/* regex.cpp */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#include "regex.h"
#include "regex.compat.inc"
#include "core/os/memory.h"
extern "C" {
#include <pcre2.h>
}
static void *_regex_malloc(PCRE2_SIZE size, void *user) {
return memalloc(size);
}
static void _regex_free(void *ptr, void *user) {
if (ptr) {
memfree(ptr);
}
}
int RegExMatch::_find(const Variant &p_name) const {
if (p_name.is_num()) {
int i = (int)p_name;
if (i >= data.size()) {
return -1;
}
return i;
} else if (p_name.is_string()) {
HashMap<String, int>::ConstIterator found = names.find(p_name);
if (found) {
return found->value;
}
}
return -1;
}
String RegExMatch::get_subject() const {
return subject;
}
int RegExMatch::get_group_count() const {
if (data.is_empty()) {
return 0;
}
return data.size() - 1;
}
Dictionary RegExMatch::get_names() const {
Dictionary result;
for (const KeyValue<String, int> &E : names) {
result[E.key] = E.value;
}
return result;
}
PackedStringArray RegExMatch::get_strings() const {
PackedStringArray result;
int size = data.size();
for (int i = 0; i < size; i++) {
int start = data[i].start;
if (start == -1) {
result.append(String());
continue;
}
int length = data[i].end - start;
result.append(subject.substr(start, length));
}
return result;
}
String RegExMatch::get_string(const Variant &p_name) const {
int id = _find(p_name);
if (id < 0) {
return String();
}
int start = data[id].start;
if (start == -1) {
return String();
}
int length = data[id].end - start;
return subject.substr(start, length);
}
int RegExMatch::get_start(const Variant &p_name) const {
int id = _find(p_name);
if (id < 0) {
return -1;
}
return data[id].start;
}
int RegExMatch::get_end(const Variant &p_name) const {
int id = _find(p_name);
if (id < 0) {
return -1;
}
return data[id].end;
}
void RegExMatch::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_subject"), &RegExMatch::get_subject);
ClassDB::bind_method(D_METHOD("get_group_count"), &RegExMatch::get_group_count);
ClassDB::bind_method(D_METHOD("get_names"), &RegExMatch::get_names);
ClassDB::bind_method(D_METHOD("get_strings"), &RegExMatch::get_strings);
ClassDB::bind_method(D_METHOD("get_string", "name"), &RegExMatch::get_string, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_start", "name"), &RegExMatch::get_start, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_end", "name"), &RegExMatch::get_end, DEFVAL(0));
ADD_PROPERTY(PropertyInfo(Variant::STRING, "subject"), "", "get_subject");
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "names"), "", "get_names");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "strings"), "", "get_strings");
}
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, bool p_show_error) {
Ref<RegEx> ret;
ret.instantiate();
ret->compile(p_pattern, p_show_error);
return ret;
}
void RegEx::clear() {
if (code) {
pcre2_code_free_32((pcre2_code_32 *)code);
code = nullptr;
}
}
Error RegEx::compile(const String &p_pattern, bool p_show_error) {
pattern = p_pattern;
clear();
int err;
PCRE2_SIZE offset;
uint32_t flags = PCRE2_DUPNAMES;
pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx;
pcre2_compile_context_32 *cctx = pcre2_compile_context_create_32(gctx);
PCRE2_SPTR32 p = (PCRE2_SPTR32)pattern.get_data();
code = pcre2_compile_32(p, pattern.length(), flags, &err, &offset, cctx);
pcre2_compile_context_free_32(cctx);
if (!code) {
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);
}
return FAILED;
}
return OK;
}
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);
int length = p_subject.length();
if (p_end >= 0 && p_end < length) {
length = p_end;
}
pcre2_code_32 *c = (pcre2_code_32 *)code;
pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx;
pcre2_match_context_32 *mctx = pcre2_match_context_create_32(gctx);
PCRE2_SPTR32 s = (PCRE2_SPTR32)p_subject.get_data();
pcre2_match_data_32 *match = pcre2_match_data_create_from_pattern_32(c, gctx);
int res = pcre2_match_32(c, s, length, p_offset, 0, match, mctx);
if (res < 0) {
pcre2_match_data_free_32(match);
pcre2_match_context_free_32(mctx);
return nullptr;
}
uint32_t size = pcre2_get_ovector_count_32(match);
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_32(match);
result->data.resize(size);
for (uint32_t i = 0; i < size; i++) {
result->data.write[i].start = ovector[i * 2];
result->data.write[i].end = ovector[i * 2 + 1];
}
pcre2_match_data_free_32(match);
pcre2_match_context_free_32(mctx);
result->subject = p_subject;
uint32_t count;
const char32_t *table;
uint32_t entry_size;
_pattern_info(PCRE2_INFO_NAMECOUNT, &count);
_pattern_info(PCRE2_INFO_NAMETABLE, &table);
_pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size);
for (uint32_t i = 0; i < count; i++) {
char32_t id = table[i * entry_size];
if (result->data[id].start == -1) {
continue;
}
String name = &table[i * entry_size + 1];
if (result->names.has(name)) {
continue;
}
result->names.insert(name, id);
}
return result;
}
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 = 0;
TypedArray<RegExMatch> result;
Ref<RegExMatch> match = search(p_subject, p_offset, p_end);
while (match.is_valid()) {
last_end = match->get_end(0);
if (match->get_start(0) == last_end) {
last_end++;
}
result.push_back(match);
match = search(p_subject, last_end, p_end);
}
return result;
}
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.
Vector<char32_t> output;
output.resize(olength + safety_zone);
PCRE2_SIZE length = p_subject.length();
if (p_end >= 0 && (uint32_t)p_end < length) {
length = p_end;
}
pcre2_code_32 *c = (pcre2_code_32 *)code;
pcre2_general_context_32 *gctx = (pcre2_general_context_32 *)general_ctx;
pcre2_match_context_32 *mctx = pcre2_match_context_create_32(gctx);
PCRE2_SPTR32 s = (PCRE2_SPTR32)p_subject.get_data();
PCRE2_SPTR32 r = (PCRE2_SPTR32)p_replacement.get_data();
PCRE2_UCHAR32 *o = (PCRE2_UCHAR32 *)output.ptrw();
pcre2_match_data_32 *match = pcre2_match_data_create_from_pattern_32(c, gctx);
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, 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) {
r_output = String::utf32(Span(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);
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 {
return (code != nullptr);
}
String RegEx::get_pattern() const {
return pattern;
}
int RegEx::get_group_count() const {
ERR_FAIL_COND_V(!is_valid(), 0);
uint32_t count;
_pattern_info(PCRE2_INFO_CAPTURECOUNT, &count);
return count;
}
PackedStringArray RegEx::get_names() const {
PackedStringArray result;
ERR_FAIL_COND_V(!is_valid(), result);
uint32_t count;
const char32_t *table;
uint32_t entry_size;
_pattern_info(PCRE2_INFO_NAMECOUNT, &count);
_pattern_info(PCRE2_INFO_NAMETABLE, &table);
_pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size);
for (uint32_t i = 0; i < count; i++) {
String name = &table[i * entry_size + 1];
if (!result.has(name)) {
result.append(name);
}
}
return result;
}
RegEx::RegEx() {
general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr);
}
RegEx::RegEx(const String &p_pattern) {
general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, nullptr);
compile(p_pattern);
}
RegEx::~RegEx() {
if (code) {
pcre2_code_free_32((pcre2_code_32 *)code);
}
pcre2_general_context_free_32((pcre2_general_context_32 *)general_ctx);
}
void RegEx::_bind_methods() {
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", "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));
ClassDB::bind_method(D_METHOD("is_valid"), &RegEx::is_valid);
ClassDB::bind_method(D_METHOD("get_pattern"), &RegEx::get_pattern);
ClassDB::bind_method(D_METHOD("get_group_count"), &RegEx::get_group_count);
ClassDB::bind_method(D_METHOD("get_names"), &RegEx::get_names);
}

109
modules/regex/regex.h Normal file
View File

@@ -0,0 +1,109 @@
/**************************************************************************/
/* regex.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "core/object/ref_counted.h"
#include "core/string/ustring.h"
#include "core/templates/hash_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);
struct Range {
int start = 0;
int end = 0;
};
String subject;
Vector<Range> data;
HashMap<String, int> names;
friend class RegEx;
protected:
static void _bind_methods();
int _find(const Variant &p_name) const;
public:
String get_subject() const;
int get_group_count() const;
Dictionary get_names() const;
PackedStringArray get_strings() const;
String get_string(const Variant &p_name) const;
int get_start(const Variant &p_name) const;
int get_end(const Variant &p_name) const;
};
class RegEx : public RefCounted {
GDCLASS(RegEx, RefCounted);
void *general_ctx = nullptr;
void *code = nullptr;
String pattern;
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, bool p_show_error = true);
void clear();
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;
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;
PackedStringArray get_names() const;
RegEx();
RegEx(const String &p_pattern);
~RegEx();
};

View File

@@ -0,0 +1,50 @@
/**************************************************************************/
/* register_types.cpp */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#include "register_types.h"
#include "regex.h"
#include "core/object/class_db.h"
void initialize_regex_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
GDREGISTER_CLASS(RegExMatch);
GDREGISTER_CLASS(RegEx);
}
void uninitialize_regex_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}

View File

@@ -0,0 +1,36 @@
/**************************************************************************/
/* register_types.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "modules/register_module_types.h"
void initialize_regex_module(ModuleInitializationLevel p_level);
void uninitialize_regex_module(ModuleInitializationLevel p_level);

View File

@@ -0,0 +1,424 @@
/**************************************************************************/
/* test_regex.h */
/**************************************************************************/
/* 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. */
/**************************************************************************/
#pragma once
#include "../regex.h"
#include "core/string/ustring.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);
PackedStringArray 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.is_valid());
CHECK(match->get_string(0) == "ea");
match = re.search(s, 1, 2);
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "e");
match = re.search(s, 2, 4);
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "a");
match = re.search(s, 3, 5);
CHECK(match.is_null());
match = re.search(s, 6, 2);
CHECK(match.is_null());
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 2);
match = all_results[0];
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "ea");
match = all_results[1];
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == "i");
CHECK(re.compile(numerics) == OK);
CHECK(re.is_valid());
CHECK(re.search(s).is_null());
CHECK(re.search_all(s).size() == 0);
}
TEST_CASE("[RegEx] Substitution") {
const String s1 = "Double all the vowels.";
RegEx re1("(?<vowel>[aeiou])");
REQUIRE(re1.is_valid());
CHECK(re1.sub(s1, "$0$vowel", true) == "Doouublee aall thee vooweels.");
const String s2 = "Substitution with group.";
RegEx re2("Substitution (.+)");
REQUIRE(re2.is_valid());
CHECK(re2.sub(s2, "Test ${1}") == "Test with group.");
const String s3 = "Useless substitution";
RegEx re3("Anything");
REQUIRE(re3.is_valid());
CHECK(re3.sub(s3, "Something") == "Useless substitution");
const String s4 = "acacac";
RegEx re4("(a)(b){0}(c)");
REQUIRE(re4.is_valid());
CHECK(re4.sub(s4, "${1}.${3}.", true) == "a.c.a.c.a.c.");
const String s5 = "aaaa";
RegEx re5("a");
REQUIRE(re5.is_valid());
CHECK(re5.sub(s5, "b", true, 0, 2) == "bbaa");
CHECK(re5.sub(s5, "b", true, 1, 3) == "abba");
CHECK(re5.sub(s5, "b", true, 0, 0) == "aaaa");
CHECK(re5.sub(s5, "b", true, 1, 1) == "aaaa");
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") {
const String s1 = "";
const String s2 = "gogogo";
RegEx re1("");
REQUIRE(re1.is_valid());
CHECK(re1.sub(s1, "") == "");
CHECK(re1.sub(s1, "a") == "a");
CHECK(re1.sub(s2, "") == "gogogo");
RegEx re2("go");
REQUIRE(re2.is_valid());
CHECK(re2.sub(s2, "") == "gogo");
CHECK(re2.sub(s2, "", true) == "");
}
TEST_CASE("[RegEx] Uninitialized use") {
const String s = "Godot";
RegEx re;
ERR_PRINT_OFF;
CHECK(re.search(s).is_null());
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] Complex Grouping") {
const String test = "https://docs.godotengine.org/en/latest/contributing/";
// Ignored protocol in grouping.
RegEx re("^(?:https?://)([a-zA-Z]{2,4})\\.([a-zA-Z][a-zA-Z0-9_\\-]{2,64})\\.([a-zA-Z]{2,4})");
REQUIRE(re.is_valid());
Ref<RegExMatch> expr = re.search(test);
CHECK(expr->get_group_count() == 3);
CHECK(expr->get_string(0) == "https://docs.godotengine.org");
CHECK(expr->get_string(1) == "docs");
CHECK(expr->get_string(2) == "godotengine");
CHECK(expr->get_string(3) == "org");
}
TEST_CASE("[RegEx] Number Expression") {
const String test = "(2.5e-3 + 35 + 46) / 2.8e0 = 28.9294642857";
// Not an exact regex for number but a good test.
RegEx re("([+-]?\\d+)(\\.\\d+([eE][+-]?\\d+)?)?");
REQUIRE(re.is_valid());
Array number_match = re.search_all(test);
CHECK(number_match.size() == 5);
Ref<RegExMatch> number = number_match[0];
CHECK(number->get_string(0) == "2.5e-3");
CHECK(number->get_string(1) == "2");
number = number_match[1];
CHECK(number->get_string(0) == "35");
number = number_match[2];
CHECK(number->get_string(0) == "46");
number = number_match[3];
CHECK(number->get_string(0) == "2.8e0");
number = number_match[4];
CHECK(number->get_string(0) == "28.9294642857");
CHECK(number->get_string(1) == "28");
CHECK(number->get_string(2) == ".9294642857");
}
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.is_valid());
CHECK(match->get_string(0) == String("o"));
match = all_results[1];
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String("o"));
CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
}
TEST_CASE("[RegEx] Get match string list") {
const String s = "Godot Engine";
RegEx re("(Go)(dot)");
Ref<RegExMatch> match = re.search(s);
REQUIRE(match.is_valid());
PackedStringArray result;
result.append("Godot");
result.append("Go");
result.append("dot");
CHECK(match->get_strings() == result);
}
TEST_CASE("[RegEx] Match start and end positions") {
const String s = "Whole pattern";
RegEx re1("pattern");
REQUIRE(re1.is_valid());
Ref<RegExMatch> match = re1.search(s);
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.is_valid());
CHECK(match->get_start("vowel") == 2);
CHECK(match->get_end("vowel") == 3);
}
TEST_CASE("[RegEx] Asterisk search all") {
const String s = "Godot Engine";
RegEx re("o*");
REQUIRE(re.is_valid());
Ref<RegExMatch> match;
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 13);
match = all_results[0];
CHECK(match->get_string(0) == "");
match = all_results[1];
CHECK(match->get_string(0) == "o");
match = all_results[2];
CHECK(match->get_string(0) == "");
match = all_results[3];
CHECK(match->get_string(0) == "o");
for (int i = 4; i < 13; i++) {
match = all_results[i];
CHECK(match->get_string(0) == "");
}
}
TEST_CASE("[RegEx] Simple lookahead") {
const String s = "Godot Engine";
RegEx re("o(?=t)");
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
}
TEST_CASE("[RegEx] Lookahead groups empty matches") {
const String s = "12";
RegEx re("(?=(\\d+))");
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
CHECK(match->get_string(0) == "");
CHECK(match->get_string(1) == "12");
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 2);
match = all_results[0];
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("12"));
match = all_results[1];
REQUIRE(match.is_valid());
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("2"));
}
TEST_CASE("[RegEx] Simple lookbehind") {
const String s = "Godot Engine";
RegEx re("(?<=d)o");
REQUIRE(re.is_valid());
Ref<RegExMatch> match = re.search(s);
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
}
TEST_CASE("[RegEx] Simple lookbehind search all") {
const String s = "ababbaabab";
RegEx re("(?<=a)b");
REQUIRE(re.is_valid());
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 4);
Ref<RegExMatch> match = all_results[0];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 1);
CHECK(match->get_end(0) == 2);
match = all_results[1];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 3);
CHECK(match->get_end(0) == 4);
match = all_results[2];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 7);
CHECK(match->get_end(0) == 8);
match = all_results[3];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 9);
CHECK(match->get_end(0) == 10);
}
TEST_CASE("[RegEx] Lookbehind groups empty matches") {
const String s = "abaaabab";
RegEx re("(?<=(b))");
REQUIRE(re.is_valid());
Ref<RegExMatch> match;
const Array all_results = re.search_all(s);
CHECK(all_results.size() == 3);
match = all_results[0];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 2);
CHECK(match->get_end(0) == 2);
CHECK(match->get_start(1) == 1);
CHECK(match->get_end(1) == 2);
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("b"));
match = all_results[1];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 6);
CHECK(match->get_end(0) == 6);
CHECK(match->get_start(1) == 5);
CHECK(match->get_end(1) == 6);
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("b"));
match = all_results[2];
REQUIRE(match.is_valid());
CHECK(match->get_start(0) == 8);
CHECK(match->get_end(0) == 8);
CHECK(match->get_start(1) == 7);
CHECK(match->get_end(1) == 8);
CHECK(match->get_string(0) == String(""));
CHECK(match->get_string(1) == String("b"));
}
} // namespace TestRegEx