2016-10-23 00:22:48 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* regex.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-10-23 00:22:48 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-10-23 00:22:48 +00:00
|
|
|
/* */
|
|
|
|
/* 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"
|
2017-08-07 15:13:15 +00:00
|
|
|
#include "core/os/memory.h"
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <pcre2.h>
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
static void *_regex_malloc(PCRE2_SIZE size, void *user) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return memalloc(size);
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
static void _regex_free(void *ptr, void *user) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
memfree(ptr);
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int RegExMatch::_find(const Variant &p_name) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (p_name.is_num()) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int i = (int)p_name;
|
|
|
|
if (i >= data.size())
|
2016-10-23 00:22:48 +00:00
|
|
|
return -1;
|
2017-08-07 15:13:15 +00:00
|
|
|
return i;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else if (p_name.get_type() == Variant::STRING) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
const Map<String, int>::Element *found = names.find((String)p_name);
|
|
|
|
if (found)
|
|
|
|
return found->value();
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String RegExMatch::get_subject() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return subject;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int RegExMatch::get_group_count() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (data.size() == 0)
|
|
|
|
return 0;
|
|
|
|
return data.size() - 1;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Dictionary RegExMatch::get_names() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Dictionary result;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (const Map<String, int>::Element *i = names.front(); i != NULL; i = i->next()) {
|
|
|
|
result[i->key()] = i->value();
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Array RegExMatch::get_strings() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Array result;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int size = data.size();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int start = data[i].start;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (start == -1) {
|
|
|
|
result.append(String());
|
|
|
|
continue;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int length = data[i].end - start;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result.append(subject.substr(start, length));
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String RegExMatch::get_string(const Variant &p_name) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int id = _find(p_name);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (id < 0)
|
|
|
|
return String();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int start = data[id].start;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (start == -1)
|
|
|
|
return String();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int length = data[id].end - start;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return subject.substr(start, length);
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int RegExMatch::get_start(const Variant &p_name) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int id = _find(p_name);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (id < 0)
|
|
|
|
return -1;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return data[id].start;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int RegExMatch::get_end(const Variant &p_name) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int id = _find(p_name);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (id < 0)
|
|
|
|
return -1;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return data[id].end;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
void RegExMatch::_bind_methods() {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
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));
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
void RegEx::_pattern_info(uint32_t what, void *where) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_pattern_info_16((pcre2_code_16 *)code, what, where);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_pattern_info_32((pcre2_code_32 *)code, what, where);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
2017-08-07 15:13:15 +00:00
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
void RegEx::clear() {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (code)
|
|
|
|
pcre2_code_free_16((pcre2_code_16 *)code);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (code)
|
|
|
|
pcre2_code_free_32((pcre2_code_32 *)code);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
2017-08-07 15:13:15 +00:00
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Error RegEx::compile(const String &p_pattern) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pattern = p_pattern;
|
|
|
|
clear();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int err;
|
|
|
|
PCRE2_SIZE offset;
|
|
|
|
uint32_t flags = PCRE2_DUPNAMES;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx;
|
|
|
|
pcre2_compile_context_16 *cctx = pcre2_compile_context_create_16(gctx);
|
|
|
|
PCRE2_SPTR16 p = (PCRE2_SPTR16)pattern.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
code = pcre2_compile_16(p, pattern.length(), flags, &err, &offset, cctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (!code) {
|
|
|
|
PCRE2_UCHAR16 buf[256];
|
|
|
|
pcre2_get_error_message_16(err, buf, 256);
|
|
|
|
String message = String::num(offset) + ": " + String((const CharType *)buf);
|
|
|
|
ERR_PRINT(message.utf8());
|
|
|
|
return FAILED;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
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.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
code = pcre2_compile_32(p, pattern.length(), flags, &err, &offset, cctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (!code) {
|
|
|
|
PCRE2_UCHAR32 buf[256];
|
|
|
|
pcre2_get_error_message_32(err, buf, 256);
|
|
|
|
String message = String::num(offset) + ": " + String((const CharType *)buf);
|
|
|
|
ERR_PRINT(message.utf8());
|
|
|
|
return FAILED;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-07 15:13:15 +00:00
|
|
|
return OK;
|
|
|
|
}
|
2017-06-26 07:16:33 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Ref<RegExMatch> RegEx::search(const String &p_subject, int p_offset, int p_end) const {
|
2017-06-26 07:16:33 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), NULL);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Ref<RegExMatch> result = memnew(RegExMatch);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int length = p_subject.length();
|
|
|
|
if (p_end >= 0 && p_end < length)
|
|
|
|
length = p_end;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_code_16 *c = (pcre2_code_16 *)code;
|
|
|
|
pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx;
|
|
|
|
pcre2_match_context_16 *mctx = pcre2_match_context_create_16(gctx);
|
|
|
|
PCRE2_SPTR16 s = (PCRE2_SPTR16)p_subject.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_16 *match = pcre2_match_data_create_from_pattern_16(c, gctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int res = pcre2_match_16(c, s, length, p_offset, 0, match, mctx);
|
2017-06-26 07:25:10 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res < 0) {
|
|
|
|
pcre2_match_data_free_16(match);
|
|
|
|
return NULL;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t size = pcre2_get_ovector_count_16(match);
|
|
|
|
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_16(match);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->data.resize(size);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->data[i].start = ovector[i * 2];
|
|
|
|
result->data[i].end = ovector[i * 2 + 1];
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_free_16(match);
|
|
|
|
pcre2_match_context_free_16(mctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
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.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_32 *match = pcre2_match_data_create_from_pattern_32(c, gctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int res = pcre2_match_32(c, s, length, p_offset, 0, match, mctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res < 0) {
|
|
|
|
pcre2_match_data_free_32(match);
|
|
|
|
return NULL;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t size = pcre2_get_ovector_count_32(match);
|
|
|
|
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer_32(match);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->data.resize(size);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->data[i].start = ovector[i * 2];
|
|
|
|
result->data[i].end = ovector[i * 2 + 1];
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_free_32(match);
|
|
|
|
pcre2_match_context_free_32(mctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->subject = p_subject;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t count;
|
|
|
|
const CharType *table;
|
|
|
|
uint32_t entry_size;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
_pattern_info(PCRE2_INFO_NAMECOUNT, &count);
|
|
|
|
_pattern_info(PCRE2_INFO_NAMETABLE, &table);
|
|
|
|
_pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2016-11-01 18:16:46 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
CharType 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;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
result->names.insert(name, id);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String RegEx::sub(const String &p_subject, const String &p_replacement, bool p_all, int p_offset, int p_end) const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), String());
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String output;
|
|
|
|
output.resize(p_subject.length());
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t flags = PCRE2_SUBSTITUTE_OVERFLOW_LENGTH;
|
|
|
|
if (p_all)
|
|
|
|
flags |= PCRE2_SUBSTITUTE_GLOBAL;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
PCRE2_SIZE olength = output.length();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
PCRE2_SIZE length = p_subject.length();
|
|
|
|
if (p_end >= 0 && p_end < length)
|
|
|
|
length = p_end;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_code_16 *c = (pcre2_code_16 *)code;
|
|
|
|
pcre2_general_context_16 *gctx = (pcre2_general_context_16 *)general_ctx;
|
|
|
|
pcre2_match_context_16 *mctx = pcre2_match_context_create_16(gctx);
|
|
|
|
PCRE2_SPTR16 s = (PCRE2_SPTR16)p_subject.c_str();
|
|
|
|
PCRE2_SPTR16 r = (PCRE2_SPTR16)p_replacement.c_str();
|
|
|
|
PCRE2_UCHAR16 *o = (PCRE2_UCHAR16 *)output.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_16 *match = pcre2_match_data_create_from_pattern_16(c, gctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int res = pcre2_substitute_16(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res == PCRE2_ERROR_NOMEMORY) {
|
|
|
|
output.resize(olength);
|
|
|
|
o = (PCRE2_UCHAR16 *)output.c_str();
|
|
|
|
res = pcre2_substitute_16(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_free_16(match);
|
|
|
|
pcre2_match_context_free_16(mctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res < 0)
|
|
|
|
return String();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
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.c_str();
|
|
|
|
PCRE2_SPTR32 r = (PCRE2_SPTR32)p_replacement.c_str();
|
|
|
|
PCRE2_UCHAR32 *o = (PCRE2_UCHAR32 *)output.c_str();
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_32 *match = pcre2_match_data_create_from_pattern_32(c, gctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int res = pcre2_substitute_32(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res == PCRE2_ERROR_NOMEMORY) {
|
|
|
|
output.resize(olength);
|
|
|
|
o = (PCRE2_UCHAR32 *)output.c_str();
|
|
|
|
res = pcre2_substitute_32(c, s, length, p_offset, flags, match, mctx, r, p_replacement.length(), o, &olength);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
pcre2_match_data_free_32(match);
|
|
|
|
pcre2_match_context_free_32(mctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (res < 0)
|
2016-10-23 00:22:48 +00:00
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return output;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
bool RegEx::is_valid() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return (code != NULL);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String RegEx::get_pattern() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return pattern;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
int RegEx::get_group_count() const {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), 0);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t count;
|
2016-10-26 12:05:00 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
_pattern_info(PCRE2_INFO_CAPTURECOUNT, &count);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return count;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Array RegEx::get_names() const {
|
2016-10-24 21:13:26 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
Array result;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
ERR_FAIL_COND_V(!is_valid(), result);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
uint32_t count;
|
|
|
|
const CharType *table;
|
|
|
|
uint32_t entry_size;
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
_pattern_info(PCRE2_INFO_NAMECOUNT, &count);
|
|
|
|
_pattern_info(PCRE2_INFO_NAMETABLE, &table);
|
|
|
|
_pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &entry_size);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
String name = &table[i * entry_size + 1];
|
|
|
|
if (result.find(name) < 0) {
|
|
|
|
result.append(name);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
return result;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
RegEx::RegEx() {
|
2016-10-24 21:13:26 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-24 21:13:26 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
general_ctx = pcre2_general_context_create_16(&_regex_malloc, &_regex_free, NULL);
|
2016-10-24 21:13:26 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-24 21:13:26 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, NULL);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
2017-08-07 15:13:15 +00:00
|
|
|
code = NULL;
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
RegEx::RegEx(const String &p_pattern) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
general_ctx = pcre2_general_context_create_16(&_regex_malloc, &_regex_free, NULL);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
general_ctx = pcre2_general_context_create_32(&_regex_malloc, &_regex_free, NULL);
|
|
|
|
}
|
|
|
|
code = NULL;
|
2016-10-23 00:22:48 +00:00
|
|
|
compile(p_pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
RegEx::~RegEx() {
|
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (sizeof(CharType) == 2) {
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
if (code)
|
|
|
|
pcre2_code_free_16((pcre2_code_16 *)code);
|
|
|
|
pcre2_general_context_free_16((pcre2_general_context_16 *)general_ctx);
|
2016-10-23 00:22:48 +00:00
|
|
|
|
2017-08-07 15:13:15 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (code)
|
|
|
|
pcre2_code_free_32((pcre2_code_32 *)code);
|
|
|
|
pcre2_general_context_free_32((pcre2_general_context_32 *)general_ctx);
|
|
|
|
}
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegEx::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &RegEx::clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("compile", "pattern"), &RegEx::compile);
|
2017-08-07 15:13:15 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("search", "subject", "offset", "end"), &RegEx::search, DEFVAL(0), DEFVAL(-1));
|
|
|
|
ClassDB::bind_method(D_METHOD("sub", "subject", "replacement", "all", "offset", "end"), &RegEx::sub, DEFVAL(false), DEFVAL(0), DEFVAL(-1));
|
2017-03-05 15:44:50 +00:00
|
|
|
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);
|
2016-10-23 00:22:48 +00:00
|
|
|
}
|