Merge pull request #48183 from madmiraal/fix-regex-offset
Generate error if RegEx offset is negative
This commit is contained in:
commit
335fc2a946
|
@ -194,6 +194,7 @@ Error RegEx::compile(const String &p_pattern) {
|
||||||
|
|
||||||
Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end) const {
|
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(!is_valid(), nullptr);
|
||||||
|
ERR_FAIL_COND_V_MSG(p_offset < 0, nullptr, "RegEx search offset must be >= 0");
|
||||||
|
|
||||||
Ref<RegExMatch> result = memnew(RegExMatch);
|
Ref<RegExMatch> result = memnew(RegExMatch);
|
||||||
|
|
||||||
|
@ -258,6 +259,8 @@ Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end)
|
||||||
}
|
}
|
||||||
|
|
||||||
Array RegEx::search_all(const String &p_subject, int p_offset, int p_end) const {
|
Array 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;
|
int last_end = -1;
|
||||||
Array result;
|
Array result;
|
||||||
Ref<RegExMatch> match = search(p_subject, p_offset, p_end);
|
Ref<RegExMatch> match = search(p_subject, p_offset, p_end);
|
||||||
|
@ -274,6 +277,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 {
|
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(!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
|
// 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
|
// guard against the PCRE API writing one additional \0 at the end. PCRE's API docs are unclear on whether
|
||||||
|
|
|
@ -130,16 +130,6 @@ TEST_CASE("[RegEx] Empty Pattern") {
|
||||||
CHECK(re.is_valid());
|
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") {
|
TEST_CASE("[RegEx] Invalid end position") {
|
||||||
const String s = "Godot";
|
const String s = "Godot";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue